Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opencv: finding contoures on numpy array

I'm trying to find contours in a binary image which is a numpy array

a = np.array(np.random.rand(1024,768),dtype='float32')    
_, t2 = cv2.threshold(a,127,255,0)
im2, contours, hierarchy = cv2.findContours(t2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

When I try to run that code I get this error

OpenCV Error: 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 cvStartFindContours
like image 861
Ehab AlBadawy Avatar asked Oct 19 '22 11:10

Ehab AlBadawy


1 Answers

As the error message states - the only format supported, when the mode is not CV_RETR_FLOODFILL, is CV_8UC1 => single channel 8 bit, unsigned integer matrix. When the mode is CV_RETR_FLOODFILL, the only supported format is CV_32SC1 - 32 bit signed...

Since you are passing array of float32, it is CV_32FC1 - 32 bit, floating, which is not supported. You have to use array of integer.

like image 139
j.kaspar Avatar answered Nov 03 '22 00:11

j.kaspar