----> 2 contours=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
error: OpenCV(4.5.2) /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/pip-req-build-6ogwzase/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
错误原因:
cv2.findContours处理的应该为单通道的图片,当前binary图片为3通道
binary.shape
(1039, 585, 3)
解决方案:
切片形成单通道
# 读取蓝色通道,
b = image[:, :, 0]
# 读取绿色通道,最后一个值为 1
g = image[:, :, 1]
# 读取红色通道,最后一个值为 2
r = image[:, :, 2]
参考: