compatibility issue with contourArea in openCV 3

前端 未结 4 1054
既然无缘
既然无缘 2020-12-17 08:52

I am trying to do a simple area calculation of contours I get from findContours. My openCv version is 3.1.0

My code is:

cc = cv2.findContours(im_bw.c         


        
4条回答
  •  别那么骄傲
    2020-12-17 09:41

    Depending on the OpenCV version, cv2.findContours() has varying return signatures.

    In OpenCV 3.4.X, cv2.findContours() returns 3 items

    image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
    

    In OpenCV 2.X and 4.1.X, cv2.findContours() returns 2 items

    contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
    

    You can easily obtain the contours regardless of the version like this:

    cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    

提交回复
热议问题