如何解决如何使用cv2获得圆的半径?
有什么办法可以使用 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。