【问题标题】:OpenCV pixel colours all the sameOpenCV的像素颜色都一样
【发布时间】:2013-09-12 00:24:24
【问题描述】:

我需要重新审视获得圆心颜色的代码。由于某些不为人知的原因,它为每个离正确的中心返回接近相同的值。圈子的结果:

中心颜色: [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)

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    据我了解,您应该获得内圈的颜色,因为您是在访问颜色值之前绘制它。但由于返回的值不是 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]
    ...
    

    这就是你要找的吗?

    【讨论】:

    • 是的。非常感谢你,我知道它必须如此简单。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多