如何使用OpenCV在几乎圆形的孔中找到不同弧度的不同中心? [英] How to find different centers of various arcs in a almost circular hole using OpenCV?

查看:152
本文介绍了如何使用OpenCV在几乎圆形的孔中找到不同弧度的不同中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个洞的照片.我目前假设孔不是一个完美的圆,因此我需要找到作品尺寸的变化.现在,我正在考虑采用三个30度弧线,并求出这些弧线的中心与圆心的距离(我将使用霍夫圆找到该圆),并取这些值的平均值.这是我研究需要的东西.我附上我钻过的一个孔的样本照片.任何帮助都会有所帮助.

I have a photo of a hole. I am currently assuming that the hole is not a perfect circle, and hence I need to find the change in dimension of the work. Now I was thinking of taking three 30 degrees arcs and find the distance from the centres of those arcs and the centre of the circle (which I will find using Hough circles) and take the mean of those values. Which is what I need for my research. I am attaching a sample photo of one of the holes that I have drilled. Any help will be helpful.

推荐答案

一个想法是先确定阈值,然后找到轮廓并使用最大轮廓区域进行过滤.在这里,我们使用 cv2.minEnclosingCircle() 查找中心(x,y)点和半径.这是用绿色突出显示的最大轮廓

An idea is to threshold then find contours and filter using the largest contour area. From here we use cv2.minEnclosingCircle() to find the center (x,y) point and the radius. Here's the largest contour highlighted in green

现在,我们只需找到轮廓周围的最小包围圆即可确定中心点.这是结果

Now we simply find the minimum enclosing circle around the contour to determine the center point. Here's the result

(x,y)坐标

176.53846740722656 174.33653259277344

代码

import cv2
import numpy as np

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

for c in cnts:
    (x,y), radius = cv2.minEnclosingCircle(c)
    cv2.circle(image,(int(x),int(y)),int(radius),(35,255,12),3)
    cv2.circle(image,(int(x),int(y)),1,(35,255,12),2)
    print(x,y)
    break

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

这篇关于如何使用OpenCV在几乎圆形的孔中找到不同弧度的不同中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆