Reading uint16 image and converting to uint8 problem

20 vues (au cours des 30 derniers jours)
Oskar
Oskar le 12 Fév 2019
Commenté : Oskar le 13 Fév 2019
I have been trying to convert my 16-bit TIF file to a 8-bit image inside MATLAB. When opening in ImageJ it looks great. After doing imread of the TIF the values do however already look strange. All the pixel value are extremely close to 0.5*2^16. After to conversion to 8-bit I only end up with a gray image (128 in pixel value all across the image).
Can I read the images in another way with MATLAB? Or is there a better way to convert them from 16-bit to 8-bit?
Please see the attached file.
  5 commentaires
Oskar
Oskar le 12 Fév 2019
Thanks for the formatting Jan!
So I use imadjust to adjust the contrast to make the image more visible. So I can see my image with the code below:
obj = Tiff(filepath);
I = read(obj);
I_uint16 = uint16(I);
figure();imshow(imadjust(I_uint16));
It looks like below. I observe the image values (around 32769) all across the image.
Capture.PNG
Jan
Jan le 12 Fév 2019
Modifié(e) : Jan le 12 Fév 2019
"I observe the image values (around 32769) all across the image." Now please explain, how you observe this: before or after imadjust and/or unit16? With which tool? Why you expect something else? What do you see for:
obj = Tiff(filepath);
Img = read(obj);
class(Img)
Img16 = im2uint16(Img);
figure
imshow(Img16);
ImgD = im2double(Img);
figure
image(ImgD)

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 12 Fév 2019
As far as I can tell, there is nothing wrong with the way matlab decodes your image. It genuinely have very little contrast with all intensities in the range 32768 to 32786. Viewing that image with other image viewers show the same image.
Note that instead of imadjust, you can simply tell imshow to scale the display to the intensity range of the image:
img = imread('ch1_561Exc_685Det_Conf.tif');
imshow(img, [])
However note that the image has two private tags with IDs 50838 and 50839. A quick search show they're ImageJ tags. Perhaps they affect how ImageJ interpret the image. Since they're private, matlab certainly doesn't know what to do with them.
>> info = imfinfo('ch1_561Exc_685Det_Conf.tif');
>> info.UnknownTags(1).Value
ans =
12 3608
>> char(info.UnknownTags(2).Value(1:8))
ans =
'IJIJinfo'
>> native2unicode(uint8(info.UnknownTags(2).Value(13:end)), 'UTF16')
ans =
' ExpControl Ch1 {0} BitsPerPixel = 16
ExpControl Ch1 {0} DimensionOrder = XYCZT
ExpControl Ch1 {0} IsInterleaved = false
ExpControl Ch1 {0} IsRGB = false
ExpControl Ch1 {0} LittleEndian = true
ExpControl Ch1 {0} PixelType = int16
ExpControl Ch1 {0} SizeC = 1
ExpControl Ch1 {0} SizeT = 1
ExpControl Ch1 {0} SizeX = 170
ExpControl Ch1 {0} SizeY = 1510
ExpControl Ch1 {0} SizeZ = 1
ExpControl Ch2 {0} BitsPerPixel = 16
ExpControl Ch2 {0} DimensionOrder = XYCZT
ExpControl Ch2 {0} IsInterleaved = false
ExpControl Ch2 {0} IsRGB = false
ExpControl Ch2 {0} LittleEndian = true
ExpControl Ch2 {0} PixelType = int16
ExpControl Ch2 {0} SizeC = 1
ExpControl Ch2 {0} SizeT = 1
ExpControl Ch2 {0} SizeX = 170
ExpControl Ch2 {0} SizeY = 1510
ExpControl Ch2 {0} SizeZ = 1
Series 0 Name = ExpControl Ch1 {0}
Series 1 Name = ExpControl Ch2 {0}
Description =
ExpControl Ch1 {0} Labels = [ExpControl X, ExpControl Y, Ax1, Ax2]
ExpControl Ch1 {0} Lengths = [3.3999999686784577E-6, 3.020000076503493E-5, 1.0, 1.0]
ExpControl Ch1 {0} Name = ExpControl Ch1 {0}
ExpControl Ch1 {0} Offsets = [-3.7999998312443495E-6, -6.800000846851617E-6, 0.0, 0.0]
ExpControl Ch1 {0} StepLabels = [[], [], [], []]
ExpControl Ch1 {0} Steps = [[], [], [], []]
ExpControl Ch2 {0} Labels = [ExpControl X, ExpControl Y, Ax1, Ax2]
ExpControl Ch2 {0} Lengths = [3.3999999686784577E-6, 3.020000076503493E-5, 1.0, 1.0]
ExpControl Ch2 {0} Name = ExpControl Ch2 {0}
ExpControl Ch2 {0} Offsets = [-3.7999998312443495E-6, -6.800000846851617E-6, 0.0, 0.0]
ExpControl Ch2 {0} StepLabels = [[], [], [], []]
ExpControl Ch2 {0} Steps = [[], [], [], []]
Location = E:\LundUniversity\Experiments\STED Microscopes\JonasSTED\2018-01-23_DNA_YOYO3_TOTO3\2018-01-23_Selection_2PanelImages_ConfSTED\2018-01-23_v02c_ConfSTED_LambdaDNA-TOTO3.msr
  1 commentaire
Oskar
Oskar le 13 Fév 2019
Thanks for this comprehensive answer!

Connectez-vous pour commenter.

Plus de réponses (1)

Oskar
Oskar le 12 Fév 2019
I see these values before imadjust. I see this after both
obj = Tiff(filepath);
I = read(obj);
and
I_16 = imread(filepath);
I just look at the variables. After looking at the file again with ImageJ I see similar pixel values (around 32769). However, if I convert it to 8-bit in ImageJ the pixel values vary from 0 to 100. So maybe there is a better way to convert the uint16 image with pixel values from 32768 to 32786 to uint8?
Otherwise I'll just do it in ImageJ...
After your code Jan I see this:
Capture2.PNG
Thanks for all the help!

Community Treasure Hunt

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

Start Hunting!

Translated by