如何使用 cv2 获取圆的半径?

How the get the radius of a circle using cv2?

有什么方法可以使用 cv2 找到每个圆的 radius

这是我的代码:

import cv2
import numpy as np

class Image:
    def __init__(self, name, path):
        self.name = name
        self.path = path
    def loadnshow(self):
        img = cv2.imread(self.path)
        img = cv2.resize(img, (500, 500))
        #w, h = img.shape[:2]
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        bordes = cv2.Canny(gray, 100, 100)

        contours, _ = cv2.findContours(bordes, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, (255, 0, 0), 2)

        for i in contours:
            x, y, w, h = cv2.boundingRect(i)
            center = ((x+x+w)//2, (y+y+h)//2)
            cv2.circle(img, (center), 2, (255, 0, 0), 2)
            cv2.putText(img, "R: ", (center[0]-45, center[1]+40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 1)

        cv2.imshow(self.name+f" [{str(len(contours))} figuras encontradas]", img)
        print(f"{str(len(contours))} Figuras encontradas en {self.name}")



circulos = Image("Circulos", "img/circulos.jpg")
circulos.loadnshow()
#circulos2 = Image("Circulos 2", "img/circulos2.png")
#circulos2.loadnshow()
#pelotas = Image("Pelotas", "img/pelotas2.jpg")
#pelotas.loadnshow()

cv2.waitKey(0)
cv2.destroyAllWindows()

还有 Result

我找不到任何方法,希望你能帮助我,谢谢。

您只需在中心点和边界点之间画一条线(在画圆之前)。可以使用opencv画线功能:

cv2.arrowedLine(img, (x1, y1), (x2, y2), black, 1)