cv2.findContours报错

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'

cv2.findContours 出现这个错误通常是因为传入的图像类型不正确导致的。
findContours函数需要传入二值图像,也就是只有0和255两个值的图像,数据类型需要是CV_8UC1。
你可以先确认下你传入的图像到底是什么类型的,可以用:
python
print(img.dtype)
如果不是CV_8UC1类型,可以先用cv2.threshold或其他二值化方法先转成二值图像,然后再传入findContours函数。
例如:
python
import cv2

img = cv2.imread('image.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
另外还需要确认下是不是OpenCV的版本问题,老版本的OpenCV只支持CV_8UC1类型的图像。