python - opencv python中的椭圆检测

标签 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)))

最佳答案

Xie, Yonghong, and Qiang Ji 制作的 skimage 中有一个替代品并发布为...

“A new efficient ellipse detection method.” Pattern Recognition, 2002. Proceedings. 16th International Conference on. Vol. 2. IEEE, 2002.

他们的椭圆检测代码比较慢,例子大概需要70秒;与网站声称的“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'})

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

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

plt.show()

关于python - opencv python中的椭圆检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42206042/

相关文章:

python - 如何用 Python 拆分合并的 Excel 单元格?

python - 如何在 python 中为 chrome 的 Selenium Webdriver 设置 luminati 代理?

python - 如何在 Ubuntu 20.04 中为 Python2.7 安装 pip

c++ - 显示 cv2 垫的差异

python - 函数列表相对于变量列表的微分

python - Markdown 语法突出显示调用 Python 脚本的 Bash 命令

Python 内部服务器错误

Python错误: cannot import name cross_validation

安卓 : How can I open a file using OpenCV C++ ?

opencv - 如何在 ubuntu 12.04 中安装 OpenCV