OpenCV & Python : 在 HoughCircles 中打印红点像素值
OpenCV & Python : Printing the Red dot pixel value in HoughCircles
我能辨认出一个圆形物体。我如何以红色像素打印 HoughCircle 中心点。如果有两个圆圈,如何按像素访问??
我的密码是
import cv2
import numpy as np
img = cv2.imread('STACK.jpg',0)
img = cv2.medianBlur(img,1)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,3,100,param1=60,param2=80,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),2)
cv2.imshow('Detected Circle',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
圆的中心像素坐标存储在 cv2.HoughLines
返回的 circles
变量中。此数据结构提供圆的 (X, Y) 坐标及其半径。
查看您的代码,每个 i
存储一个圆圈。
如果您只想显示圆心坐标,您可以将它们打印在标准输出上或使用数组 i
:
中的值在图像中显示它们
for i in circles[0,:]:
x = i[0]
y = i[1]
radius = i[2]
# Draw text in image
font = cv2.FONT_HERSHEY_SIMPLEX
text = '(' + str(x) + ',' + str(y) + ')'
cv2.putText(cimg, text,(x + 5, y - 5), font, 0.5, (0, 125, 255), 2)
# Print circle position
print 'Circle with radius %.2f is at (%d, %d)' % (radius, x, y)
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),2)
我能辨认出一个圆形物体。我如何以红色像素打印 HoughCircle 中心点。如果有两个圆圈,如何按像素访问??
我的密码是
import cv2
import numpy as np
img = cv2.imread('STACK.jpg',0)
img = cv2.medianBlur(img,1)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,3,100,param1=60,param2=80,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),2)
cv2.imshow('Detected Circle',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
圆的中心像素坐标存储在 cv2.HoughLines
返回的 circles
变量中。此数据结构提供圆的 (X, Y) 坐标及其半径。
查看您的代码,每个 i
存储一个圆圈。
如果您只想显示圆心坐标,您可以将它们打印在标准输出上或使用数组 i
:
for i in circles[0,:]:
x = i[0]
y = i[1]
radius = i[2]
# Draw text in image
font = cv2.FONT_HERSHEY_SIMPLEX
text = '(' + str(x) + ',' + str(y) + ')'
cv2.putText(cimg, text,(x + 5, y - 5), font, 0.5, (0, 125, 255), 2)
# Print circle position
print 'Circle with radius %.2f is at (%d, %d)' % (radius, x, y)
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),2)