Use low-level functions (not imread.m) to read an grayscale image

7 views (last 30 days)
I try to use low-level functions (fopen.m and fread.m) to read a grayscale image of uint16 in tiff format as follows:
fid = fopen(filepath,'r');
img = fread(fid,[ncolms, nrows], 'uint16=>uint16')';
The obtained image matrix is different from that obtained by simply using imread.m:
img = imread(filepath)
The resulting images by the two approaches are shown below (the left is by fread.m and the right is by imread.m):
You can see that, in addition to the obvious intensity difference, fread.m gives some artifacts in the top edge of image. I think this must be due to their different mechanisms of reading images.
I want to know how to use such low-level functions as fopen.m and fread.m to read images (grayscale not binary), equivalently to using imread.m, if they can?

Answers (1)

dpb
dpb on 12 May 2017
Edited: dpb on 13 May 2017
See <TIFF-v6> for structure...
ERRATUM: Corrected link -- dpb
ADDENDUM
"I think this [displayed image differences] must be due to their different mechanisms of reading images."
Well, not really. As the spec shows, there's a whole lot more in an image file than just the data...you were just lucky in that the image you chose was only a single image in the file and wasn't compressed (albeit for gray-scale that's often futile so they generally aren't) so that what you got by ignoring all that structure in the file looked anything whatsoever like an image.
Sure, it's possible, but you've got to decode the file header to retrieve the data from the proper locations within the file where it's stored and put it together properly.
Why would you want to reinvent the wheel, here???

Community Treasure Hunt

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

Start Hunting!