尝试检测圆心颜色时出错
Error while trying to detect color at center of circle
我正在尝试从 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
这里要知道HoughCircles
的方法是returns以width x height
形式的圆心和numpy
找到带[=14=的图像].
所以你需要先通过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()
我正在尝试从 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
这里要知道HoughCircles
的方法是returns以width x height
形式的圆心和numpy
找到带[=14=的图像].
所以你需要先通过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()