OpenCV python: ValueError: too many values to unpack

前端 未结 7 1030
借酒劲吻你
借酒劲吻你 2020-11-30 01:39

I\'m writing an opencv program and I found a script on another stackoverflow question: Computer Vision: Masking a human hand

When I run the scripted answer, I get th

相关标签:
7条回答
  • 2020-11-30 01:58

    No big deal, just you might be using open-cv 3.something, which returns 3 values at the point of error and you must be catching only 2, just add any random variable before the contours variable -

    _,contours,_ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    0 讨论(0)
  • 2020-11-30 02:01

    I got the answer from the OpenCV Stack Exchange site. Answer

    THE ANSWER:

    I bet you are using the current OpenCV's master branch: here the return statements have changed, see http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours.

    Thus, change the corresponding line to read:

    _, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    

    Or: since the current trunk is still not stable and you probably will run in some more problems, you may want to use OpenCV's current stable version 2.4.9.

    0 讨论(0)
  • 2020-11-30 02:02

    python is right.
    you cannot unpack 3 values from the turple and place them in a turple of two contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    use

    img, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    0 讨论(0)
  • 2020-11-30 02:12

    This works in all cv2 versions:

    contours, hierarchy = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

    Explanation: By using [-2:], we are basically taking the last two values from the tuple returned by cv2.findContours. Since in some versions, it returns (image, contours, hierarchy) and in other versions it returns (contours, hierarchy), contours, hierarchy are always the last two values.

    0 讨论(0)
  • 2020-11-30 02:17

    I'm using python3.x and opencv 4.1.0 i was getting error in the following code :

    cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    
    ERROR : too many values to Unpack
    

    then i came to know that above code is used in python2.x SO i just replaced above code with below one(IN python3.x) by adding one more '_' in the left most side have a look

    _,cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    
    0 讨论(0)
  • 2020-11-30 02:20

    You have to change this line;

    image, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    0 讨论(0)
提交回复
热议问题