When I wrote the following code using findContours
of ʻOpenCV`,
wrong
import numpy as np
import cv2
im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #This line
not enough values to unpack (expected 3, got 2)I got angry.
## solution
In the latest version of opencv, OpenCV4, the return value of `findContours` is two,` contours` and `hierarchy`, so if you write as follows, it will work as expected.
#### **`good`**
```python
import numpy as np
import cv2
im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #This line
The above code is
http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_contours/py_contours_begin/py_contours_begin.html
Is referred to.
However, since this code is based on OpenCV3, some specifications are different from the current latest version, OpenCV4.
One of the examples was the return value of this findContours
.
To make matters worse, within the same HP http://lang.sist.chukyo-u.ac.jp/classes/OpenCV/py_tutorials/py_imgproc/py_contours/py_contours_more_functions/py_contours_more_functions.html Because, here,
In OpenCV3, unlike OpenCV2, cv2.findContours () returns 3 values instead of 2.
There is a description. This caused a lot of confusion about the version of OpenCV I was using.
For OpenCV4, please say ʻimport cv4`. I've been wondering if I'm using OpenCV2 to this day.
Recommended Posts