uint8 vs int8 in imshow

19 views (last 30 days)
Dani D
Dani D on 12 Mar 2016
Edited: DGM on 27 Nov 2023
Hello, What is difference between these? When i use int8 , the most of pixel been 127 and when i use uint8 the problem is solve.
I=imread('cameraman.tif');
I = uint8(I);
imshow(I);
I=imread('cameraman.tif');
I = int8(I);
imshow(I);

Accepted Answer

Walter Roberson
Walter Roberson on 12 Mar 2016
When you give int8() a value that is greater than 127, then it "saturates" and returns 127. A lot of your input values are greater than 127 so they are "saturating" at 127.
The same kind of thing happens for uint8 if you give it a value greater than 255: it saturates at 255.

More Answers (1)

DGM
DGM on 27 Nov 2023
Edited: DGM on 27 Nov 2023
Imshow() is odd in that it does actually support int8 images. It seems most other IPT tools which are class-sensitive don't. As usual though, the image needs to be properly-scaled for its class in order to display as expected.
The data needs to be rescaled as appropriate when casting in order for the image to be treated as intended. As Walter explained, simply casting a uint8() array using int8() will simply result in truncation without otherwise scaling or shifting the data. Consider the case with uint16 -> int16 conversion:
inpict = imread('cameraman16.png'); % uint16
A = int16(inpict); % half the data is truncated
imshow(A,'border','tight')
B = im2int16(inpict); % rescaled/offset as needed
imshow(B,'border','tight')
Why did I do this example with uint16/int16 instead of uint8/int8? Well remember that I said int8 support is an oddity in IPT? Yeah. There is no such im2int8() tool like there is for 16 bit images. You would either have to do it yourself:
inpict = imread('cameraman16.png'); % uint8 (could be any other class)
ir = getrangefromclass(inpict);
or = [-128 127];
scalefactor = diff(or)/diff(ir);
C = int8((double(inpict) - ir(1))*scalefactor + or(1)); % int8
imshow(C,'border','tight')
... but there's no need to reinvent that wheel every time. MIMT imcast() supports int8, and it offers other practical conveniences.
% supports uint8,uint16,uint32,int8,int16,int32,double,single,logical
outpict = imcast(inpict,'int8');
Either way, a workflow centered around int8 working images is going to be a huge hassle with no obvious benefit.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!