如何使用Scikit-Image库从Python中的RGB图像中提取绿色通道?

时间:2022-02-06 21:21:16

I am extremely new to scikit-image (skimage) library in Python for image processing (started few minutes ago!). I have used imread to read an image file in a numpy.ndarray. The array is 3 dimensional where the size of the third dimension is 3 (namely one for each of Red, Green and Blue components of an image).

我对Python中的scikit-image (skimage)库非常陌生,用于图像处理(几分钟前就开始了!)我使用imread读取了一个numpi .ndarray中的图像文件。该数组是三维的,其中第三维的大小为3(即一个图像的红、绿、蓝分量各一个)。

rgb_image = imread("input_rgb_image.jpg")
rgb_image.shape # gives (1411L, 1411L, 3L)

I tried to extract green channel as:

我尝试提取绿色通道为:

green_image = rgb_image[:,:,1]

But when I write this image matrix to an output file as:

但是当我将这个图像矩阵写到输出文件时

imsave("green_output_image.jpg",green_image)

I get an image which doesn't really look ONLY green!

我得到的图像不是真的看起来只有绿色!

1 个解决方案

#1


16  

What you are extracting is just a single channel and this shows you how much green colour each pixel has. This will ultimately be visualized as a grayscale image where darker pixels denote that there isn't much "greenness" at those points and lighter pixels denote that there is a high amount of "greenness" at those points.

你提取的只是一个单独的通道,这显示了每个像素有多少绿色。这最终将被可视化为一幅灰度图像,其中深色像素表示在这些点上没有多少“绿色”,而浅色像素表示在这些点上有大量的“绿色”。

If I'm interpreting what you're saying properly, you wish to visualize the "green" of each colour. In that case, set both the red and blue channels to zero and leave the green channel intact.

如果我正确地解释你所说的话,你希望把每种颜色的绿色想象出来。在这种情况下,将红色和蓝色通道都设置为零,并保持绿色通道的完整性。

So:

所以:

green_image = rgb_image.copy() # Make a copy
green_image[:,:,0] = 0
green_image[:,:,2] = 0

Note that I've made a copy of your original image and changed the channels instead of modifying the original one in case you need it. However, if you just want to extract the green channel and visualize this as a grayscale image as I've mentioned above, then doing what you did above with the setting of your green_image variable is just fine.

注意,我已经复制了您的原始图像,并更改了通道,而不是修改原始图像,以防您需要它。但是,如果您只是想提取绿色通道并将其可视化为我前面提到的灰度图像,那么您就可以通过设置green_image变量来完成上面所做的工作。

#1


16  

What you are extracting is just a single channel and this shows you how much green colour each pixel has. This will ultimately be visualized as a grayscale image where darker pixels denote that there isn't much "greenness" at those points and lighter pixels denote that there is a high amount of "greenness" at those points.

你提取的只是一个单独的通道,这显示了每个像素有多少绿色。这最终将被可视化为一幅灰度图像,其中深色像素表示在这些点上没有多少“绿色”,而浅色像素表示在这些点上有大量的“绿色”。

If I'm interpreting what you're saying properly, you wish to visualize the "green" of each colour. In that case, set both the red and blue channels to zero and leave the green channel intact.

如果我正确地解释你所说的话,你希望把每种颜色的绿色想象出来。在这种情况下,将红色和蓝色通道都设置为零,并保持绿色通道的完整性。

So:

所以:

green_image = rgb_image.copy() # Make a copy
green_image[:,:,0] = 0
green_image[:,:,2] = 0

Note that I've made a copy of your original image and changed the channels instead of modifying the original one in case you need it. However, if you just want to extract the green channel and visualize this as a grayscale image as I've mentioned above, then doing what you did above with the setting of your green_image variable is just fine.

注意,我已经复制了您的原始图像,并更改了通道,而不是修改原始图像,以防您需要它。但是,如果您只是想提取绿色通道并将其可视化为我前面提到的灰度图像,那么您就可以通过设置green_image变量来完成上面所做的工作。