[Python] opencv cv2.drawcontours contour argument: contour vs [contour]

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

  1. Stack

    Stack Membro Participativo

    my input :

    [​IMG]

    my code :

    # import the necessary packages
    import numpy as np
    import argparse
    import imutils
    import cv2


    print("Your OpenCV version: {}".format(cv2.__version__))
    print("Are you using OpenCV 2.X? {}".format(imutils.is_cv2()))
    print("Are you using OpenCV 3.X? {}".format(imutils.is_cv3()))
    print("Are you using OpenCV 4.X? {}".format(imutils.is_cv4()))


    # load the image
    image_orig = cv2.imread('box1.jpg')
    height_orig, width_orig = image_orig.shape[:2]

    # cv2.imshow('image_orig' , image_orig)
    # cv2.waitKey(0)
    cv2.imwrite('image_orig.png', image_orig)


    # copy of original image
    image_to_process = image_orig.copy()

    image_gray = cv2.cvtColor(image_to_process, cv2.COLOR_BGR2GRAY)
    image_gray = cv2.GaussianBlur(image_gray, (5, 5), 0)


    # cv2.imshow('image_gray' , image_gray)
    # cv2.waitKey(0)
    cv2.imwrite('image_gray.png', image_gray)

    # perform edge detection, then perform a dilation + erosion to close gaps in between object edges
    image_edged = cv2.Canny(image_gray, 50, 100)
    image_edged = cv2.dilate(image_edged, None, iterations=1)
    image_edged = cv2.erode(image_edged, None, iterations=1)

    # cv2.imshow('image_edged' , image_edged)
    # cv2.waitKey(0)
    cv2.imwrite('image_edged.png', image_edged)


    # find contours in the edge map
    cnts = cv2.findContours(image_edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    cnts = cnts[1] if imutils.is_cv2() else cnts[0] #### #### using CV4

    print(len(cnts))



    # loop over the contours individually

    # prints contours in red color thickness = 1
    image_contours = image_to_process.copy()
    for c in cnts:

    cv2.drawContours(image_contours, c , -1 ,(0,0,255),1)

    cv2.imshow('1', image_contours)
    cv2.waitKey(0)



    # prints contours in red color thickness = 10
    image_contours = image_to_process.copy()
    for c in cnts:

    cv2.drawContours(image_contours, c , -1 ,(0,0,255),10)

    cv2.imshow('10', image_contours)
    cv2.waitKey(0)

    # prints contours in red color thickness = cv2.FILLED
    image_contours = image_to_process.copy()
    for c in cnts:

    cv2.drawContours(image_contours, c , -1 ,(0,0,255), cv2.FILLED)

    cv2.imshow('filled', image_contours)
    cv2.waitKey(0)

    # prints contours in red color thickness = cv2.FILLED
    image_contours = image_to_process.copy()
    for c in cnts:

    cv2.drawContours(image_contours, [c] , -1 ,(0,0,255), cv2.FILLED)

    cv2.imshow('filled_[]', image_contours)
    cv2.waitKey(0)


    output:

    [​IMG]

    Any particular reason:

    cv2.drawContours(image_contours, [c] , -1 ,(0,0,255), cv2.FILLED)

    works while

    cv2.drawContours(image_contours, c , -1 ,(0,0,255), cv2.FILLED)

    doesn't , and why

    cv2.drawContours(image_contours, c , -1 ,(0,0,255), 10) do work ?

    Which one is the right way to code it ? Am I missing anything ?

    Continue reading...

Compartilhe esta Página