Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
MatlabMain/fread_Tsm_redshirt.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (74 sloc)
3.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% alternative approach to read out binary data | |
% fullframe loading | |
ind1 = NaN(1,256*256/2); | |
ind2 = NaN(1,256*256/2); | |
for c = 1:256 | |
ind1( [1:128]+128*(c-1) ) = [1:128] + 256*(c-1); | |
ind2( [1:128]+128*(c-1) ) = fliplr([129:256] + 256*(c-1)); | |
end | |
n_frames = 100; | |
pixel_interval = 256*256; | |
frames_skip = 2000; | |
bit_depth = info.PrimaryData.Keywords{2,2}; | |
byte_conversion = bit_depth/8; | |
fid = fopen(filename,'r'); | |
fseek(fid,offset,'bof') | |
% skip to the end of frame_skip and start reading from the following frame | |
fseek(fid, pixel_interval * byte_conversion * frames_skip, 'cof') | |
datatemp = fread(fid,[256*256,n_frames],'int16'); | |
data1 = datatemp(ind1,:); | |
data1 = reshape(data1,128,256,n_frames); | |
data2 = datatemp(ind2,:); | |
data2 = reshape(data2,128,256,n_frames); | |
dataout = cat(1,data1,data2); | |
fclose(fid); | |
% reading pixels from ROIs, ROIs should be rectangles of pixels | |
% ROIs given as [rows , cols]; transform into pixel linear indices | |
fid = fopen(filename,'r'); | |
frame_pxl = prod(rec_dim(1:2)); % pixels per frame | |
pix_linear = reshape(1:rec_dim(1)*rec_dim(2),rec_dim(1),rec_dim(2)); %linear indices pixels in final de-interleaved matrix | |
ind1 = NaN(1,256*256/2); | |
ind2 = NaN(1,256*256/2); | |
n_frames = 100; %input function | |
for c = 1:256 | |
ind1( [1:128]+128*(c-1) ) = [1:128] + 256*(c-1); | |
ind2( [1:128]+128*(c-1) ) = fliplr([129:256] + 256*(c-1)); | |
end | |
pix_linear_interleaved = cat(1,reshape(ind1,128,256),reshape(ind2,128,256)); | |
% sort out ROIs based on the linear indices of the first pixel (top-left | |
% corner) | |
[ROIs_first_pxl ROIs_indices] = sort(diag(pix_linear(ROIs(:,1),ROIs(:,2)))); | |
ROIs = ROIs(ROIs_indices,:); | |
for r = 1:size(ROIs,1) | |
rows = ROIs(r,1):ROIs(r,1)+ROIs(r,3); | |
cols = ROIs(r,2):ROIs(r,2)+ROIs(r,4); | |
pixels = pix_linear(rows,cols); | |
pixels_read = []; precision = []; skip = []; | |
dataout = NaN(numel(rows),numel(cols),n_frames); | |
% extract data for pixels belonging to first channel | |
pixels_read = pixels(ismember(pixels,ind1)); | |
if ~isempty(pixels_read) | |
pixels_read = reshape(pixels_read,numel(find(rows <= 128)),numel(cols)); | |
precision = [num2str(size(pixels_read,1)),'*','int16']; | |
skip = frame_pxl - size(pixels_read,1) * byte_conversion; | |
% generate vector of offsets to move the beginning of a new column | |
for cc = 1:length(cols) | |
fseek(fid,offset + pix_linear(rows(1)-1,cols(cc))*byte_conversion,'bof'); | |
temp = fread(fid, [size(pixels_read,1) n_frames], precision, skip); | |
dataout(find(rows <= 128),cc,:) = temp; | |
end | |
end | |
% extract data for pixels belonging to second channel | |
pixels_read = pix_linear_interleaved(pixels(ismember(pixels,ind2))); | |
if ~isempty(pixels_read) | |
pixels_read = reshape(pixels_read,numel(find(rows > 128)),numel(cols)); | |
precision = [num2str(size(pixels_read,1)),'*','int16']; | |
skip = frame_pxl - size(pixels_read,1) * byte_conversion; | |
% generate vector of offsets to move the beginning of a new column | |
for cc = 1:length(cols) | |
fseek(fid,offset + (pixels_read(end,cc)-1)*byte_conversion,'bof'); | |
temp = fread(fid, [size(pixels_read,1) n_frames], precision, skip); | |
dataout(find(rows > 128),cc,:) = flipdim(temp,1); | |
end | |
end | |
end | |