How to read a mraw file
조회 수: 83 (최근 30일)
이전 댓글 표시
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
Mathilde Schneider
2021년 7월 1일
편집: Mathilde Schneider
2021년 7월 1일
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
2021년 8월 31일
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
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
댓글 수: 0
추가 답변 (2개)
Phil Kreth
2021년 8월 31일
편집: Phil Kreth
2024년 9월 9일
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.
댓글 수: 5
Phil Kreth
2024년 6월 13일
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
Phil Kreth
2025년 3월 19일 20:37
So, I just found out what's going on. Apparently, the "type" can be more than just "Mono" or "Color". The other options are "Raw" and possibly "RawBayer". I was unaware of the "Raw*" formats until I came across one today. One of our 12-bit mono files got saved out of PFV4 with the "Raw" option checked under the MRAW options dialog. Reopening this file in PFV4 itself actually displayed the images incorrectly even.
I am not sure how to handle these formats as even PFV was not displaying them properly. If you come across this error, drag and drop your cihx file into MATLAB to view its contents. Not far down from the top, you will see lines similar to these:
<colorInfo>
<type>Mono</type>
<bit>12</bit>
</colorInfo>
The <type>XXXX</type> line is what I was referring to. The mrawreader() function is only expecting that type to be "Color" or "Mono". If your camera is a color camera, you can change "Raw" or "RawBayer" to "Color" and it will work.
Cheers!
Turbulence Analysis
2024년 2월 17일
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
Phil Kreth
2024년 6월 13일
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
2024년 10월 3일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!