【问题标题】:How to find if color exists in an image如何查找图像中是否存在颜色
【发布时间】:2019-09-13 20:23:22
【问题描述】:

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

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

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")

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

【问题讨论】:

    标签: python image opencv image-processing computer-vision


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      相关资源
      最近更新 更多