Is there an elegant way to read in a 16-bit image binary file row-by-row?

5 views (last 30 days)
I have a number of 16-bit image files from a camera sensor which is 3860 pixels wide and 2178 pixels high. The pixel values are written row-by-row into a binary file (little endian).
I've written a bit of Matlab code (below) to read in the binary values and convert them to an array comprising 3860 columns and 2178 rows to match the pixel locations. The code works, but it doesn't feel very elegant.
My problem is this: When I fread in the binary data, the first 7720 bytes of data are placed in column vector 1 and then it starts filling up column vector 2 and so on. I would prefer fread to allocate the first 7720 bytes to row 1 and then wrap round to the start of row 2 etc.
E.g. fread creates an array thus:
3040 3051 3047 ...
3107 3036 3083 ...
2989 3016 3112 ...
... ... ... ...
whereas I want the output format below:
3040 3107 2989 ...
3051 3036 3016 ...
3047 3083 3112 ...
... ... ... ...
I've solved it by using the transpose function but wondered if there was a better way to do this?
(I realise that I can analyse the array values without transposing but I want the array to match the image sensor pixel layout for simplicity.)
imgCol=3860; % width of image
imgRow=2178; % height of image
filename='my_binary_file(3860x2178image).raw' %raw image file (no header) file size 16,814,160 bytes
fileID = fopen(filename, 'r', 'ieee-le'); % open the file for low level I/O function and obtain file identifier
if fileID == -1, error('Cannot open file: %s', filename); end
precision = 'uint16'; %16-bit file, little endian
Array = fread(fileID, [imgCol imgRow], precision); %reads data at byte level
fclose(fileID);
Array= transpose(Array);

Answers (0)

Community Treasure Hunt

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

Start Hunting!