Imshow () function: the parameters are of different types.

Source: Internet
Author: User
Tags ranges

This is a problem yesterday:

RGB = imread('a.jpg ');
The obtained RGB is a uint8 three-dimensional array.
So
Imshow (RGB), OK is OK, and the source image is obtained.
However
Imshow (double (RGB ))
Or imshow (uint16 (RGB ))

Or imshow (single (RGB ))

All the results are black or white.

For half a day, it may feel that the range of various types is different, because for grayscale image, different types have different ranges [imshow (I, [low high])]

What about truecolor image?



Now we can understand the following points:

In the Help file, there is a sentence: for Grayscale Images of classSingleOrDouble, The default display range is[0 1]

It can also be associated with truecolor, single, and double types in the range of [0, 1.

Therefore, replace imshow (double (RGB) with imshow (double (RGB)/255) or imshow (
Replace single (RGB) with imshow (single (RGB)/255;

For unit16, we know that the range of unit8 is [0 255], and the range of uint16 is [0 2 ^ 16-1]. In the same way, replace imshow (uint16 (RGB) with imshow (
Uint16 (RGB) * 255)


(PS: I think the truecolor ranges from 0 to 255. Therefore, the default value is uint8. Note that errors may occur when adding the values, for example, the sum of 180 of uint8 and 180 of uint8 overflows, and the result is 255. special attention is required here! The solution should be changed to uint16 or double, but double is. In the end, imshow should also prevent errors. So I was wondering if there was a way to avoid that much trouble, so don't turn around?)


Note the following when converting data types:

T = [1 2 3]; here t is of the double type. to convert it to unit8, you only need t = uint8 (T). For uint16, similar methods are used for signle and so on.

However, if you use the "im2xx" function, you must note that the default value of the double type is [0 1], and that of the uint8 type is [0 255 ].



Take the following example:

T = [1 2 3]; % here t is of the Double Type

% You want to convert t to uint8 type

T1 = im2uint8 (t );

%, The result is

T1 = [255 255 255] % This is incorrect

% How to get the correct result

T2 = uint8 (t); % The result is t2 = [1 2 3] and is of the unit8 type.

% Or

T2 = im2uint8 (T/255); % at this time, T2 = [0 1 2] and is of the unit8 type.


Likewise: two methods to convert t to uint16:

T = [1 2 3]; T1 = im2uint16 (T/65535); % because the uint16 range is [0 65535] T2 = uint16 (t) % use the uint16 function directly



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.