uint8 vs int8 in imshow
Show older comments
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
More Answers (1)
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(round((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,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.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
