如何使用 Scikit-Image 库从 Python 中的 RGB 图像中提取绿色通道?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29718877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract green channel from RGB image in Python using Scikit-Image library?
提问by exAres
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).
我skimage
对 Python 中用于图像处理的scikit-image ( ) 库非常陌生(几分钟前开始!)。我曾经imread
在numpy.ndarray
. 该数组是 3 维的,其中第三维的大小为 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!
我得到的图像看起来并不只有绿色!
采纳答案by rayryeng
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
变量设置执行上面的操作就可以了。