如何查找图像中是否存在颜色



我是打开 cv 并试图检测我的图像中是否存在绿色的新手。

我的 cv2.range 中有颜色上限和下限颜色边界。当我打开cv2.bitwise_and看到颜色时,它显示有绿色,但我不知道如何打印绿色是否存在

hsv_image= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lg = np.array([56,255,251])        
ug = np.array([60,255,255])
gmask = cv2.inRange(hsv_image,lg ,ug)
color = cv2.bitwise_and(img,img,mask=gmask)

if gmask.equals(img):
print("green exist")
else: 
print("not found")

我希望看到输出绿色存在于给定图像中

您可以在蒙版图像上使用cv2.countNonZero()。由于cv2.inRange()返回最小/最大颜色阈值内所有像素的二进制蒙版,因此其想法是,如果蒙版上至少有一个白色像素,则颜色存在

pixels = cv2.countNonZero(gmask)
if pixels > 0:
print("green exist")
else: 
print("not found")

最新更新