Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pixel type of imread

Tags:

c++

opencv

What is the default pixel type of imread create? I test different images, all of them give me unsigned char with different channels. Would imread create a pixel type with signed char if I do not ask it explicitly?

cv::Mat img = cv::imread("lena.jpg", -1); //always give me unsigned char

I checked the document of cv::imread, but it said nothing about the default pixel of imread create.

The link of the document http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat imread(const string& filename, int flags)

like image 629
StereoMatching Avatar asked Dec 04 '22 13:12

StereoMatching


1 Answers

Most .jpg images are 8 bit images. Therfore their default data-type is unsigned char or char.

Some images like .png or .tif also support 16 bit pixel value, so their data type is unsigned short. But it is not necessary as they may be 8 bit.

To load the image as-is in OpenCV, use imread like this:

cv::Mat img = cv::imread(imagePath, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);

There are different combinations of these flags:

Load as 8 bit (whatever the original depth is):

CV_LOAD_IMAGE_COLOR

Load with original depth:

CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH

Load as grayscale (no matter how many channels the image has):

CV_LOAD_IMAGE_GRAYSCALE

Load as grayscale with original depth:

CV_LOAD_IMAGE_GRAYSCALE| CV_LOAD_IMAGE_ANYDEPTH

In your case, lena.jpg is 8 bit image, so you are getting unsigned char data type.

Update:

For newer versions of OpenCV, use the flags defined by enum in the C++ interface. Just replace CV_LOAD_IMAGE_* with cv::IMREAD_*.

e.g. CV_LOAD_IMAGE_GRAYSCALE becomes cv::IMREAD_GRAYSCALE.

like image 139
sgarizvi Avatar answered Dec 25 '22 02:12

sgarizvi