简体   繁体   English

霍夫圆转换为圆形阴影

[英]Hough circle transform to circular shadow

I have an image in which I am trying to apply Hough circle transforms to the circular objects in view. 我有一个图像,我试图将Hough圆形变换应用于视图中的圆形对象。

I am having difficulty finding a circle that fits the outer shadow of the cyclinder. 我很难找到一个适合气缸外部阴影的圆圈。 What can be done to properly segment this shadow and easily fit a circle to it? 可以做些什么来正确地分割这个阴影并轻松地适应它的圆圈?

Code: 码:

img = cv2.medianBlur(im,7)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

plt.imshow(cimg)
plt.show()

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=150,minRadius=100,maxRadius=0)

circles = np.uint16(np.around(circles))

for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(255,0,0),10)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),20)

    radius = i[2]
    print 'radius', radius, 'px'

plt.imshow(cimg)
plt.show()

The shadow you are looking to segment is by far the darkest region. 你想要分割的阴影是迄今为止最黑暗的区域。 I would use a threshold to filter out all pixels that are brighter. 我会使用阈值来过滤掉所有更亮的像素。 If there is noise that remains, I would use Connected Components to find the largest "blob". 如果仍有噪音,我会使用Connected Components找到最大的“blob”。 Once the shadow is the only thing remaining and all other pixels are set to 0, I would try the minEnclosingCircle that dhanushka recommended above. 一旦阴影是剩下的唯一东西而所有其他像素都设置为0,我会尝试上面推荐的dhanushka的minEnclosingCircle。

I'm just going to write out the code and not go through it because there's a lot of functions and I would hate to assume what you know or don't know and spend a long time doing the write-up. 我只是要编写代码而不是通过它,因为它有很多功能,我不愿意假设你知道或不知道的东西并且花了很长时间来写这篇文章。 If you have any questions feel free to ask and I'll add them in the post. 如果您有任何问题可以随意提问,我会在帖子中添加它们。

You asked to fit a circle to the crescent shadows, so I've fitted circles to the shadows. 你要求在新月形阴影上加一个圆圈,所以我把圆圈装到了阴影里。 It's important to realize that in some kind of production code which would, I imagine, have to process a lot of images of this nature it would be necessary to refine the circles fitted. 重要的是要意识到,在某些生产代码中,我想,必须处理大量这种性质的图像,有必要改进所适合的圆形。 In particular any kind of structural analysis of this type is just worried about fitting the given shape to the pixels, not that the object in question is what you're looking for. 特别是这种类型的任何结构分析只是担心将给定的形状拟合到像素,而不是所讨论的对象是你正在寻找的。

I've intentionally left the wrongly-fitted circle in there. 我故意在那里留下了错误的圆圈。 I would suggest going for the convexHull, or Haar detector or shape matching depending on what exactly you're interested in. 我建议根据你感兴趣的内容选择凸包,哈尔探测器或形状匹配。

import cv2 
import numpy as np

img = cv2.imread("orig.png", cv2.IMREAD_GRAYSCALE)

ret, thresh = cv2.threshold(img, 80, 255, cv2.THRESH_BINARY_INV)

ero = cv2.erode(thresh, np.ones((5,5)))
dil = cv2.dilate(ero, np.ones((5,5)))

img, contours, hierarchy = cv2.findContours(dil, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_NONE)

#just for drawing purposes, the cimg is not really required
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for cnt in contours:
    (x, y), radius = cv2.minEnclosingCircle(cnt)
    center = (int(x), int(y))
    radius = int(radius)
    cv2.circle(cimg, center, radius, (255, 0, 0), 1)

The output image I got was 我得到的输出图像是

在此输入图像描述

Both crescents are fitted to correctly with the bottom one matching the outside of the tank and not the crescent exactly. 两个新月形都正确安装,底部与月亮相匹配,而不是新月形。 You could do a sort of hysteresis tracking and shift that circle until it's outer edge is precisely at the crescent fairly consistently though. 您可以进行一种滞后跟踪并移动该圆,直到它的外边缘正好相当一致的新月。

There's an additional circle that can be removed if you tune the parameters just right but filtering the exact circles you need is up to you. 如果您正确调整参数,可以删除一个额外的圆圈,但过滤您需要的确切圆圈取决于您。 Fe if you only want the top crescent ask for the smallest y coordinate, if all the shadows are as big as these you can ask for only circles of radii larger than some threshold etc... Fe如果你只想要顶部新月要求最小y坐标,如果所有阴影都大到这些你可以要求只有半径大于某个阈值的圆等...

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

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