我刚开始打开 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")
我希望看到给定图像中存在输出绿色
最佳答案
您可以使用 cv2.countNonZero()
在蒙版图像上。由于 cv2.inRange()
返回最小/最大颜色阈值内所有像素的二进制掩码,其想法是,如果掩码上至少有一个白色像素,则该颜色存在
pixels = cv2.countNonZero(gmask)
if pixels > 0:
print("green exist")
else:
print("not found")
关于python - 如何查找图像中是否存在颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57929939/