views:

1187

answers:

5

Hello,

I have bmp images in image folder on my computer.I named it from 1.bmp to 100.bmp .All of them size is 576*768

I read one by one these hundered images.I select rectangular region from all hundred images.Rectangular region's pixel coordinates is changing from 182 to 281 vertically and changing 426 to 639 horizontally.I save graphics which represent pixel values exchange between images in all pixel coordinates in the rectangular region to another file

My m file is below:

pixvalue=zeros(100);
j=1 ;% will represent pixel coordinate sequence 
%  find pizel coordinates in rectangular region 
 for y=182:281
       for x=426:639
           for i=1:100
          % read images
        s = sprintf('C:\\images\\%d.bmp', i);  
        A = imread(s);
        A=double(A);
        pixvalue(i)= A(y,x);
        end
       s2=sprintf('%d.plot', j);
      f=figure('visible','off'),
      plot(pixvalue);
      xlabel('images');
      ylabel('pixvalue');
      title(s2);
      s3=sprintf('C:\\registration\\%d.bmp', j);

      %% save figures as 8 bit bitmap to file
      print(f,'-dbmp256',s3);
      j=j+1;
       end
   end

Unfortunately this code have been working vey vey slow!! How can I accelerate it?

Best Regards...

+1  A: 

you are reading the same image by (281-182)*(639-426) times..

maybe you should read all the images before this loop once. store it in some variable.

than you should do what you have to do..

something like:

for i=1:100
    % read images
    s = sprintf('C:\\images\\%d.bmp', i);  
    A(i) = imread(s);
end

for x=...
    for y=...
        for i=1:100
            pixvalue(i)= A(i, y, x);
        end
    end

        ..
        ..


actually i dont remember the matlab syntax very well but you have to read all the imagesin one loop before this big loop. here i corrected the code.

than in big loop you use A(i) instead of A.

ps. by the way i optimized it as if the previous code is working.. i dont have a matlab now to try..

ufukgun
ufukgun, there is a problem here. The images returned by IMREAD are 2-D (or potentially 3-D) arrays. You would have to store them in a multidimensional array or cell array... something like this for 2-D images: A(:,:,i) = imread(s);
gnovice
yes you are right. but i think it should automatically store it in multidimensional array. actually i dont have matlab on my computer now, so i am answering as i remember..
ufukgun
A: 

Could you give an example please ?How can I read all the images befroe this loop once? Is there a function in Matlab?

yalcin
Don't add answers like this. Put them in a comment on the other answer.
gnovice
A: 

This code above can't work in Matlab:

for i=1:100
    % read images
    s = sprintf('C:\\images\\%d.bmp', i);  
    A[i] = imread(s);
    A[i] = double(A);
end

Is there another function in Matlab?

yalcin
Try writing A(i) = imread(s); that is, to use round brackets.And please write your remarks as comments to the answer, not as another answer.
Liran Orevi
Thanks Liran But ıt can't work too.How can I implement this?
yalcin
WOW
Adam
+1  A: 

Your code can be broken into two parts. First, you want to load the image data and save the pixel values from a subregion of each image. This can be done with the following code:

subRegion = zeros(100,214,100);  % 3-D matrix to store image subregions
for i = 1:100,
  s = ['C:\images\' int2str(i) '.bmp'];      % Create file name string
  A = double(imread(s));  % Load image and convert to double precision
  subRegion(:,:,i) = A(182:281,426:639);    % Store subregion of image
end

Next, it appears that you want to plot the values for each pixel across all the images and output the plot to a file. This is a lot of image files (21,400!) and is bound to take a while to run. If you are sure you want to do this, here's one way:

j = 1;  % Pixel index
for y = 1:100,  % Loop over rows
  for x = 1:214,  % Loop over columns
    hFigure = figure('Visible','off');
    data = subRegion(y,x,:);  % Get pixel value from all 100 images
    plot(data(:));
    xlabel('images');
    ylabel('pixvalue');
    title(['Plot ' int2str(j)]);
    outFile = ['C:\registration\' int2str(j) '.bmp'];
    print(hFigure,'-dbmp256',outFile);  % Save figure
    j = j+1;
  end
end
gnovice
plot(subRegion(y,x,:)); % Plot pixel value from all 100 imagesin here matlab gave this error:error using ==> plotData may not have more than 2 dimensions
yalcin
I corrected the code. The problem was that PLOT doesn't recognize a 1-by-1-by-3 matrix as a vector, so it needs to be reshaped first.
gnovice
+1  A: 

I made a video on how to process a subset of files from a directory. This should cover the part about looping through the directory.

http://blogs.mathworks.com/videos/2008/02/26/matlab-basics-getting-a-directory-listing/

MatlabDoug