简体   繁体   English

如何使用python在openCV中将一系列HSV值的所有像素设置为另一个值?

[英]how to set all pixels of a range of HSV values to another value in openCV with python?

i am using OpenCV with python. 我正在使用带有Python的OpenCV。 i want to replace the range of color by " white " color. 我想用“ 白色 ”颜色代替颜色范围。

color range: 颜色范围:

HMin = 130
SMin = 65
VMin = 135
HMax = 200
SMax = 160
VMax = 255

i know how to replace specific pixel value to another value: 我知道如何将特定的像素值替换为另一个值:

im[np.where((im == [20,78,90]).all(axis = 2))] = [0,0,100]
cv2.imwrite('output.png', im)

so, do you know how could i use this method to set all pixels of a range of HSV values instead of a specific pixel value to another value? 因此,您知道我该如何使用这种方法将一系列 HSV值的所有像素而不是将特定像素值设置为另一个值?

Use inRange to find the mask, then replace the origin image with other value. 使用inRange查找遮罩,然后将原始图像替换为其他值。

The following is replace the GREEN range into RED. 以下是将绿色范围替换为红色。

在此处输入图片说明

import numpy as np
import cv2

img = cv2.imread( "sunflower.jpg" )

## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## mask of green (36,0,0) ~ (70, 255,255)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
bak = img.copy()

# replace with red 
bak[mask > 0] = (0, 0, 255)

cv2.imwrite("bak.png", bak)

Some links: 一些链接:

  1. How to define a threshold value to detect only green colour objects in an image :Opencv 如何定义阈值以仅检测图像中的绿色对象:Opencv

  2. How to change the image color when the pixels hsv values are in specific ranges? 当像素hsv值在特定范围内时,如何更改图像颜色?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM