The difference between double, im2double and Mat2gray in MATLAB

Source: Internet
Author: User

Reprint: http://blog.sina.com.cn/s/blog_6c41e2f30101559d.html

Suppose an image data a (uint8 format) **********************

A =

235 200 89 20

>> double (A)% returns a matrix with the same value as the original matrix but type double;

Ans =

235 200 89 20

>> im2double (A)
% return matrix type: double; value range [0 1], 0 corresponds to 0;1 in uint8 corresponding to 255 in uint8;

Ans =

0.9216 0.7843 0.3490 0.0784

>> Mat2gray (a)% normalization of the original matrix

Ans =

1.0000 0.8372 0.3209 0

Suppose that matrix A is a general two-dimensional array, non-image data (double format) **********************

A =

235 200 89 20

>> Double (A)

Ans =

235 200 89 20

>> im2double (A)

Ans =

235 200 89 20

>> Mat2gray (A)

Ans =

1.0000 0.8372 0.3209 0

Summary ***************************

Im2double: If the input type is uint8, unit16, logical, the value is proportionally treated as a double value between 0-->>0,255-->>1, if the input type is double, The output is not processed;

Double: Returns the same value as the input double type matrix;

Mat2gray: The input is normalized, the minimum value-->>0; the maximum value-->>1, and the output type is double.

In the actual process of image processing, because we read the image is unit8 type, and in the matrix operation of MATLAB requires all the operation variables are double type (dual type). Therefore, the im2double function is often used to convert image data to double-precision data.

To conserve storage space, MATLAB provides a special data type Uint8 (8-bit unsigned integer) for the image, and the image stored in this way is called a 8-bit image.

Imread the grayscale image into a 8-bit matrix, and when it is an RGB image, it is stored in a 8-bit RGB matrix.

As a result, Matlab reads the image data is uint8, and MATLAB is generally used in the value of double type (64-bit) storage and operation. So you have to convert the image to a double-format.

I2=im2double (I1)% converts image I1 to double precision type (assuming graph matrix range 0~255)

Or

I64=double (I8)/255; %uint Convert to Double

If you do not convert, the calculation generates an overflow.

After calculation, the I2 is already a double type. If you want to imshow display image results Now, you need to convert to uint8 format. If the matrix composite data Image standard (between 0~1),

I3=im2uint8 (I2)% convert matrix I2 to uint8 type

If the 0~1 range is exceeded, it is necessary to use UINT8 ()

I8=uint8 (Round (i64*255)); %double conversion into Uint8

or Mat2gray ()

I3=mat2gray (I2) & convert matrix to grayscale image format double

Finally, it can be known that im2uint8,im2double and uint8,double are different.

The difference between double, im2double and Mat2gray in MATLAB

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.