Mr.Teen
Mr.Teen

Reputation: 591

What's the meaning of the values in the OpenCV image representation?

I am using OpenCV with Python. When I load a JPG or PNG image by calling cv2.imread(), I get a 2D matrix, whose size is the same as the resolution of that image. Each number in the matrix is in the range of 0 to 255.

I don't understand how this matrix can represent an image. In particular, I expect to see a 3D matrix, whose third dimension represents RGB channels. If a pixel is represented by one 8-bit integer, then the whole image can only have 256 colors, but it is clearly not true.

What am I missing here?

Upvotes: 1

Views: 2009

Answers (3)

satyamsovan123
satyamsovan123

Reputation: 11

For someone, still confused, here you go. I assume the OP is referring to a coloured image. OpenCV represents the image as [Blue, Green, Red] (because BGR was more popular in the 90's compared to RGB). Let's take an example of a 3 x 3 image. It would be represented as follows: Image showing a 3D matrix

I hope it solves the query.

Upvotes: 0

Dmitry Aleks
Dmitry Aleks

Reputation: 156

"imread" defaults the second argument to 0, which means that your image is converted to 8-bit depth grayscale. Therefore you are getting a value from 0 to 255 as a color for each pixel of your image.

Try changing your call as follows to get a 3-channel color image:

cv2.imread("yourimage.bmp",1);

Upvotes: 2

michaeltang
michaeltang

Reputation: 2898

refer doc here

Mat src1 = imread(inputImageFilename1.c_str(), 1); # make sure flag > 0

Upvotes: 2

Related Questions