首页 > 解决方案 > 使用skimage时如何选择正确的椭圆

问题描述

我正在尝试从图像中检测椭圆。我编写的程序只要在受限区域内就可以正常工作(图 1)。但是一旦我想分析更大的图片(图 2),程序就不会选择与检测到的边缘有很多重叠的椭圆。如何选择正确的椭圆?

另外,如果您知道如何加快 hough_ellipse 函数的速度:tipp 会很棒。

我没有很多练习,所以这段代码可能不是很好。

非常感谢你提前

import matplotlib.pyplot as plt
from skimage import color, img_as_ubyte, filters
from skimage.feature import canny
from skimage.io import imread
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter


def show_image(img):
    """
    Function to show steps in image processing for testing purposes
    :param img: the Image to show
    :return: none
    """
    plt.imshow(img)
    plt.show()


def image_manipulation(image):
    """
    This function contains all image manipulating functions necessary
    :param image: contains the raw RGB image
    :return: contains the manipulated image (just the edges contained by the image)
    """
    image = color.rgb2gray(image)
    image = filters.difference_of_gaussians(image, 10, 30)
    image = canny(image, sigma=5.0,
                  low_threshold=0.03, high_threshold=0.06)
    return image


def choose_ellipse(all_ellipses):
    """
    This function chooses the ellipse with the most overlap with the edges
    :param all_ellipses:
    :return: returns just the data for the best ellipse
    """
    all_ellipses.sort(order='accumulator')
    # Estimated parameters for the ellipse
    best = list(all_ellipses[-1])
    return best


def draw_ellipse(best, edges):
    """
    This function draws the resulting best ellipse for debugging purposes
    :param best: contains the best ellipse found
    :param edges: contains an image of the edges detected in the image
    :return: none
    """
    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="all", sharey="all")

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

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

    plt.show()


def fit_ellipses(edges):
    """
    Fits ellipses to the edges in the picture
    :param edges: picture of the edges contained in the original picture
    :return: an array of all ellipses found
    """
    resulting = hough_ellipse(edges, accuracy=10, threshold=30,
                              min_size=80, max_size=110)
    return resulting


if __name__ == "__main__":
    print("Start")
    image_rgb = imread('cxgep9os0z411_Zuschnitt2.png')
    print("Image loaded")
    cannyImage = image_manipulation(image_rgb)
    print("edges detected")
    result = fit_ellipses(cannyImage)
    print("ellipses found")
    bestEllipse = choose_ellipse(result)
    print("Best ellipse chosen")
    draw_ellipse(bestEllipse, cannyImage)
    print("closing")

标签: pythonimage-processingscikit-imagehough-transform

解决方案


推荐阅读