히비스서커스의 블로그

[Opencv] error: OpenCV(4.5.4) /tmp/pip-req-build-sokf1_ny/opencv/modules/imgproc/src/contours.cpp:195: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise s.. 본문

Programming/Python

[Opencv] error: OpenCV(4.5.4) /tmp/pip-req-build-sokf1_ny/opencv/modules/imgproc/src/contours.cpp:195: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise s..

HibisCircus 2022. 1. 3. 17:31
728x90

출처 : https://medium.com/analytics-vidhya/images-manipulations-using-opencv-numpy-and-python-2e5538d35614

 

상황

np.where 후 cv2.findContours을 해주었더니 에러가 발생하였다.

 

대략적인 코드

import numpy as np
import cv2

mask = np.zeros((300,300))
pts1 = [np.array([(100, 200), (100, 200), (200, 200), (200, 100)], dtype=np.int32)]
cv2.drawContours(mask, pts1, -1, 2.5, 10)

cv2.findContours(mask, ,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

 

에러메시지

error: OpenCV(4.5.4) /tmp/pip-req-build-sokf1_ny/opencv/modules/imgproc/src/contours.cpp:195: 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'

 

원인

np.where 후 데이터 타입을 지정해주지 않았기 때문이다.

 

해결방법

np.where 후 astype(np.uint8)로 데이터 타입을 지정해준다.

 

수정된 코드

import numpy as np
import cv2

mask = np.zeros((300,300))
pts1 = [np.array([(100, 200), (100, 200), (200, 200), (200, 100)], dtype=np.int32)]
cv2.drawContours(mask, pts1, -1, 2.5, 10)
mask = mask.astype(np.uint8)

cv2.findContours(mask, ,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

 

잘 해결되었다!

728x90