Reading raw image file of unknown size
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
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 Commenti
Nour Aburaed
il 29 Ago 2018
Modificato: Nour Aburaed
il 29 Ago 2018
Walter Roberson
il 29 Ago 2018
I am not sure what k is? Looks like it should be length(I) ?
Friedrich
il 29 Ago 2018
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
il 30 Ago 2018
Categorie
Scopri di più su Images in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!