在 OpenCV (Python) 中,为什么我从灰度图像中获取 3 通道图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18870603/
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
In OpenCV (Python), why am I getting 3 channel images from a grayscale image?
提问by Steve
I am using Python (2.7) and bindings for OpenCV 2.4.6 on Ubuntu 12.04
我在 Ubuntu 12.04 上使用 Python (2.7) 和 OpenCV 2.4.6 的绑定
I load an image
我加载图像
image = cv2.imread('image.jpg')
I then check the shape of the image array
然后我检查图像数组的形状
print image.shape
I get (480, 640, 3), which I expect for a 640x480 colour image. I then convert the image to grayscale and check the shape again.
我得到 (480, 640, 3),这是我期望的 640x480 彩色图像。然后我将图像转换为灰度并再次检查形状。
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print gray_image.shape
I get (480, 640, 1), which I expect for a 640x480 grayscaleimage. I then save the image:
我得到 (480, 640, 1),这是我期望的 640x480灰度图像。然后我保存图像:
cv2.imwrite('gray.jpg', gray_image)
I am on linux, so I tried looking at the image with gThumb, which shows all the colour channels. When I bring the gray image back into OpenCV the image has three channels again. I am aware of this flag for reading images:
我在 linux 上,所以我尝试用 gThumb 查看图像,它显示了所有颜色通道。当我将灰色图像带回 OpenCV 时,图像再次具有三个通道。我知道这个用于读取图像的标志:
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
But this sounds like it is going to bring in the image as a colour image and then convert it. I am porting this project to RaspberryPi so I don't want unecessary operatioms happening.
但这听起来像是要将图像作为彩色图像引入然后进行转换。我正在将这个项目移植到 RaspberryPi,所以我不希望发生不必要的操作。
EDIT: I have done some timing checks and I have discovered that loading an image using the CV_LOAD_IMAGE_GRAYSCALE flag set results in the image loading twice as fast, irrespective of the image input.
编辑:我做了一些时间检查,我发现使用 CV_LOAD_IMAGE_GRAYSCALE 标志集加载图像会导致图像加载速度提高两倍,而不管图像输入如何。
Using a 3072 x 4608 x 3 image
0.196774959564 seconds with default loading
0.0931899547577 seconds with CV_LOAD_IMAGE_GRAYSCALE
The problem seems to be that OpenCV is creating a 3 channel JPG output whether I have a grayscale image matrix or not!
问题似乎是 OpenCV 正在创建一个 3 通道 JPG 输出,无论我是否有灰度图像矩阵!
What other app can I use to make sure I am getting a single 8 bit channel JPG image out?? (Perhaps gThumb is reporting the channels incorrectly).
我可以使用什么其他应用程序来确保我得到一个单一的 8 位通道 JPG 图像??(也许 gThumb 错误地报告了频道)。
If the image is not single channel, why is OpenCV saving my grayscale image to a 3 channel image at disk write?
如果图像不是单通道,为什么 OpenCV 在磁盘写入时将我的灰度图像保存为 3 通道图像?
Thanks in advance.
提前致谢。
采纳答案by jabaldonedo
Your code is correct, it seems that cv2.imread
load an image with three channels unless CV_LOAD_IMAGE_GRAYSCALE
is set.
您的代码是正确的,cv2.imread
除非CV_LOAD_IMAGE_GRAYSCALE
设置,否则似乎加载具有三个通道的图像。
>>> import cv2
>>> image = cv2.imread('foo.jpg')
>>> print image.shape
(184, 300, 3)
>>> gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
>>> print gray_image.shape
(184, 300)
>>> cv2.imwrite('gray.jpg', gray_image)
Now if you load the image:
现在,如果您加载图像:
>>> image = cv2.imread('gray.jpg')
>>> print image.shape
(184, 300, 3)
It seems that you have saved the image as BGR, however it is not true, it is just opencv, by default it reads the image with 3 channels, and in the case it is grayscale it copies its layer three times. If you load again the image with scipy you could see that the image is indeed grayscale:
似乎您已将图像保存为 BGR,但事实并非如此,它只是 opencv,默认情况下它读取具有 3 个通道的图像,如果是灰度,则将其图层复制 3 次。如果您再次使用 scipy 加载图像,您可以看到图像确实是灰度的:
>>> from scipy.ndimage import imread
>>> image2 = imread('gray.jpg')
>>> print image2.shape
(184, 300)
So if you want to load a grayscale image you will need to set CV_LOAD_IMAGE_GRAYSCALE
flag:
因此,如果要加载灰度图像,则需要设置CV_LOAD_IMAGE_GRAYSCALE
标志:
>>> image = cv2.imread('gray.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
>>> print image.shape
(184, 300)
回答by Henri Korhonen
In openCV reading jpg images result 3 channel images by default. So i'm not sure if you can actually see from jpg file that it's already grayscaled but you can always load it as grayscaled. It would bring problems only if the image isn't grayscaled beforehand and for your case i believe that it wouldn't work. So short answer: you cant save jpg as onechanneled image. So you would need to grayscale it again after reading or figure out new way determine if image is grayscaled or not.
在 openCV 中读取 jpg 图像默认为 3 通道图像。所以我不确定你是否真的可以从 jpg 文件中看到它已经是灰度的,但你总是可以将它加载为灰度。仅当图像事先没有灰度化时才会带来问题,对于您的情况,我相信它不起作用。这么简短的回答:您不能将 jpg 保存为单通道图像。因此,您需要在阅读或找出新方法后再次对其进行灰度化,以确定图像是否已灰度化。
回答by Jino
try this:
尝试这个:
img = cv2.imread('gray.jpg',0)
0for gray and 1for color
0代表灰色,1代表彩色