python - 有没有办法使用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()
解决方案
推荐阅读
- mongodb - 集成 Solr 8.9.0 和 MongoDB 5.0 社区版
- postgresql - 尝试连接到 Cloud SQL 实例时出现 403
- python - Python for loop int 没有 .value
- r - dplyr汇总功能的R循环
- python - 如何从回复 ID(Python)中获取线程/对话?
- ios - 当观察到可能发布在“用于注册观察者的线程之外的线程上”的通知时,如何确保线程安全的 UI 工作?
- c# - 更改嵌套节点的值
- reactjs - 来自 Dockerfile 的值可在 Sources 选项卡中搜索 - 如何保护环境变量?
- python - Python JSON API 打印字典内的列表
- batch-file - windows下python虚拟环境发送命令