简体   繁体   English

OpenCV和Python:在HoughCircles中打印红点像素值

[英]OpenCV & Python : Printing the Red dot pixel value in HoughCircles

I am able to identify a circular object. 我能够识别圆形物体。 How could i print the HoughCircle center point in pixels which is red in color. 我如何以红色打印以像素为单位的HoughCircle中心点。 If there are two circles, how can i access by pixels ?? 如果有两个圆圈,如何按像素访问?

My code is 我的代码是

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()

Center pixel coordinates of your circles are stored inside the circles variable returned by cv2.HoughLines . 你的圈子的中心像素坐标存储在内部circles通过返回的变量cv2.HoughLines This data structure provides the (X, Y) coordinates of your circles and their radius. 该数据结构提供了圆的(X,Y)坐标及其半径。

Looking at your code, each one of the i stores a circle. 查看您的代码,每个i存储一个圆圈。

If what you want is just show the circle center coordinates, you can print them on the standard output or show them in the image using the values inside array 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM