Clear Filters
Clear Filters

uint8 vs int8 in imshow

21 views (last 30 days)
Dani D
Dani D on 12 Mar 2016
Edited: DGM on 12 Jun 2024 at 8:25
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 = uint8(I);
I = int8(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 on 27 Nov 2023
Edited: DGM on 12 Jun 2024 at 8:25
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
B = im2int16(inpict); % rescaled/offset as needed
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(round((double(inpict) - ir(1))*scalefactor) + or(1)); % int8
... 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,uint64,
% int8,int16,int32,int64,
% 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.


Find more on Images in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!