[英]How the get the radius of a circle using cv2?
Is there any way I can find the radius of every single circle using cv2 ?有什么办法可以使用cv2找到每个圆的半径?
Here's my code:这是我的代码:
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()
I can't find any way to do it, I hope you can help me, Thank you.我找不到任何方法来做到这一点,我希望你能帮助我,谢谢。
You just need to draw a line between the center and the boundary point (before drawing the circle).您只需要在中心和边界点之间画一条线(在画圆之前)。 You can use opencv line drawing function:
您可以使用 opencv 画线功能:
cv2.arrowedLine(img, (x1, y1), (x2, y2), black, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.