本文介绍了如何检查RGB图像中的像素是否为绿色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv和numpy来处理一些卫星图像。

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).

我的问题是:如何判断哪种值接近RGB格式的绿色?

到目前为止我所做的是:

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

此代码有效,但并不实用。我需要一种更精确的方式来决定哪些RGB值对应绿色,哪些不对应。

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.

我如何实现这一目标?

推荐答案

您可以使用InRange函数查找特定范围内的颜色,因为只有一个或几个像素值,您将无法从卫星上找到绿色。 InRange功能将帮助您找到一系列设置颜色(您应该设置绿色范围)并返回一个图像,其中绿色像素的坐标与原始图像相同。我已经通过示例和代码回答了类似的问题 (虽然它不是python,你应该理解这些方法并在OpenCV项目中轻松实现它),你应该找到你需要的所有东西。

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.

这篇关于如何检查RGB图像中的像素是否为绿色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:15