1

I have been working on a project "Car number plate detector" which i am implementing using opencv. After updating my Python Version and opencv, I am getting an error in finding contour as in:

imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)   # find all contours
ValueError: not enough values to unpack (expected 3, got 2)

This is the code that i used:

def findPossibleCharsInScene(imgThresh):
listOfPossibleChars = []                # this will be the return value

intCountOfPossibleChars = 0

imgThreshCopy = imgThresh.copy()

imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)   # find all contours

height, width = imgThresh.shape
imgContours = np.zeros((height, width, 3), np.uint8)

for i in range(0, len(contours)):                       # for each contour

    if Main.showSteps == True: # show steps ###################################################
        cv2.drawContours(imgContours, contours, i, Main.SCALAR_WHITE)
    # end if # show steps #####################################################################

    possibleChar = PossibleChar.PossibleChar(contours[i])

    if DetectChars.checkIfPossibleChar(possibleChar):                   # if contour is a possible char, note this does not compare to other chars (yet) . . .
        intCountOfPossibleChars = intCountOfPossibleChars + 1           # increment count of possible chars
        listOfPossibleChars.append(possibleChar)                        # and add to list of possible chars
    # end if
# end for

if Main.showSteps == True: # show steps #######################################################
    print("\nstep 2 - len(contours) = " + str(len(contours)))  # 2362 with MCLRNF1 image
    print("step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars))  # 131 with MCLRNF1 image
    cv2.imshow("2a", imgContours)
# end if # show steps #########################################################################

return listOfPossibleChars
# end function

What changes should i do to correct it?

Anish Arya
  • 518
  • 1
  • 7
  • 24
  • Possible duplicate of [too many values to unpack calling cv2.findContours](https://stackoverflow.com/questions/43960257/too-many-values-to-unpack-calling-cv2-findcontours) – Dan Mašek Feb 25 '19 at 11:25

1 Answers1

2

I think cv2.findContours() function returns only 2 values. You should change the code to the following

contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
Vishnu Murali
  • 240
  • 1
  • 2
  • 9
  • 3
    Just to add to your answer, it changed from 2 return values(version 2.4.x at least) to 3 (version 3.4 and 3.5 that I can confirm) and now it is again 2 (version 4.x). That is why it is so confusing, and a lot of tutorials have errors depending on the version of OpenCV used – api55 Feb 25 '19 at 07:56
  • that is exactly what i faced. – Anish Arya Feb 25 '19 at 10:50