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

社会主义新天地 提交于 2019-12-13 08:06:58

问题


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:


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.



来源:https://stackoverflow.com/questions/38444243/how-can-i-check-if-a-pixel-is-green-in-a-rgb-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!