-1

So this is my code:

import cv2
import numpy as np
import imutils
import argparse

cap = cv2.VideoCapture(0)

while True :
    _,frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_blue = np.array([150,104,90])
    upper_blue = np.array ([176,161,157])
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    

    cv2.imshow ("frame", frame)
    cv2.imshow("mask", mask)

    _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(frame, contours)


    key = cv2.waitKey(1)
    if key ==27 :
        break

cap.release()
cv2.destroyAllWindows()

After running the above code, this error was shown:

Traceback (most recent call last): File "D:/1Kuliah/Kegiatan Kuliah/Roboboat/ROBONATION JADI!!/kkctbn/coba.py", line 20, in _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

ValueError: not enough values to unpack (expected 3, got 2)

I think the problem is on the value of contours but I can't fix it, please help me

Community
  • 1
  • 1
  • 2
    You're asking for `_, contours, _ = ` which means it's expecting 3 - hence the error... you probably want to adjust to be either `_, contours = ` or `contours, _` = depending which of the *two* arguments it does return is the contours and which one you wish to by naming convention want to ignore... From a quick search... it appears the actual data you want to treat as `contours` is the first returned, so maybe: `contours, _ = ` is what you're after – Jon Clements Mar 13 '20 at 15:15
  • 1
    from the [docs](https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours), `findContours` returns a tuple of `contours, hierarchy` so you probably simply want `contours, _ = ...` – Tomerikoo Mar 13 '20 at 15:17
  • so after i check my opencv version it show opencv 4. and only return 2 values. so what should i do to use only 2 values or extract to 2 values? – Taufiq Dimas Mar 13 '20 at 15:45

1 Answers1

-1

You could just modify the line calling cv2.findContours:

contours, _ = cv2.findContours( # Just extract return values 
                                # into two variables
    mask, 
    cv2.RETR_TREE,         
    cv2.CHAIN_APPROX_NONE
)
cv2.drawContours(frame, contours)
shayan rok rok
  • 494
  • 3
  • 12