我需要重新审视我的代码,以获得圆圈颜色的中心。出于某种不为人知的原因,它为每个远非正确的中心返回接近相同的值。圈子的结果:
中心颜色: [126 126 126] 观点 x: 502 y: 440
中心颜色 [124 124 124] 观点 x: 502 y: 516
中心颜色 [133 133 133] 观点 x: 502 y: 596
中心颜色 [116 116 116] 观点 x: 504 y: 306
中心颜色 [119 119 119] 观点 x: 504 y: 366
输入图像如下所示。显然应该有非常不同的值,因为黑色圆圈的平均 RBG 应该远低于 100 范围。
下图显示代码正在正确找到圆圈和圆心(标记为绿色)只是没有找到正确的中心颜色值
代码如下:
import cv2
import numpy as np
from math import sqrt
# Open
img = cv2.imread('test1.jpg',0)
# Process
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
# Find Interest
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=1,maxRadius=20)
circles = np.uint16(np.around(circles))
# Post Process
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
print "Center Colour"
print cimg[i[0], i[1]]
print "Point"
print "x: "+str(i[0]) + " y: " + str(i[1])
cv2.imwrite('output.png',cimg)
最佳答案
根据我的理解,您应该获得内圈的颜色,因为您是在访问颜色值之前绘制它的。但由于返回的值不是 0、0、255,因此必须交换坐标。
原始打印是:
print "Center Colour (orig)", cimg[i[0], i[1]]
交换打印是:
print "Center Colour (swapped)", cimg[i[1], i[0]]
绘制后的输出:
Center Colour (orig) [126 126 126]
Center Colour (orig) [116 116 116]
...
Center Colour (swapped) [ 0 0 255]
Center Colour (swapped) [ 0 0 255]
...
如果您现在在绘制内圆之前使用交换打印,输出将如下所示:
Center Colour (swapped) [128 128 128]
Center Colour (swapped) [27 27 27]
...
这就是你要找的吗?
关于python - OpenCV像素颜色都一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745260/