首页 > 解决方案 > 有没有办法使用opencv从给定的输入图像中提取圆的精确坐标?

问题描述

我有一个任务来检测一个圆并得到它的中心,给定的输入是输入图像,这就是我得到的,输出图像我总是得到一个小错误,这对项目有很大的影响,所以有什么方法可以正确地得到中心?我在 OpenCV 中使用 HoughCircles 函数

import cv2 
import numpy as np 
import matplotlib.pyplot as plt
# Read image. 
img = cv2.imread('grid_crop_image1600357669.7175033.png', cv2.IMREAD_COLOR) 
h, w, c = img.shape
# Convert to grayscale. 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

# Blur using 3 * 3 kernel. 
gray_blurred = cv2.blur(gray, (3, 3)) 

# Apply Hough transform on the blurred image. 
detected_circles = cv2.HoughCircles(gray_blurred,  
               cv2.HOUGH_GRADIENT, 1, 20, param1 = 50, 
           param2 = 30, minRadius = 1, maxRadius = 40) 

# Draw circles that are detected. 
if detected_circles is not None: 

# Convert the circle parameters a, b and r to integers. 
detected_circles = np.uint16(np.around(detected_circles)) 

for pt in detected_circles[0, :]: 
    a, b, r = pt[0], pt[1], pt[2] 

    # Draw the circumference of the circle. 
    cv2.circle(img, (a, b), r, (0, 255, 0), 2) 

    # Draw a small circle (of radius 1) to show the center. 
    cv2.circle(img, (a, b), 1, (0, 0, 255), 1) 
cv2.line(img,(int(a),int(b)), (int(a),int(h)), (0,255,0),1)
plt.imshow(img)
plt.show()

标签: pythonopencv

解决方案


推荐阅读