Reading raw image file of unknown size

20 次查看(过去 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