read multiple high resolution Images

Hi,
I have a set of 400 images of 1024 x 1024 resolution. I want to open each of the 400 images and extract a perticular row of each image and form a new image out of this. So the resulting number of images will be 1024 each of size 400 x 1024. I need to save these 1024 images separately.
I am able to do this but it is taking more time. I am running a for loop to read/load images from directory one by one, shredding them into a variable of size 1024 x 400 x 1024 size variable. Then running another for loop to save these images.
My question: Is there any better/faster way of doing this (like cell arrays etc or otherwise). Currently it is taking about 80 sec. (intel i-3, win10, r17a).

7 Comments

Rajesh - have you presized your "destination" array (the 1024x400x1024)? Could you put the "file saving code" into the same loop where you read the files from (and perhaps avoid having the need for the destination array):
% get list of images
% loop over each image
for k = 1:numImages
% read image
% extract subset of data
% save smaller image to file
end
Why do you have a 3-D variable? If you're extracting one row from 400 images, and stitching them together, won't the final image be just one image of 400x1024? How are you getting/wanting 1024 images?
Thanks. I am extracting all the 1024 rows. hence 1024 files. Pl refer the code below. How to make it fast.
dir_presino=uigetdir('','Select Directory With Projections');
dir_sino=uigetdir('','Select Directory to Save Sinograms');
presino_file=dir(fullfile(dir_presino,'*.mat'));
[sorted index]=qsort(presino_file,@compfunc);
presino_image=fullfile(dir_presino,'\',sorted(1).name);
Struct_aligned=load(presino_image);
presino_image=Struct_aligned.shift_image;
[row,column]=size(presino_image);
sinogram=zeros(Np,column,row); % declaring
h=waitbar(0,'Building Sinograms...');
for file=1:Np
waitbar(file/Np);
presino_image=fullfile(dir_presino,'\',sorted(file).name);
Struct_aligned=load(presino_image);
presino_image=Struct_aligned.shift_image;
for RR=1:row
sinogram(file,:,RR)=presino_image(RR,:);
end
end
close(h);
h=waitbar(0,'Saving as files...');
for R=1:row,
waitbar(R/row);
string1=num2str(R);
% Sinogram=(uint16(sinogram(:,:,R)));
% fname3=[dir_sino,'\Sinogram',string1,'.tif'];
fname3=[dir_sino,'\Sinogram',string1,'.mat'];
sino_image=sinogram(:,:,R);
save(fname3,'sino_image');
% imwrite(Sinogram,fname3);
end
close(h);
As Geoff indicates this is a problem of memmory reallocation. If you can pre-allocate your sinogram variable:
sinogram = zeros([400 1024 1024]);
before the loop then matlab will not have to grow that variable every step in the loop. Also you should be able to skip the inner loop over rows and replace that with:
sinogram(file,:,:) = presino_image.';
Obviously if the first allocation fails you'll have to divide-to-conquer and save some fraction of the sinograms, then another fraction...
HTH
Thanks. I am preallocating the memory.
sinogram=zeros(Np,column,row); % declaring
Np being number of projections (passed as variable into the function so not defined).
for 600 x 600 images it seems ok but as the size of image increases it seems to be taking longer than 'normal'.
I'll try eliminating the inner loop. Thanks.
Is there any other optimisation possible?
My bad missing that line.
sinogram becomes a rather large variable. I encountered similar slowing-downs when some 3-D arrays started to grow. This might be a memmory-issue.
If this becomes "punitively slow" you might have to resort to extracting sinograms from the first half then the second half of your files, and then concateneat the first-half and second-half sinogram-slices together file-by-file. This is an uggly-uggly thing to have to do, but might save you time.
HTH

Sign in to comment.

Answers (0)

Asked:

on 21 Apr 2020

Commented:

on 22 Apr 2020

Community Treasure Hunt

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

Start Hunting!