问题描述
有什么办法可以使用 cv2 找到每个圆的半径?
这是我的代码:
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),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,2)
cv2.putText(img,"R: ",(center[0]-45,center[1]+40),cv2.FONT_HERShey_SIMPLEX,0.7,(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)