首页 > 解决方案 > OpenCV python findContours 要求 cv.CV_8UC1 但这就是我要传递的

问题描述

出于某种原因,即使我传递了一个 8 位图像。谁能告诉我这里有什么问题?

mask = cv.inRange(im, (115,180,225), (165,255,255))
mask_rgb = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
t_im = cv.cvtColor(im & mask_rgb, cv.COLOR_BGR2GRAY)
print(np.shape(t_im), t_im.dtype)
contours, heirarchy = cv.findContours(t_im, cv.RETR_FLOODFILL, cv.CHAIN_APPROX_SIMPLE)
t_im = cv.cvtColor(t_im, cv.COLOR_GRAY2BGR)
targets = list()
for i, c in enumerate(contours):
    if 4200 >= cv.contourArea(c) >= 400:
        #t_im = cv.drawContours(t_im, c, -1, (0,0,255), 2)
        x,y,w,h = cv.boundingRect(c)
        cv.rectangle(t_im, (x,y), (x+w, y+h), (0,255,0),2)
        targets.append(dict({
            'x':x,
            'y':y,
            'w':w,
            'h':h
            }))

这个的输出是

(450, 800) uint8
    contours, heirarchy = cv.findContours(t_im, cv.RETR_FLOODFILL, cv.CHAIN_APPROX_SIMPLE)
    cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\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'

谢谢,麻烦您了

标签: pythonopencv

解决方案


请参阅第一条评论以获取答案。要点是 RETR_FLOODFILL 需要 32 位有符号整数


推荐阅读