开发者

How to average multiple images in matlab?

开发者 https://www.devze.com 2023-02-26 07:06 出处:网络
I am trying to get the average image from 1000 frames. Each image is 512 by 512 pixel size.The file I got from the experiments is a tiff unit 16 data.

I am trying to get the average image from 1000 frames.

  • Each image is 512 by 512 pixel size.The file I got from the experiments is a tiff unit 16 data.
  • the tiff file contains 1000 frames of the same spot.

I was thinkin开发者_如何学运维g writing a m file where I read out 1000 frames from the tiff file then average them but it seems will eat up the memory very fast.

What is the better way to get the average image of these 1000 frames. If the only way is to average them after load all the frames into matlab, how should I average over 1000 frames? Thanks.


try the following:

a=zeros(512);
for i=1:1000
    a=a+frame(i);
end
a=a/1000;

a is the average of the frames.


After each image is read you can accumulate it in the temporary variable, i.e. add current image to this variable at each step. After you read all images the accumulator will store the sum of all images. Finally, divide it by the number of images and you will get final image.

But it is significant that images are usually stored as uint8 (unsigned 8-bit integer). And if it is summed up, the overflow will occur. To prevent this accumulator should be e.g. uint32 or double. If you want the final image to be uint8, an explicit convertion is required.


% slight modiifcations to the last answer
if the files are named image1.tif, image2.tif,....image1000.tif)
im = imread('image1.tif');
for i = 2:1000
im = imadd(im,imread(sprintf('image%d.tif',i)));
end
im = im/1000;
imshow(im,[]);

The problem gets interesting when you have names like image00001, image00002,....image00010,...image00100,.... here just read from 2-10 in one loop 11-99 in another and so so...hope it helps


source = 'D:\file_path\'; %'
im_number=262; % image number should not exceed 16 843 009 images
sum=uint32(imread([source,'image1.bmp'],'bmp')); % converts image array to Unsigned 32-bit integer to escape overflow
  for n=2:im_number;
      sum=imadd(sum,uint32(imread([source,'image', num2str(n),'.bmp'],'bmp'))); % perform addition without overflow
%      using images specific function imadd
  end;
  sum = imdivide(sum,im_number); % performing normalization of addition using images specific function imdivide
  imshow(uint8(sum),[]); % displais the image converted back to Unsigned 8-bit integer
  imwrite(uint8(sum),[source,'sum_image.bmp'],'bmp');  % saves the averaged image


% assuming there are files named: '1.tif','2.tif',...'1000.tif'
im = imread('1.tif');
for i = 2:1000
 im = imadd(im,imread(sprintf('%d.tif',i)));
end
im = im/1000;
imshow(im,[]);


J=0;
for i =1:index %index is the number of images to be averaged
    I=uint32(imread(['frame',num2str(i),'.bmp']));
    J=imadd(I,J);
end
J=floor(J/index);
J=uint16(J); %J is the required image
0

精彩评论

暂无评论...
验证码 换一张
取 消