Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask Type Error in OpenCV Mean

Tags:

python

opencv

I'm trying to mask out an area of my frame so that I can get the mean of a shape. My code is as follows:

for h, cnt in enumerate(contours):
    mask = np.zeros(source_img.shape, np.uint8)
    cv2.drawContours(mask, [cnt], 0, 255, -1)
    print mask
    print mask.dtype
    mean = cv2.mean(source_img, mask=mask)

However, when running this code, I get error: (-215) mask.empty() || mask.type() == CV_8U in function mean.

The print statements included come back that it is uint8. The print of the mask itself prints out a non-empty numpy array with values of 0 and 255. Any other ideas of where I'm going wrong?

like image 331
Lark Avatar asked Jul 22 '15 13:07

Lark


1 Answers

Rookie mistake: As it turns out, the source image that I took the shape from was a colored image, which meant that source_img.shape() came out to (480, 640, 3). So despite then setting the type of the mask correctly, it was still a 3 channel image, when it needed to be two.

Fixed by doing my initialization of the mask layer as:

mask = np.zeros(source_img.shape[:2], np.uint8)
like image 50
Lark Avatar answered Oct 02 '22 19:10

Lark