1

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

Fabio
  • 782
  • 4
  • 8
  • 25
  • 1
    [XY problem here...](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Just compute the formula only on valid values, according to your mask. Either manually iterating over all pixels, or using Python indexing should be ok – Miki Oct 12 '17 at 09:43
  • 2
    Your question is answered here on [SO](https://stackoverflow.com/questions/38542548/numpy-mean-of-nonzero-values). Second approach would be as recommended by @Miki – Croolman Oct 12 '17 at 09:51
  • Thanks guys, i'll try to go for python indexing – Fabio Oct 12 '17 at 09:53

0 Answers0