如何读取中心像素颜色
How to read center pixel color
输入图像link是: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
要测试数组是否相等,可以使用numpy.array_equal
center_px = img[cy, cx]
if np.array_equal(center_px, (0, 127, 255)):
print("Orange")
输入图像link是: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
要测试数组是否相等,可以使用numpy.array_equal
center_px = img[cy, cx]
if np.array_equal(center_px, (0, 127, 255)):
print("Orange")