How to sum 20 images in a loop

10 Ansichten (letzte 30 Tage)
Ivan Shorokhov
Ivan Shorokhov am 9 Dez. 2014
Bearbeitet: Ivan Shorokhov am 14 Jan. 2015
Hello everybody I'm wondering how I can sum more than 15 images in a loop?
Here is the code:
imgs=<500x500x20 uint8> % 20 images
NI=size(imgs,3); % NI = Number of Images (i.e. = 20)
for e=2:NI % e = from 2 to 20 with step 1 (i.e 2,3,4....20)
I1(:,:,e-1)= imgs(:,:,1)-imgs(:,:,e); % subtract all other 19 images form the first image
figure; imshow(I1(:,:,e-1),[]); % plot results of each single subtractions
end
% Now I want to sum all subtracted images, how can I do it?
I have tried:
for i=1:size(I1,3)
I2= sum(I1(:,:,i));
end
for i=2:size(I1,3)
i3= imadd(I1(:,:,i),I1(:,:,i-1));
end
Can anyone give me line of code how to do it?

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 10 Dez. 2014
Ivan - as all of your images are already of dimension 500x500, what is the imresize supposed to accomplish since the code seems to be trying to resize the images to the same dimension?
As for summing the 19 images, you want to just iterate over each one and add to the previous. Something like
% initialize the image sum to the first image in the array
I2 = I1(:,:,1);
% now sum with the subsequent 18 images
for k=2:size(I1,3)
I2 = I2 + I1(:,:,k);
end
Or, if you want to use imadd, then do
% initialize the image sum to the first image in the array
I2 = I1(:,:,1);
% now sum with the subsequent 18 images
for k=2:size(I1,3)
I2 = imadd(I2,I1(:,:,k));
end
The problem with your code, in both examples, is that you never re-use the I2 or i3 in subsequent additions, and so they are initialized with the last pair of matrices (or single matrix) only. As for using sum, this operation will sum the elements along one dimension which is not what you want.
Note that if you just use regular addition or imadd, if the summed value exceeds the range of the uint8 type, then it will be truncated to that maximum value. For example,
uint8(254) + uint8(42)
returns a value of
255
  6 Kommentare
Image Analyst
Image Analyst am 8 Jan. 2015
And I'm still baffled, Ivan, as to why you even want to use a loop at all instead of just using the sum() function like I suggested. It's especially baffling since imadd will clip, like it says in the documentation "If X and Y are integer arrays, elements in the output that exceed the range of the integer type are truncated, and fractional values are rounded." Did you know that? sum() does not have that clipping problem, so tell me why you're using imadd() and not sum().
Ivan Shorokhov
Ivan Shorokhov am 14 Jan. 2015
Bearbeitet: Ivan Shorokhov am 14 Jan. 2015
Dear Image Analyst, sorry for making you baffled, initially I wanted to use a loop to add extra functions inside the loop. But now I realised that you are right, and I will use the sum(), instead of inimadd(). Thank you to both of you for your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 10 Dez. 2014
Bearbeitet: Image Analyst am 10 Dez. 2014
Did you try not using a loop and simply using the sum function?
I2 = sum(double(I1), 3);
??? What you are doing is taking a slice, and summing the rows along the columns, so you have a row vector where each element is the sum along the columns.

Community Treasure Hunt

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

Start Hunting!

Translated by