How can I check if a pixel is green in a RGB image

2019-09-19 11:52发布

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?

1条回答
狗以群分
2楼-- · 2019-09-19 12:45

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.

查看更多
登录 后发表回答