[Python] Finding contours in noisy images

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I try to find the inner boundaries within this 8-bit grayscale image:

    img = cv2.imread("C:/users/XXX/test.png", -1)


    [​IMG]

    I've been playing around with various morphological operations but I can't seem to find any that work well. The most promising approach so far:

    After (simple) binarization I receive the following:

    img = (img > 0) * 1
    img = img.astype('uint8')
    plt.imshow(img, cmap = "gray", vmin = 0, vmax = 1)
    plt.show()
    plt.close()


    [​IMG]

    Or, using mild blurring in advance:

    img = cv2.medianBlur(img, 3)
    img = (img > 0) * 1
    img = img.astype('uint8')
    plt.imshow(img, cmap = "gray", vmin = 0, vmax = 1)
    plt.show()
    plt.close()


    [​IMG]

    Applying closing with an asymmetric kernel I then tried to further enhance the more-or-less horizontal lines:

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,3))
    imgClose = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    plt.imshow(imgClose, cmap = "gray", vmin = 0, vmax = 1)
    plt.show()
    plt.close()


    [​IMG]

    What I want to achieve (red line drawn by hand):

    [​IMG]

    Any idea how to get there?

    Continue reading...

Compartilhe esta Página