1

I'm using opencv and numpy to process some satellite images.

I need to differentiate what is "land" from what is "green" (crops and vegetation).

My question is: How can I decide which values are close to green in the RGB format?

What I'm doing so far is:

img = cv2.imread('image1.jpg',1)
mat = np.asarray(img)
for elemento in mat: 
    for pixel in elemento:
        if pixel[1] > 200: # If the level of green is higher than 200, I change it to black
            pixel[0] = 0
            pixel[1] = 0
            pixel[2] = 0
        else: # If the level of G is lower than 200 I change it to white.
            pixel[0] = 255
            pixel[1] = 255
            pixel[2] = 255

This code works, but isn't really useful. I need a more precise manner to decide which RGB values correspond to green and which ones does not.

How can I achieve this?

dot.Py
  • 5,007
  • 5
  • 31
  • 52
Gabriel Belini
  • 760
  • 1
  • 13
  • 32
  • 3
    One slight wrinkle r=255 g=200 b=255 is a light purple. Consider using instead g>64 and g>r+b – Oscar Smith Jul 18 '16 at 19:19
  • 2
    this may help you: [color distance](https://en.wikipedia.org/wiki/Color_difference). – Arnial Jul 18 '16 at 19:34
  • Typically you would want to do a Supervised Classification which might also use infra-red bands, and other factors such as texture along with ground truth and accuracy analysis. Trying to do it with RGB alone will likely result in limited success. – Mark Setchell Jul 18 '16 at 22:00
  • 2
    changing the color to HSV can be very useful – Farshid PirahanSiah Jul 19 '16 at 01:13

1 Answers1

1

You could use InRange function to find colors in specific range, because you will not be able to find green color from satelites just with one or few values of pixels. InRange function will help you to find a range of set colors (you should set the range of green colors) and return an image with the coordinates of those green pixels ir the original image. I've answered a similar quiestion HERE with examples and code (although it is not python, you should understand the methods and easily implement it in your OpenCV project), you should find everything you need there.

Community
  • 1
  • 1
Dainius Šaltenis
  • 1,644
  • 16
  • 29