Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenCV (Python), why am I getting 3 channel images from a grayscale image?

I am using Python (2.7) and bindings for OpenCV 2.4.6 on Ubuntu 12.04

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.

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)     print gray_image.shape 

I get (480, 640, 1), which I expect for a 640x480 grayscale image. I then save the image:

    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:

    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.

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.

     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!

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

If the image is not single channel, why is OpenCV saving my grayscale image to a 3 channel image at disk write?

Thanks in advance.

like image 804
Steve Avatar asked Sep 18 '13 11:09

Steve


People also ask

Why does my grayscale image have 3 channels?

The grayscale image has three channels because technically it is not a grayscale image. It is a colored image with the same values for all the three channels (r, g, b) in every pixel. Therefore, visually it looks like a grayscale image.

How many channels are there in a color image taken in the default OpenCV program?

In openCV reading jpg images result 3 channel images by default.

What is channel in image OpenCV?

There are three channels in an RGB image- red, green and blue. The color space where red, green and blue channels represent images is called RGB color space. In OpenCV, BGR sequence is used instead of RGB. This means the first channel is blue, the second channel is green, and the third channel is red.


2 Answers

Your code is correct, it seems that cv2.imread load an image with three channels unless CV_LOAD_IMAGE_GRAYSCALE is set.

>>> 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:

>>> 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:

>>> image = cv2.imread('gray.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE) >>> print image.shape  (184, 300) 
like image 114
jabaldonedo Avatar answered Sep 24 '22 17:09

jabaldonedo


try this:

 img = cv2.imread('gray.jpg',0) 

0 for gray and 1 for color

like image 40
Jino Avatar answered Sep 22 '22 17:09

Jino