输入图片链接为:Input Image
我想读取输入图像的中心像素值,如上所示并检测颜色如果输出颜色为橙色则打印橙色。但我倾向于得到一个错误
if (center_px == ORANGE):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
代码如图所示
import numpy as np
import cv2
img = cv2.imread('New Bitmap Image.bmp')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,contours,-1,(0,0,255),19)
for i in range(0,len(contours)):
M = cv2.moments(contours[i])
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print "Centroid = ", cx, ", ", cy
center_px = img[cx,cy]
print center_px
ORANGE = (0,127,255)
if (center_px == ORANGE):
print "Orange"
我正在使用pyhton和cv2
答案 0 :(得分:1)
要测试数组是否相等,可以使用numpy.array_equal
center_px = img[cy, cx]
if np.array_equal(center_px, (0, 127, 255)):
print("Orange")