How to read a mraw file

69 views (last 30 days)
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 Comments
Mathilde Schneider
Mathilde Schneider on 1 Jul 2021
Edited: Mathilde Schneider on 1 Jul 2021
Hi Lylia,
I'm facing the same issue, and using the readmraw doesn't seems to work. Have you managed to figure it out?
Regards,
Mathilde
Phil Kreth
Phil Kreth on 31 Aug 2021
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

Sign in to comment.

Accepted Answer

Shadaab Siddiqie
Shadaab Siddiqie on 30 Apr 2021
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.

More Answers (2)

Phil Kreth
Phil Kreth on 31 Aug 2021
Edited: Phil Kreth on 9 Sep 2024
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: Jun 20, 2013
% Editor: Phil Kreth Modification Date: Sep 9, 2024
%
% 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(startsWith(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
*Note: I updated this code on September 9, 2024 to handle an edge case that was found when trying to read all images with a version 3 CIH file. Sometimes, you may have encountered an error if you saved a couple copies of the MRAW and one of those had fewer frames than an earlier copy. Loading all frames with a 0 entered for the second argument could produce an error as the code would have looked for the larger number of frames instead of the correct one.
  4 Comments
Jonny Cheng
Jonny Cheng on 31 May 2023
Hello Phil,
Thanks for your contributions on the scripts.
I have modified the code to read the cih file for a Photron SA-Z color camera. However, for the same camera with cihx file. The image shows wired color and lower resolution. Could you check my code and tell what is wrong?
function imgs = readmrawcihx2(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-4)),'r');
if fid1 < 0
fid1 = fopen(sprintf('%s.cihx',filename(1:end-5)),'r');
fid2 = fopen(sprintf('%s.mraw',filename(1:end-5)),'r');
cihx = true;
else
fid1 = fopen(sprintf('%s.cih',filename(1:end-4)),'r');
fid2 = fopen(sprintf('%s.mraw',filename(1:end-4)),'r');
cihx = false;
end
%fid2 = fopen(sprintf('%s.mraw',filename(1:end-5)),'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,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))), 'RawBayer')
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);
Phil Kreth
Phil Kreth on 13 Jun 2024
Jonny,
I know I'm super late with my reply here, but I can't find what's going on with your code unless I have an example file to work with. I don't have access to a color SA-Z, so it's challenging to guestimate what I might need to modify. Can you share a CIH(X) and MRAW with me? Anything that's got a couple of frames would be fine, and I would recommend uploading to a public resource like Google Drive.
Thanks,
Phil

Sign in to comment.


Turbulence Analysis
Turbulence Analysis on 17 Feb 2024
Hi,
With this function, I am getting the below error
Unable to perform assignment because the size of the left side is 589824-by-1 and
the size of the right side is 17977-by-1.
Error in readmraw (line 126)
imgs(:,n) = fread(fid2, Pixels, bit_depth, 0, 'b');
  2 Comments
Phil Kreth
Phil Kreth on 13 Jun 2024
Hi there, Turbulence Analysis,
Sorry for the late reply. I don't monitor this site.
It looks like the code was anticipating a much larger image than what was being fed in through the fread() command. The interesting thing about the fread() output there being 17977x1 is that 17977 is a prime number, so there's no way it could be divided up (by bit depth, by color vs grayscale, or by any width & height combination). What were the characteristics of the file you were trying to feed in (W, H, Number of Frames, Bit Depth, Color vs Grayscale)?
Best,
Phil
Clarice du Plessis
Clarice du Plessis on 3 Oct 2024
Hi Turbulence Analysis
I got the same error when I specified the filename as .cihx. Changing it to .mraw solved the issue for me.
Regards
Clarice

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!