Is it possible to convert a tiff image to uint16 without changing the images actual appearance?

9 views (last 30 days)
I have a code that I am using to change some tiff images to uint16. The images look darker than the original. Is there a way to make them identical? I have attached a part of my code below.
for j = 1:length(imageFiles)
currentImage = imageFiles(j).name;
imagePath = fullfile(subfolderPath, currentImage);
outputImageName = [currentImage(1:end-5), '_converted.tiff']; % Adjusted output image name
% Read the image
img = imread(imagePath);
% Convert to unsigned 16-bit integer
img_uint16 = im2uint16(img);
% Save the image
tiffPath = fullfile(subfolderPath, outputImageName);
imwrite(img_uint16, tiffPath, 'tiff', 'Compression', 'none', 'WriteMode', 'overwrite');

Accepted Answer

Image Analyst
Image Analyst on 19 Feb 2024
If you have a uint8 image and the values are in the range of 0-255, it seems to multiply everything by 256 (roughly) to make the new uint16 image in the range of 0-65535 if you use im2uint16():
% Read the image
img = uint8(1:100);
% Convert to unsigned 16-bit integer
img_uint16 = im2uint16(img);
maxValue = max(img_uint16, [], 'all')
maxValue = uint16 25700
If you use imshow(img, []), it should show up identically for both classes. Are you sure you're using [] in imshow? Are you sure you want to change the range of the numbers? If not, just use uint16() instead of im2uint16().
img2 = uint16(img);
maxValue = max(img2, [], 'all')
maxValue = uint16 100
In that case it might look darker if you didn't use [] (because 100 is just a small fraction of 65535 so everything will be scaled to zero before display) but if you did, it should look the same.
  3 Comments
Image Analyst
Image Analyst on 20 Feb 2024
Edited: Image Analyst on 20 Feb 2024
Yes, like I said -- everything will be scaled to zero.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
DGM
DGM on 21 Feb 2024
Don't use uint16() or uint8() unless you do the appropriate scaling as well. Since we don't have the image and we don't know what your objective is, we can't tell you what the appropriate scaling is.
IPT im2uint16(), im2uint8(), etc. presume that floating-point inputs are unit-scale. If your data is not unit-scale, and you're viewing it using imshow(myimage,[]), then you're being blinded to what the actual scale is; consequently, you're blinding yourself to what the actual brightness/contrast is. If that's what you want, then just use mat2gray() on the data prior to using im2uint16(). Otherwise, attach an example tiff and elaborate on your expectations regarding how things should be represented.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!