[英]Identifying the range of a color in HSV using openCV
我正在使用 python 中的 openCV 识别黄色。 我已经到了这一步,我必须在 HSV 中定义黄色的下限和上限。
定义蓝色范围的示例:
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
HSV通常以百分比定义,我想知道如何像示例一样定义黄色的范围。
编辑:上面提到的博客中有一些建议,但它没有给我想要的输出。
很简单。 您可以使用函数cv2.cvtColor()
。
您只需传递要转换为 hsv 的 BGR 值,而不是传递图像。
例如,要查找 Green 的 HSV 值,请键入以下命令
import numpy as np
import cv2
green = np.uint8([[[0, 255, 0]]]) #here insert the bgr values which you want to convert to hsv
hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print(hsvGreen)
lowerLimit = hsvGreen[0][0][0] - 10, 100, 100
upperLimit = hsvGreen[0][0][0] + 10, 255, 255
print(upperLimit)
print(lowerLimit)
现在,上限将是[H+10, 100,100]
下限为[H-10, 255, 255]
官方文档(请参阅以下网页的最后一部分) https://docs.opencv.org/3.1.0/df/d9d/tutorial_py_colorspaces.html
看看这个页面,你会发现你想要的颜色的 HSV 值。
对于 HSV,色调范围为 [0,179],饱和度范围为 [0,255],值范围为 [0,255]。 不同的软件使用不同的尺度。 因此,如果您将 OpenCV 值与它们进行比较,则需要对这些范围进行归一化。
我猜你正在搜索下面的黄色值
lower_blue = np.array([25,50,50])
upper_blue = np.array([32,255,255])
您可以使用此示例调色板。 第一个值是上限,第二个值是下限
color_dict_HSV = {'black': [[180, 255, 30], [0, 0, 0]],
'white': [[180, 18, 255], [0, 0, 231]],
'red1': [[180, 255, 255], [159, 50, 70]],
'red2': [[9, 255, 255], [0, 50, 70]],
'green': [[89, 255, 255], [36, 50, 70]],
'blue': [[128, 255, 255], [90, 50, 70]],
'yellow': [[35, 255, 255], [25, 50, 70]],
'purple': [[158, 255, 255], [129, 50, 70]],
'orange': [[24, 255, 255], [10, 50, 70]],
'gray': [[180, 18, 230], [0, 0, 40]]}
如果我想这样做,首先找到黄色的 rgb 数字(我在油漆中使用“编辑颜色”),然后使用此方法将它们更改为 HSV:
u = np.uint8([[[0,236,236]]])
# define range of blue color in HSV
lower_yellow = np.array(cv2.cvtColor(l,cv2.COLOR_BGR2HSV))
upper_yellow = np.array( cv2.cvtColor(u,cv2.COLOR_BGR2HSV))```
如果您从相机拍摄照片,这将取决于照明条件。 如果您的目标是跟踪某些对象,则应始终更新 HSV 值。 我的建议是在您的照明条件下保持您的边界尽可能窄。
color_dict_HSV = {'black': [[180, 255, 30], [0, 0, 0]],
'white': [[180, 18, 255], [0, 0, 231]],
'red1': [[180, 255, 255], [159, 50, 70]],
'red2': [[9, 255, 255], [0, 50, 70]],
'green': [[89, 255, 255], [36, 50, 70]],
'blue': [[128, 255, 255], [90, 50, 70]],
'yellow': [[35, 255, 255], [25, 50, 70]],
'purple': [[158, 255, 255], [129, 50, 70]],
'orange': [[24, 255, 255], [10, 50, 70]],
'gray': [[180, 18, 230], [0, 0, 40]]}
阿里·哈希米安
由于你们大多数人都想这样做,即在我的情况下,任务是从图像中删除蓝色,我使用以下代码从我的图像中删除蓝色墨水图章和蓝色刻度线,以便使用正确的 OCR立方体。
[颜色去除] 代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# image path:
#path = "D://opencvImages//"
#fileName = "out.jpg"
# Reading an image in default mode:
inputImage = cv2.imread('0.jpg')
# Convert RGB to grayscale:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Convert the BGR image to HSV:
hsvImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)
# Create the HSV range for the blue ink:
# [128, 255, 255], [90, 50, 70]
lowerValues = np.array([90, 50, 70])
upperValues = np.array([128, 255, 255])
# Get binary mask of the blue ink:
bluepenMask = cv2.inRange(hsvImage, lowerValues, upperValues)
# Use a little bit of morphology to clean the mask:
# Set kernel (structuring element) size:
kernelSize = 3
# Set morph operation iterations:
opIterations = 1
# Get the structuring element:
morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
# Perform closing:
bluepenMask = cv2.morphologyEx(bluepenMask, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)
# Add the white mask to the grayscale image:
colorMask = cv2.add(grayscaleImage, bluepenMask)
_, binaryImage = cv2.threshold(colorMask, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imwrite('bwimage.jpg',binaryImage)
thresh, im_bw = cv2.threshold(binaryImage, 210, 230, cv2.THRESH_BINARY)
kernel = np.ones((1, 1), np.uint8)
imgfinal = cv2.dilate(im_bw, kernel=kernel, iterations=1)
cv2.imshow(imgfinal)
在这里你可以看到几乎所有的刻度线都被删除了,原因是因为总是有改进的空间,但这似乎是我们能得到的最好的,因为即使删除这些小标记也不会有使用 Tesseract 对 OCR 产生深远影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.