简体   繁体   English

像opencv一样检测圆形

[英]Detect circle like shapes opencv

everyone i'm fairly new to OpenCV and computer vision and i'm stuck at this problem , which might seem like a fairly trivial but forgive my noobness :) 每个人我对OpenCV和计算机视觉都很陌生,而且我坚持这个问题,这看起来似乎是一件相当微不足道的事,但原谅我的无聊:)

I'm trying to detect Rebars from a cross-sectional image. 我试图从横截面图像中检测出Rebars。

原始彩色图像

i'm using this code : 我正在使用此代码:

import cv2
import cv2.cv as cv
import numpy as np

img = cv2.imread('test/t2.jpg',0)
img = cv2.equalizeHist(img)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is the result i'm getting currently, which is not good : 这是我目前得到的结果,这并不好: 结果

I'm looking for pointers on how to proceed with this problem and how to learn more about CV as i'm really interested! 我正在寻找关于如何解决这个问题以及如何了解更多关于简历的指示,因为我真的很感兴趣!

Thanks a ton! 万分感谢!

HoughCircles is not a strong enough way to detect circle in such complex image like your case. HoughCircles不是一种足够强大的方法来检测像你这样的复杂图像中的圆圈。

SO has already had some discussion about this. SO已经对此进行了一些讨论。 You could refer these post with quality accepted answers 你可以用质量接受的答案来引用这些帖子

Standard way: 标准方式:

Filled circle detection using CV2 in Python? 在Python中使用CV2填充圆检测?

What are the possible fast ways to detect circle in an image? 检测图像中圆圈的快速方法有哪些?

Noise image: 噪音图像:

https://dsp.stackexchange.com/questions/5930/find-circle-in-noisy-data https://dsp.stackexchange.com/questions/5930/find-circle-in-noisy-data

Another method: 另一种方法:

Gradient Pair Vectors 梯度对向量

Learning Automata 学习自动机

Those results can be slightly improved with setting the parameters better on this line: 通过在此行上更好地设置参数,可以略微改善这些结果:

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,10,param1=50,param2=30,minRadius=0,maxRadius=25)

For example, you can reduce the maxRadius slightly and increase the sensitivity. 例如,您可以稍微降低maxRadius并增加灵敏度。

In my experience, however, you won't get a good result on an image like this. 但是,根据我的经验,你不会在这样的图像上得到好的结果。 It is very complex, the circles are irregular and at different angles. 它非常复杂,圆形不规则,角度不同。 If your goal is to practice, then sure, play with the parameters and try different methods to improve it. 如果您的目标是练习,那么请确保使用参数并尝试不同的方法来改进它。 I don't see much practical use though. 虽然我没有看到太多实际用途。

You can detect features here, using the module trackpy. 您可以使用模块trackpy在此处检测功能。 You need to vary feature sizes with odd numbers and see which one matches best. 您需要使用奇数来改变特征尺寸,并查看哪一个最匹配。 You may also need to do some pre-processing like, converting image to grayscale. 您可能还需要进行一些预处理,例如将图像转换为灰度。

import trackpy as tp
import numpy as np
import pandas as pd
import pims
import matplotlib.pyplot as plt

#%% importing the data
frames=pims.ImageSequence('F:/TrapHysteresis/processing/Positions/*.TIF')

#%% tracking circles and center positions
featuresize=71
f1=tp.locate(frames[0],featuresize)

plt.figure()
tp.annotate(f1,frames[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM