【问题标题】:Error while trying to detect color at center of circle尝试检测圆心颜色时出错
【发布时间】:2019-01-27 05:19:15
【问题描述】:

我正在尝试从HoughCircles 检测检测到的圆圈中心的颜色。我这样做的方式如下:

print("Center of the circle: ", i[0]," ", i[1])
print(ci[i[0]][i[1]][0]," blue")
print(ci[i[0]][i[1]][1]," green")
print(ci[i[0]][i[1]][2]," red")

这里ci 是opencv 图像数组,i[0]i[1] 表示圆的中心坐标,如下面代码中HoughCircles 给出的那样。

但是当我这样做时,我得到一个错误提示。

IndexError: index 1034 is out of bounds for axis 0 with size 600

我不明白这是为什么。我正在尝试检测圆心的颜色。

    import cv2
    import numpy as np
    import sys
    import math


    img = cv2.imread("images/diffc.jpeg", 0)
    ci = cv2.imread("images/diffc.jpeg")

    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)



    minDist = 150
    param1 = 120
    param2 = 37

    minRadius = 120
    maxRadius = 140


    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                                param1=param1,param2=param2,minRadius=minRadius,maxRadius=maxRadius)


    if circles is None:
            print("No circles detected!")
            sys.exit(-1)


    circles = np.uint16(np.around(circles))

    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 of the circle: ", i[0]," ", i[1])
        # STATEMENTS THAT THROW ERROR
        print(ci[i[0]][i[1]][0]," blue")
        print(ci[i[0]][i[1]][1]," green")
        print(ci[i[0]][i[1]][2]," red")

    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

图片如下: Image

【问题讨论】:

  • 请附上您输入的图片
  • @NisheetPatel 在我的问题中添加了图片:i.stack.imgur.com/q3tJj.jpg
  • @NisheetPatel 另外如何检测圆心的颜色?

标签: python opencv image-processing computer-vision hough-transform


【解决方案1】:

这里你需要知道HoughCircles方法以width x heightnumpy的形式返回圆心,找到rows x columns的图像。

所以你需要先通过columns,然后在ci中传递rows

所以要检测蓝色:ci[i[1]][i[0]][0].

你的最终代码是:

import cv2
import numpy as np
import sys
import math


img = cv2.imread("images/diffc.jpeg", 0)
ci = cv2.imread("images/diffc.jpeg")

cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)



minDist = 150
param1 = 120
param2 = 37

minRadius = 120
maxRadius = 140


circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                          param1=param1,param2=param2,minRadius=minRadius,maxRadius=maxRadius)


if circles is None:
      print("No circles detected!")
      sys.exit(-1)


circles = np.uint16(np.around(circles))

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 of the circle: ", i[0]," ", i[1])
  # STATEMENTS THAT THROW ERROR
  print(ci[i[1]][i[0]][0]," blue")
  print(ci[i[1]][i[0]][1]," green")
  print(ci[i[1]][i[0]][2]," red")

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

【讨论】:

  • 你能解释一下吗?我不明白宽度,高度和行,列之间的关系。
  • image width 显示数组中的总 columns,image height 显示总 rows 在数组中。此处,您的图像 width1279height959。但 numpy 数组将其视为 959 rows1279 columns
  • 好的。根据从每个通道接收到的颜色,假设特定圆圈的颜色分布是恒定的,我有没有办法找到哪个圆圈最亮。
  • HSL图像中转换您的图像并根据您对照明的要求检测L通道
  • 为了打印L 值,我使用hls = cv2.cvtColor(ci, cv2.COLOR_BGR2HLS) 将图像转换为HLS,然后使用hls[i[1]][i[0]][1] 放置L
猜你喜欢
  • 2020-02-22
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2018-05-10
  • 2018-04-22
  • 1970-01-01
相关资源
最近更新 更多