Converting between image classes (Conversion between image classes in MATLAB)

Source: Internet
Author: User

First, similar to type conversion in C, Matlab also has its corresponding type conversion.

For example,

A = 2; double (a) ---> 2.0 or more digits

A = 2.1; uint8 (a) ---> 2. the fractional part is removed.

Because uint8 is stored in eight bits, that is, one byte, the range is 0 ~ 255. The double data that exceeds 255 is forcibly converted to 255. The double data smaller than 0 is forcibly converted to 0. The double data in the middle is directly removed from the decimal part.

A = [;]; logical (a) ---> A = [;], not all 0 are converted to 1

A = [2.0;, 0]; logical (a) ---> same as above

In fact, all a before the preceding conversions are double, because MATLAB stores the matrix as double by default...

But,

A = uint8 ([;]); logical (a) ---> A = [;], not all are converted to 1

Therefore, logical rules are relatively simple.

 

Next, let's take a look at the differences between the type conversion functions provided by Image Processing Toolbox.

There are some default prerequisites: you cannot find the source...

---> In the standard image data format, the value range of logical is {0,255}, the value range of double is [], and the value range of uint8 is [].

The following is a function example:

F = [-0.5, 0.5; 0.75, 1.5];

(1). G1 = im2uint8 (f) --> G1 = [0,128; 191,255]

---> Values lower than 0 are directly converted to 0, and values greater than 1 are directly converted to 1. Note that the maximum value in the original F is f_max = 1.5, and the minimum value is f_min =-0.5.

Then the value in F between 0 and 1 (set to f_value) is converted to (f_value-f_min)/(f_max-f_min );

It can be seen that this is a proportional conversion...

Convert all to the ratio of [0, 1], and then collectively multiply by 255

(2). Sometimes there may be multiple numbers smaller than 0 or more than 1 in F. If im2uint8 is used again, the proportion information will be lost, which is disgusting.

To look less disgusting, Matlab puts forward a function named mat2gray.

As the name suggests, matrix data (always double) Converting to gray (notation for density of image, 0 ~ 255 and so on )...

The output matrix of but, and mat2gray is in the range of [0, 1].

Ex. f = [-2.0,-0.5; 0.5, 2.0]; mat2gray (f) ---> [0, 0.3750; 0.6250, 1.0]

Find f_min and f_max, and convert each data (originally f_value) in F according to (f_value-f_min)/(f_max-f_min...

After this operation, the obtained matrix is still of the double type, and then im2uint8 can be used to map the Matrix to the range of [0,255] in the initial proportion... What a simple ing ah, stupid I have been thinking for a long time...

(3). simple logical conversion:

F = [-0.5, 0.5; 0.75, 1.5];

G1 = mat2gray (f); ---> G1 = [0, 0.5; 0.625, 1]

Gb1 = im2bw (G1, 0.6); --> gb1 = [0.6;], greater than means output 1, and vice versa .... The result matrix is of the logical type.

GB2 = G1> 0.6 ---> same as above .... It is also a Boolean value...

(4). Convert to double type...

Gb3 = im2double (gb1); ---> gb3 = [;]... only the class of gb3 is double ..

Gb4 = im2double (uint8 (gb1); ---> gb4 = [0.0039; 0.0039,]; ......

Pita... Logical to double is directly converted in the past, and uint8 to double has to divide the data of uint8 by 255...

Simply think about it, because in the image data format, double is [0, 1 .. So we need to convert it to a ratio...

(5). Missing one... Im2uint8 (f), that is, in the first case, if f_value is 1 when F is of the logical type, it is converted to 255. If F is of the 0 type, it is converted to 0...

Easy to understand, binary images are mainly the results of image segmentation, and are white (pixel value 255) instead of black (pixel value 0) to prove the existence of edges...

In short, uint8 is the standard color range, and double is between [0, 1], showing the proportion. Logical is mainly used for Binary Images ~~~~~~~~~~~

 

Test, inshow function, imshow (f)... im2uint8 will be performed on F by default... It is estimated that it is to show the true color...

 

Pay attention to these tricks... Then I had to delete a lot of songs for Ubuntu used to install MATLAB 2012, just to make a place ~~~~~~~~~~~~~~~~~

If there are any new pitfalls, continue to supplement them ~~~

 

Converting between image classes (Conversion between image classes 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.