Pixel type imread

What is the default pixel type of a pixel to create a read? I test different images, all of them give me unsigned char with different channels. If I tried to create a pixel type with a signed char, if I did not ask it explicitly?

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

I checked the cv :: imread document but said nothing about the pixel pixel imread create.

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

+4
source share
1 answer

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

Some images, such as .png or .tif , also support a 16-bit pixel value, so their data type is unsigned short . But this is not necessary, as they can be 8 bits.

To load the image as is in OpenCV, use imread as follows:

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

There are various combinations of these flags:

Download 8 bits (regardless of the original depth):

CV_LOAD_IMAGE_COLOR

Loading with original depth:

CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH

Download as grayscale (no matter how many channels an image has):

CV_LOAD_IMAGE_GRAYSCALE

Grayscale loading with original depth:

CV_LOAD_IMAGE_GRAYSCALE| CV_LOAD_IMAGE_ANYDEPTH

In your case, lena.jpg is an 8-bit image, so you get an 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_* .

eg. CV_LOAD_IMAGE_GRAYSCALE becomes cv::IMREAD_GRAYSCALE .

+12
source

All Articles