Reading raw image file of unknown size

조회 수: 17 (최근 30일)
Nour Aburaed
Nour Aburaed 2018년 8월 27일
댓글: Nour Aburaed 2018년 8월 30일
Hello,
I have the following code for reading a raw image file
if true
path1=pwd;
[filename, pathname] = uigetfile( ...
{'*.raw'}, ...
'Pick a Satellite Image ');
cd(pathname);
fin=fopen(filename,'r');
row=6000; col=9872;
I=fread(fin,row*col,'uint8=>uint8');
Z=reshape(I,row,col);
imshow(Z);
end
However, this reads only a fixed image size of 6000 x 9872, and not all of my images are of this size. Is there a way to make the code size-independent?

채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 27일
No. You cannot read raw image files unless you have some information about either all of the dimensions of the image, or else of all except one of the dimensions (with the remaining being calculated based upon the file size.)
In the particular case where the file size factors into exactly two prime numbers, you can deduce that the two primes are the height and the width, but you cannot tell which is the height and which is the width.
Another issue with raw image files is that they do not give you information about whether the data is stored "down" the columns (MATLAB stores arrays that way) or "across" the rows (very common for images.) For color images, it is also important to know the order of color components, and whether all of the components of each pixel are stored together in the file, or if instead the color panes are separated (MATLAB stores color images with separated color panes in memory.)

추가 답변 (1개)

Friedrich
Friedrich 2018년 8월 28일
편집: Friedrich 2018년 8월 29일
If you make some assumptions about the image, there is a way to find the height and width.
1. Image is all grayscale uint8
2. Image has no header, just pure pixel info
3. Image isn't all stripes going up-down or left-right
If these are met, your can get the width by finding the first peak in the self correlation:
[filename, pathname] = uigetfile({'*.raw'},'Pick an Image ');
fid=fopen([pathname filename],'r');
Data=fread(fid,inf,'uint8=>uint8');
fclose(fid);
% do a self correlation to find length of lines
fftData = fft(Data);
selfcorr = ifft(fftData .*conj(fftData ));
selfcorr = selfcorr -mean(selfcorr);
% find first peak in self correlation as indicator for length of line
peaks = selfcorr([2:end 1]) - selfcorr;
peaks = peaks([2:end 1]) - peaks;
ispeak = peaks < peaks([2:end 1]) & peaks < peaks([end 1:end-1]) & peaks < peaks(end)/10;
% length of line is considered width
k = numel(Data);
w = find(ispeak,1);
h = k / w;
IMG = reshape(Data,[w h]);
imshow(IMG);
These assumptions don't apply to all *.raw files, as it is a jungle of formats, most including a header. I just deduced these assupmtions from the code you provided. If you have any kind of header, the code won't work.
Edit: two typos in that code, hope it works now. Data is indeed the image data. k should be numel(Data) or length(Data)
  댓글 수: 4
Friedrich
Friedrich 2018년 8월 29일
I edited the code and it should work now.
The error "To RESHAPE the number of elements must not change." probably comes from the peak detection not working properly on that image. If you attach a sample I could check that.
To check yourself you can looks for the first peak manually with
figure
s(1) = subplot(2,1,1);
plot(selfcorr);
s(2) = subplot(2,1,2);
plot(peaks);
linkaxes(s,'x');
You have to zoom in on the left side of the graph and look for the first spike at around the width of the image. You should see a distinctive positive spike in the top plot and a negative spike in the bottom plot.
To improve robustness you can replace the line
w = find(ispeak,1);
with this:
w = find(ispeak,1);
% look for max withing 10 pixels
[~,index] = max(selfcorr(w+(-10:10)));
% use the index as best match
w = w + index - 12;
Nour Aburaed
Nour Aburaed 2018년 8월 30일
Thanks for the update. The code shows a blank figure (I cannot attach the sample raw figure, unfortunately)... I am guessing that this means the assumptions do not apply as smoothly as I thought they would, or maybe my images do not meet all the conditions.

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by