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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With