# Image data type conversion uint8 and double in MATLAB

The image pixel data is processed in MATLAB

Imshow pictures in MATLAB should be converted into uint8: 1

subplot(1,2,1),imshow(uint8(img1)),title(‘original’);
subplot(1,2,2),imshow(uint8(img2)),title(‘after’);

MATLAB image processing on Unit8_ Baidu Knows https://zhidao.baidu.com/question/545122188.html

In order to save storage space, matlab provides a special data type uint8 (8-bit unsigned integer) for image, and the image stored in this way is called 8-bit image
imread stores gray images in an 8-bit matrix. When it is an RGB image, it is stored in an 8-bit RGB matrix

therefore, the image data read in MATLAB is uint8, and the values in MATLAB are generally stored and calculated in double (64 bit). Therefore, it is necessary to convert the image to double format before operation,
I2 = im2double (I1)% convert image I1 to double precision type (assuming graph matrix range 0-255)
or
i64 = double (I8)/255% uint is converted to double
if it is not converted, the calculation will overflow

MATLAB image type conversion and uint8, double, im2double, im2uint8 and mat2gray and other instructions – Thomas can write – CSDN blog https://blog.csdn.net/kakiebu/article/details/78959249

## 1. MATLAB image saving instructions

The data saved after reading pictures in MATLAB is uint8 (8-bit unsigned integer, i.e. 1 byte). The image stored in this way is called 8-bit image. Compared with the default MATLAB data type, double precision floating-point double (64 bits, 8 bytes), it can naturally save a lot of storage space
in detail, imread stores gray-scale images in an 8-bit matrix. When it is an RGB image, it stores it in an 8-bit RGB matrix. For example, if the pixel size of a color image is 400 * 300 (height * width), the saved data matrix is 400 * 300 * 3, where each color channel value is between 0 and 255

``Function ‘*’ is not defined for values of class ‘uint8’``

One byte unsigned integer can only store 255 data at most, so it is easy to overflow the image operation

## 2. MATLAB image type conversion

The image data read by Matlab is uint8, and the values in MATLAB are usually stored and calculated by double (64 bit). Therefore, it is necessary to convert the image to double format before operation. The differences are as follows:

img = imread(‘./1.jpg’); % Read in is Unit8 (0 ~ 255) data

I1 = im2double(img); % Convert image to double precision type (0 ~ 1)

I2 = double(img)/255; % Uint8 is converted to double, which is the same as im2double

Here is a supplement to explain the difference between im2double() and double(). Double (IMG) is a simple data type conversion, which converts an unsigned integer to a double precision floating-point double, but the data size does not change. The original data is between 0 and 255, but after conversion, it is still 0 to 255. For example, if the original value is 255, then it will be 255.0 after conversion. The number of decimal places 0 is determined by the length of double data. The actual data size is 255, but this 255 is already a double type space storage, and no overflow will occur if it is added. Im2double (IMG) not only converts uint8 to double, but also maps the data size from 0 to 255 to 0 to 1