[英]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: 一些链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.