MATLAB Answers

How to read a mraw file

조회 수: 14(최근 30일)
lylia ighmouracene
lylia ighmouracene 2021년 4월 27일
댓글: Phil Kreth 2021년 8월 31일 15:38
I have a video in .mraw and I would like to read it and exploit it but with the function I found, does not work because it is .cih and i have .cihx if someone could help me please
thank you in advance
  댓글 수: 2
Phil Kreth
Phil Kreth 2021년 8월 31일 15:38
Mathilde,
Please see my reply below. I edited SEP's readmraw.m function to handle .cihx files (as well as corner cases for newer .cih files).
Cheers,
Phil

댓글을 달려면 로그인하십시오.

채택된 답변

Shadaab Siddiqie
Shadaab Siddiqie 2021년 4월 30일
From my understanding you want to read .mraw file. You can refer to the code bellow.
I=readmraw('Photron_mraw_example',[1 10]);
for n=1:1:10
imshow(I.Images.RawImages(:,:,n),[0 3000]);
pause(.1);
end
Here readmraw function is from Photron MRAW File Reader.

추가 답변(1개)

Phil Kreth
Phil Kreth 2021년 8월 31일 15:37
Hi everyone. I have edited the readmraw function that was written by "SEP" to handle either .cih or .cihx files. The originally function is posted on the FileExchange here - https://www.mathworks.com/matlabcentral/fileexchange/42408-photron-mraw-file-reader
Below is a copy of the new readmraw.m file that you can use.
function imgs = readmraw(filename, numImgs)
% readmraw.m
% READMRAW Read Photron MRAW files into MATLAB
%
% imgs = READMRAW('C:\Photron\Filename.mraw', [a,b]) loads images a
% through b from 'C:\Photron\Filename.mraw' into matrix imgs.
%
% Remarks
% -------
% This function must be handed the common *.cih(x) and *.mraw file name
% and the range of images to be loaded (MATLAB may not handle the entire
% image range for large files).
% NOTE: Both the *.cih(x) file and the *.mraw file are utilized
% Autor: SEP Creation Date: June 20,2013
% Editor: Phil Kreth Modification Date: Aug 31, 2021
%
% Examples
% --------
% % Load all images
% imgs = readmraw('C:\Photron\Moviefile.mraw', 0);
%
% % Load images 10 through 50
% imgs = readmraw('C:\Photron\Moviefile.mraw', [10,50]);
%
% % Load image 10
% imgs = readmraw('C:\Photron\Moviefile.mraw', 10);
%
fid1 = fopen(sprintf('%s.cih',filename(1:end-5)),'r');
if fid1 < 0
fid1 = fopen(sprintf('%s.cihx',filename(1:end-5)),'r');
cihx = true;
else
cihx = false;
end
fid2 = fopen(sprintf('%s',filename),'r');
if fid1 < 1 || fid2 < 1
error(['Could not locate .CIH or .CIHX header for file: ''' filename '''']);
end
if ~cihx % CIH FILE
% Read Header Information
Header = textscan(fid1,'%s','delimiter',':');
Header = Header{1};
color_ind = find(contains(Header, 'Color Type')) + 1;
if strcmp(cell2mat(Header(color_ind(1))), 'Color')
color = true;
else
color = false;
end
bit_ind = find(contains(Header, 'Color Bit')) + 1;
bits = str2double(cell2mat(Header(bit_ind(1))));
if color
bits = bits/3;
end
bit_depth = sprintf('ubit%d', bits);
frame_ind = find(contains(Header, 'Total Frame')) + 1;
Total_Frames = str2double(cell2mat(Header(frame_ind(1))));
width_ind = find(contains(Header, 'Image Width')) + 1;
Width = str2double(cell2mat(Header(width_ind(1))));
height_ind = find(contains(Header, 'Image Height')) + 1;
Height = str2double(cell2mat(Header(height_ind(1))));
% fps_ind = find(contains(Header, 'Record Rate(fps)')) + 1;
% fps = str2double(cell2mat(Header(fps_ind(1))));
else % CIHX FILE
% Read Header Information
Header = textscan(fid1,'%s','delimiter',{'<','>'});
Header = Header{1};
color_ind = find(contains(Header, 'type')) + 1;
if strcmp(cell2mat(Header(color_ind(1))), 'Color')
color = true;
else
color = false;
end
bit_ind = find(contains(Header, 'bit')) + 1;
bits = str2double(cell2mat(Header(bit_ind(1))));
if color
bits = bits/3;
end
bit_depth = sprintf('ubit%d', bits);
frame_ind = find(contains(Header, 'totalFrame')) + 1;
Total_Frames = str2double(cell2mat(Header(frame_ind(1))));
width_ind = find(contains(Header, 'width')) + 1;
Width = str2double(cell2mat(Header(width_ind(1))));
height_ind = find(contains(Header, 'height')) + 1;
Height = str2double(cell2mat(Header(height_ind(1))));
% fps_ind = find(contains(Header, 'recordRate')) + 1;
% fps = str2double(cell2mat(Header(fps_ind(1))));
end
Pixels = Width*Height;
fclose(fid1);
% Define Image Range
if numImgs == 0 % load all the images
first_frame = 1;
frames = Total_Frames;
elseif length(numImgs) == 1 % load a single image
first_frame = numImgs;
frames = 1;
else % load a specified range of images
first_frame = numImgs(1,1);
last_frame = numImgs(1,2);
frames = last_frame-first_frame+1;
end
% Load Images
bytes_offset = (first_frame-1)*Pixels*bits/8;
if color
bytes_offset = bytes_offset*3;
end
fseek(fid2, bytes_offset, 'bof');
if bits > 8
data_fmt = 'uint16';
else
data_fmt = 'uint8';
end
if color
imgs = zeros(Pixels*3, frames, data_fmt);
for n = 1:frames
imgs(:,n) = fread(fid2, Pixels*3, bit_depth, 0, 'b');
end
imgs = [imgs(1:3:end,:); imgs(2:3:end,:); imgs(3:3:end,:)]; % separate color channels
imgs = reshape(imgs, [Width*Height 3 frames]); % reshape to separate color channels
N = [Width Height 3 frames];
imgs = permute(reshape(imgs, N), [2 1 3 4]); % standard reshape and permute
else
imgs = zeros(Pixels, frames, data_fmt);
for n = 1:frames
imgs(:,n) = fread(fid2, Pixels, bit_depth, 0, 'b');
end
N = [Width Height frames];
imgs = permute(reshape(imgs, N), [2 1 3]);
end
fclose(fid2);
Cheers,
Phil

Community Treasure Hunt

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

Start Hunting!

Translated by