cropping image non regolar mask with opencv

时间:2017-10-12 09:39:53

标签: python opencv crop

i want to crop an image (so without black holes) based on a very spread and irregular mask.

So far i managed to extract the greeness mask of an image, removing all the non green/yellowish pixels from the image.

What i'd like to do now is to evaluate a greenfactor based on this formula

b, g, r = cv2.split(img) green = np.mean(g) / (np.mean(b) + np.mean(g) + np.mean(r))

The problem is that in the obtained image i have a bunch of holes, (0,0,0) pixel values, that contribute to the evaluation.

I found a similar question here but i cannot evaluate contours of the olygon in my case, i need something like a bitwise crop, but i'm not experienced with opencv and python

I attach the code and the obtained image

 img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
 mask_green = cv2.inRange(img_hsv, np.array(lb), np.array(ub))
 img_filtered = cv2.bitwise_and(img, img, mask=mask_green)

Original Image

Filtered image

0 个答案:

没有答案