-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadBinaryFileToMatrix.m
More file actions
74 lines (65 loc) · 2.91 KB
/
ReadBinaryFileToMatrix.m
File metadata and controls
74 lines (65 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function [image_mat]=ReadBinaryFileToMatrix(filename, height, width, imgBitDepth, dataFormat)
% function [image_mat]=ReadBinaryFileToMatrix(filename, height, width, imgBitDepth, dataFormat)
%
% Author: Aaron Winder
% Affiliation: Engineering Science and Mechanics, Penn State University
% https://github.com/awinde
%
% DESCRIPTION: Reads in a binary image file into a matrix
%
%_______________________________________________________________
% PARAMETERS:
% filename - [string] complete file name including extension
%
% height - [int] height of the grabbed images in pixels, this
% can be found in the .tdms file notes
%
% width - [int] width of the grapped images in pixels, this
% can be found in the .tdms file notes
%
% imgBitDepth - [string] bit depth of the grabbed images. For
% the whisker camera, this should be 'uint8', for the CBV
% camera, this should be 'uint16'
%
% dataFormat - [string] designates the data ordering of the
% camera information. This value must comply with the codes
% in the fread documentation. The Dalsa 1M60 (window camera)
% streams data to disk in the Big-endian format
% (dataFormat='b'). The Basler acA640-120gm (whisker camera)
% streams data to disk in the little-endian format
% (dataFormat='l')
%_______________________________________________________________
% RETURN:
% image_mat - [matrix, width x height x frames]
%_______________________________________________________________
display([mfilename ': Reading ' filename '...'])
% Clock performance
t1 = tic;
% Calculate pixels per frame for fread
pixels_per_frame=width*height;
% open the file , get file size , back to the begining
fid=fopen(filename);
fseek(fid,0, 'eof');
thefile_size=ftell(fid);
fseek(fid,0, 'bof');
% Identify the number of frames to read. Each frame has a previously
% defined width and height (as inputs). uint16 has 2 bytes per pixel, uint8
% has 1 byte per pixel.
% Get the number of bytes
testVariable = zeros(1,imgBitDepth);
varData = whos('testVariable');
numBytes = varData.bytes;
nframes_to_read=floor(thefile_size/(numBytes*pixels_per_frame));
display([mfilename ': ' num2str(nframes_to_read)...
' frames to read.'])
% PreAllocate
image_mat = zeros(width,height,nframes_to_read,imgBitDepth);
for n=1:nframes_to_read
z=fread(fid,pixels_per_frame,['*' imgBitDepth],0,dataFormat);
image_mat(:,:,n) = reshape(z(1:pixels_per_frame),width,height);
end
fclose(fid);
elapsedTime = toc(t1);
display([mfilename ': Done reading ' filename '. Time elapsed: ' num2str(elapsedTime) ' seconds.'])
display('-----------------------------------------------------')
display('-----------------------------------------------------')