使用Python测量图像中圆的中心到中心的距离可以通过以下步骤实现:
import cv2
import numpy as np
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(image, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(image, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
if len(circles) >= 2:
center1 = circles[0][:2]
center2 = circles[1][:2]
distance = np.linalg.norm(center1 - center2)
print("圆的中心到中心的距离:", distance)
完整代码示例:
import cv2
import numpy as np
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(image, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(image, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
if len(circles) >= 2:
center1 = circles[0][:2]
center2 = circles[1][:2]
distance = np.linalg.norm(center1 - center2)
print("圆的中心到中心的距离:", distance)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
这段代码使用OpenCV库进行图像处理和圆的检测,通过Hough变换检测图像中的圆,并计算圆的中心到中心的距离。在代码中,我们使用了灰度转换、高斯模糊和Hough圆检测等技术来提取图像中的圆。最后,通过计算两个圆的中心坐标之间的欧氏距离来得到圆的中心到中心的距离。
推荐的腾讯云相关产品:腾讯云图像处理(https://cloud.tencent.com/product/tci)
领取专属 10元无门槛券
手把手带您无忧上云