[Python] Canny Edge detector results in no contour found

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to find the edges on an image with an obvious rectangular patch pasted on top of it with Canny edge detector.

    [​IMG]

    Relevant pre-processing before applying Canny Edge detector includes scaling the lower and upper limit of the edge detector followed by morphological operator.

    v = np.median(gray)
    sigma = 0.33
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    bfilter = cv2.bilateralFilter(gray, 11, 17, 17)
    edge = cv2.Canny(gray, lower, upper)
    kernel = np.ones((3, 3), np.uint8)
    edge = cv2.morphologyEx(edge, cv2.MORPH_CLOSE, kernel)


    Which results in the following image:

    [​IMG]

    Now these edges become a problem when I try to find the rectangular patch. No contour seems to be detected when I apply polygonal approximation to find the original patch. Relevant code shown below.

    keypoints = cv2.findContours(edge.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(keypoints)
    contours = sorted(contours, key = cv2.contourArea, reverse = True)
    locations = []
    for contour in contours:
    approx = cv2.approxPolyDP(contour, 10, False)
    if len(approx) == 4:
    print('found')
    locations.append(approx)


    Why does it result in no contour being found? I suspect that it may relate to the disconnected Canny edges. If so, what process should I include so that I will be able to obtain the binary mask of the square patch?

    Continue reading...

Compartilhe esta Página