[Python] Remove contours that are almost straight with opencv

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a raster image where I am trying to remove contours that touch the image border, that are short, and that are not straight or circular. However, I have not been able to remove lines that are almost straight. I figured I could compare absolute distance between vertices to length of the contour, but was not successful.

    Code to filter contours

    for c in contours:
    # # Filter contours based on length
    if cv.arcLength(c, True) < image_length * 0.1:
    continue
    # Check if contour touches the image border
    touches_border = False
    for point in c:
    x, y = point[0]
    if x <= 0 or y <= 0 or x >= width - 1 or y >= height - 1:
    touches_border = True
    break
    if touches_border:
    continue # Skip contours that touch the border


    example image:

    [​IMG]

    Continue reading...

Compartilhe esta Página