Conversion of common MATLAB Data Types

Source: Internet
Author: User

For a long time, various image processing is often used in the process of using MATLAB for computation. Therefore, we need to convert the data of the image. Therefore, we need to sort out the frequently used conversions, easy to view.

1. Convert uint8 to double to facilitate image operations:

The double function only converts the uint8 data that is read into the image to the double type, which is generally not used.

The im2double function is commonly used to convert the uint8 image to the double type. The value range is 0-1. If the image is 255, 255 is converted to 1, 0, or 0.
The data type of the image read in MATLAB is uint8, and the data type used in the matrix is double.
Therefore
I2 = im2double (I1): Convert the Image array I1 to the Double Precision type. If the conversion is not performed, the addition or subtraction of uint8 will overflow.
By default, Matlab stores the data in the image as a double type, that is, a 64-bit floating point number;

MATLAB also supports unsigned integer types (uint8 and uint16). The advantage of uint type is space saving and must be converted to double type During computation.

Im2double (): converts an image array to a double precision type.
Im2uint8 (): converts an image array to the unit8 type.
Im2uint16 (): converts an image array to the unit16 type.

2. Differences between uint8 and im2uint8

 

During data type conversion, the difference between uint8 and im2uint8 is that the uint8 operation only removes the part after the decimal point of a double type;

However, im2uint8 sets the number of all values smaller than 0 in the input to 0, and sets all values greater than 1 in the input to 255, and then multiply all other values by 255.

Image data must be converted to double before calculation to ensure accuracy;

Many matrix data are double. to display the data, you must first convert it to the standard data format of the image.

If the data before conversion meets the image data standards (for example, if it is double, it must be in the range of 0 ~ 1), you can directly use im2uint8

 

If the data distribution before conversion is irregular, use uint8 to automatically cut it to 0 ~ 255 (more than 255 by 255), so it is best to use mat2gray to convert a matrix into a grayscale image data format (double)

3. Double-Type Image Display

Before calculation, the image data must be converted to the double type, which ensures the accuracy of Image Data calculation.

Many Matrices use double data. to display them, you must first convert them to the standard data format of the image.

If you run imshow (I) directly, we will find a white image.

This is because imshow () indicates that the double type is regarded as 0 ~ In the range of 1, that is, when the value is greater than 1, it is displayed as white, while imshow shows that the value of uint8 is 0 ~ The value range is 255.

The double data with the calculated range between 0 and is incorrectly displayed as a white image.

Specific methods include:

Imshow (I/256); ---------- convert the image matrix to 0-1

Imshow (I, []); ----------- automatically adjust the data range for display (note that here, it must be a grayscale image, not responsible)

Imshow (uint8 (I ));

Imshow (mat2gray (I); %

The above mat2gray converts the final matrix to a grayscale image. Commonly used:


A = im2uint8 (mat2gray (result ))
In this way, the result matrix is converted to a uint8 image.

 

 

 

Conversion of common MATLAB Data Types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.