Original program
import cv2
import numpy as np
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Read the image
img = cv2.imread('contours.png')
cv_show('contours', img)
# Grayscale and binarization
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
cv_show('thresh', thresh)
# Find the outline
cnt = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
# Draw the outline
draw = img.copy()
res = cv2.drawContours(draw, cnt, -1, (0, 0, 255), 2)
cv_show('res', res)
report errors
Traceback (most recent call last):
File "C:/Users/ccccj/PycharmProjects/a_opencv/opencv/opencv_notebook/ppt/ppt_season_2-8/2-7_notebook/image_operation/contours.py", line 21, in <module>
cnt = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
cv2.error: OpenCV(3.4.11) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-7_3trius\opencv\modules\imgproc\src\contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'
Reason: the SRC of findcontours() function must be a grayscale image. The original program uses img, which is changed to the following:
# Find the outline
# cnt = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
# Modify to:
cnt = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
Read More:
- [Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……
- Python+OpenCV: How to Use Background Subtraction Methods
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python
- Opencv Python realizes the paint filling function in PS, one click filling color and the possible reasons for opencv’s frequent errors
- v2.error: OpenCV(4.5.5) error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours‘
- Pychar can’t connect to Python console, but it can run. Py file, and Anaconda’s command line can run Python command
- [Solved] Pyinstaller package opencv error: ImportError: OpenCV loader: missing configuration file: [‘config.py’]. Check OpenCV installation.
- Detailed explanation of OpenCV approxpolydp() function
- [Solved] ERROR: Failed building wheel for opencv-python-headless
- [Solved] cv2.error: OpenCV(4.5.1) XXX\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 &&……
- Mac Selenium Use Error: Can not connect to the Service chromedriver
- You can run the Ansible Playbook in Python by hand
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- Python Error: SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3:
- [Solved] ValueError: only one element tensors can be converted to Python scalars
- Python Use PIP to install pyinstaller Error [Solved]
- [Solved] Python opencv imshow Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow
- Python: How to Use os.path.join()