Ask Your Question
0

Whyare all the images in uint8 data type?

asked 2015-10-29 07:14:08 -0600

arp1561 gravatar image

I was just learning OpenCV and I wondered why all the images have a unint8 data type?

I understand what a uint8 data type is, but how does it apply here?

I hope you can help me! Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-29 09:49:15 -0600

updated 2015-10-29 09:55:18 -0600

In OpenCV an "image" is represented by a cv::Mat datatype

Not all Mats are uint8 data type.

Mat initialization can be done in the following way

cv::Mat test = cv::Mat(rows, cols, type)

As an example, to initialize a 640*480 Mat of type uint8 with one channel:

cv::Mat test = cv::Mat(480, 640, CV_8UC1);

In which "CV" is the prefix of all data types, 8 means the number of bytes of each pixel, U stands for unsigned and C1 means that Mat has one channel.

Other examples:

CV_16UC1 : Mat of type unsigned short (uint16) with one channel

CV_32FC3 : Mat of type float with three channels

CV_8UC3: Mat of type unsigned char (uint8) with 3 channels (used to display RGB images)

CV_32SC2: Mat of type signed integer (int32) with two channels

The reason why Uint8 is very common is because it is the standard way to display images, in which each pixel ranges between 0 and 255. If it is a gray scale image, a pixel with value 0 is black and a pixel with value 255 is white, and gray in the middle

edit flag offensive delete link more

Comments

Well, Thanks a lot for this amazing answer I have totally understood that uint8 is used since the max value of RGB/BGR channels are 0,255 and uint8 has that range. Got that

But, Why would have an image have another data type that is more than uint8? Since the BGR/RGB channels support only 0,255 values??

Thanks in advance! :)

arp1561 gravatar imagearp1561 ( 2015-10-30 05:32:10 -0600 )edit

You got your first reasoning backwards. The RGB/BGR values are [0, 255] BECAUSE uint8 has that range :) It turns out that having 255 possible values for one pixel is enough resolution to have a clear image while not consuming much memory at the same time.

As for the other question, a cv::Mat isn't only needed to display images. A cv::Mat may be used to store any kind of data in the form of a matrix. For example, if you need to perform matrix calculations with floating point precision, uint8 isn't enough. If you have a sensor that outputs data in the range of [0-10000] and need to read it into a cv::Mat, you need uint16 datatype.

Basically OpenCV tries to be as flexible as possible :)

PedroBatista gravatar imagePedroBatista ( 2015-10-30 05:56:57 -0600 )edit

Thank You so very much!! :) Helped me out a lot!! :D

arp1561 gravatar imagearp1561 ( 2015-10-31 03:02:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-29 07:14:08 -0600

Seen: 16,457 times

Last updated: Oct 29 '15