How does MATLAB reads an image?

2 次查看(过去 30 天)
Deep P
Deep P 2017-6-20
Hello,
I would like to know how MATLAB reads an input image. Is the in the integer format or the floating point value? Is there any document available? Please give me the reference links.
Thank you.

回答(2 个)

Image Analyst
Image Analyst 2017-6-20
It reads it with imread() if it's one of the standard formats that you can see with the "imformats" command. The data class can be uint8, uint16, or double, depending on how it was saved into the file. Or it can be some special format like netcdf or dicom where you get a bunch of metadata back also, in addition to the pixel values.
  2 个评论
Deep P
Deep P 2017-6-20
Thanks for your response. I am trying to create few test images for my project. I understand that image has floating point values if I use data class double. Is it going to make any difference if I create the image using data class uint16?
Image Analyst
Image Analyst 2017-6-21
Yes, of course. It will round off the fractional part. Why do you want to save the image? If it's just to use again later with MATLAB (only), then save it in a .mat file with save(). If you need it in another program, I think the TIFF class can save double images.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-6-21
imwrite() always converts floating point to integer before writing. If you need to write double precision, then use something like,
filename = 'myfile.tif'; %adjust as needed
t = Tiff(filename,'w');
t.setTag('Photometric',Tiff.Photometric.MinIsBlack); % assume grayscale
t.setTag('BitsPerSample',64);
t.setTag('SamplesPerPixel', size(YourImage,3));
t.setTag('SampleFormat' ,Tiff.SampleFormat.IEEEFP);
t.setTag('ImageLength', size(YourImage,1));
t.setTag('ImageWidth', size(YourImage,2));
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.write(YourImage);
t.close();
(Thanks to John from https://www.mathworks.com/matlabcentral/newsreader/view_thread/314008 for the key hint I had been missing for years.)
However:
  1. You will probably not find any image viewer that can display these images, except possibly ImageJ
  2. In nearly every case I have seen so far in which someone was asking to write double precision images, what was happening was that they were reading in integer images, doing a floating point transformation on them, doing information hiding in the floating point values (watermarking or steganography) doing the inverse transform, and wanting to write the modified data stream to a file "exactly" in order to be able to read the exact coefficients back later -- the 64 bits being necessary because the inverse transform was coming out as non-integer (sometimes including negative values.) Writing out 64 bit floating point in image form is the wrong solution to this situation -- the right solution being to either use an integer to integer transform or else to do the coefficient modifications in a way that after inverse transform and rounding to integer, the new integer coefficients can still have information extracted from them.

Community Treasure Hunt

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

Start Hunting!

Translated by