opencv python中的椭圆检测

时间:2017-02-13 14:08:14

标签: python python-2.7 opencv scikit-image

我的形象在这里:

my photo is here.

我正在寻找一种更好的解决方案或算法来检测此照片中的椭圆部分(碟形)并将其掩盖在Opencv中的另一张照片中。 你能给我一些建议或解决方案吗? 我的代码是:

 circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.2, 1, param1=128, minRadius=200, maxRadius=600)
    # draw detected circles on image
    circles = circles.tolist()
    for cir in circles:
        for x, y, r in cir:
            x, y, r = int(x), int(y), int(r)
            cv2.circle(img, (x, y), r, (0, 255, 0), 4)

    # show the output image
    cv2.imshow("output", cv2.resize(img, (500, 500)))

2 个答案:

答案 0 :(得分:3)

Xie, Yonghong, and Qiang Ji制作的滑雪图片有另一种选择,并以...发布。

  

“一种新的有效椭圆检测方法。”模式识别,2002。   诉讼。第16届国际会议。卷。 2. IEEE,2002。

他们的Ellipse检测代码相对较慢,示例大约需要70秒;与声称的网站相比,#28; 28秒"。

如果你有conda或pip:" name"安装scikit-image并试一试......

他们的代码可以在here找到,也可以在下面复制/粘贴:

import matplotlib.pyplot as plt

from skimage import data, color, img_as_ubyte
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

# Load picture, convert to grayscale and detect edges
image_rgb = data.coffee()[0:220, 160:420]
image_gray = color.rgb2gray(image_rgb)
edges = canny(image_gray, sigma=2.0,
              low_threshold=0.55, high_threshold=0.8)

# Perform a Hough Transform
# The accuracy corresponds to the bin size of a major axis.
# The value is chosen in order to get a single high accumulator.
# The threshold eliminates low accumulators
result = hough_ellipse(edges, accuracy=20, threshold=250,
                       min_size=100, max_size=120)
result.sort(order='accumulator')

# Estimated parameters for the ellipse
best = list(result[-1])
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]

# Draw the ellipse on the original image
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation)
image_rgb[cy, cx] = (0, 0, 255)
# Draw the edge (white) and the resulting ellipse (red)
edges = color.gray2rgb(img_as_ubyte(edges))
edges[cy, cx] = (250, 0, 0)

fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4), sharex=True,
                                sharey=True,
                                subplot_kw={'adjustable':'box-forced'})

ax1.set_title('Original picture')
ax1.imshow(image_rgb)

ax2.set_title('Edge (white) and result (red)')
ax2.imshow(edges)

plt.show()

答案 1 :(得分:2)

方法1:

正如Miki所建议的,我能够使用轮廓属性检测给定图像中的椭圆(在此我使用了area属性)。

CODE:

#--- First obtain the threshold using the greyscale image ---
ret,th = cv2.threshold(gray,127,255, 0)

#--- Find all the contours in the binary image ---
_, contours,hierarchy = cv2.findContours(th,2,1)
cnt = contours
big_contour = []
max = 0
for i in cnt:
   area = cv2.contourArea(i) #--- find the contour having biggest area ---
    if(area > max):
        max = area
        big_contour = i 

final = cv2.drawContours(img, big_contour, -1, (0,255,0), 3)
cv2.imshow('final', final)

这是我获得的:

enter image description here

方法2:

在这种情况下,您也可以使用您建议的方法。 Hough检测椭圆/圆。

您必须预处理图像。我执行了自适应阈值并获得了这个:

enter image description here

现在您可以对此图像执行霍夫圆检测。

希望它不是满口!! :d