I am trying to implement contours using the following code..
im = cv2.imread(\'C:\\Users\\Prashant\\Desktop\\T.jpg\')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2
It now returns three values:
findContours(image, mode, method[, contours[, hierarchy[, offset]]])
return image, contours, hierarchy
So use
contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
In OpenCV 2, findContours returns just two values, contours
and hierarchy
. The error occurs when python tries to assign those two values to the three names given on left in this statement:
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
As of the year 2019, we have three versions of OpenCV (OpenCV2, OpenCV3, and OpenCV4).
OpenCV4 and OpenCV2 have similar behavoiur (of returning two values from cv2.findContours
). Whereas OpenCV3 returns three values.
if cv2.getVersionMajor() in [2, 4]:
# OpenCV 2, OpenCV 4 case
contour, hier = cv2.findContours(
thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
else:
# OpenCV 3 case
image, contour, hier = cv2.findContours(
thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
Python version 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
NumPy version: 1.16.1
argparse version: 1.1
CV2 version: 4.0.0
Traceback (most recent call last):
File "omr.py", line 254, in <module>
main()
File "omr.py", line 237, in main
answers, im = get_answers(args.input)
File "omr.py", line 188, in get_answers
contours = get_contours(im)
File "omr.py", line 26, in get_contours
im2, contours, hierarchy =cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack
This is resolved by removing 'im2 ,' from line 26.. as in OpenCv version 3.0 or higher, the function 'findContours' returns only 2 values.. so the statement should be
contours, hierarchy =cv2.findContours(image_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
and also upgrade your OpenCv version
findContours returns just three values image, contours and hierarchy in opencv3
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)