pam
pam

Reputation: 37

image processing

I have this set of images as shown, I found the centroid of each by using the code below, now I stored the new images in Im01,Im02,Im03, and they are all N by N matrix images.

Im1 = imread('image1.png');
[x,y] = ait_centroid(Im1);
Im01=circshift(Im1, [-1 -5]);
[x,y] = ait_centroid(Im01);
Im01=uint16(Im01);

Im2 = imread('image2.png');
[x,y] = ait_centroid(Im2);
Im02=circshift(Im2, [-2 -4]);
[x,y] = ait_centroid(Im02);
Im02=uint16(Im02);

Im3 = imread('image3.png');
[x,y] = ait_centroid(Im3);
Im03=circshift(Im3, [-3 -5]);
[x,y] = ait_centroid(Im03);
Im03=uint16(Im03);

my challenge is how to add this images using iteration cos i hav a large set of images(not jst the 3 images) im working on.I was able to add them manually n show the mean by doin this

G=imadd(Im01,Im02,'uint16');
G=imadd(G,Im03,'uint16');
imshow(uint8(G/3),[]);

and it worked.But when I tried iterating by doin this

G=imadd(Im01,Im02,'uint16');
for i=1:1:3
G=imadd(G,Im(i),'uint16');
end

I get error, I also tired to define the images as a matrix of a matrix by

H = [ [Im01] [Im02] [Im03] ]
G=imadd(Im01,Im02,'uint16');
for i=1:1:3
G=imadd(G,H(i),'uint16');
end

error indicates in H(i).

The code is in Matlab

Upvotes: 2

Views: 936

Answers (2)

Jonas
Jonas

Reputation: 74940

H = [ [Im01] [Im02] [Im03] ] does not create an array of matrices in Matlab. Instead, it catenates the three images into a single array. What you want to do is create a cell array, i.e. the following will work:

%# curly brackets to construct cell array
H = {Im01, Im02, Im03 }; 
G=H{1}; %# initialize G
for i=2:1:3 %# start at img#2
   G=imadd(G,H{i},'uint16');
end

Alternatively, if all you want to do with the images is to add them, you can do the following:

%# in case you want to store the images
images = cell(numberOfImages,1);

%# loop over all images
for iImg = 1:numberOfImages
   tmp = imread(sprintf('image%i.png',iImg));
   [x,y] = ait_centroid(tmp);
   Im01=circshift(tmp, [x y]); %# I assume that's what you want?
   [x,y] = ait_centroid(Im01); %# not sure what you do this for
   if iImg ==1
      G = uint16(tmp);
   else
      G = imadd(G,uint16(tmp));
   end
   %# maybe you want to store your images
   images{iImg} = uint16(tmp);
end

Upvotes: 1

Ghaul
Ghaul

Reputation: 3330

You can use sprintf and eval to iterate over the names of your images.

G=imadd(Im01,Im02,'uint16');
for i=1:1:3
   Im_name = sprinft('Im0%i',i);    
   G=imadd(G,eval(Im_name),'uint16');
end

The sprintf function will add the number i behind the string 'Im0', so you will first get Im01, then Im02, etc.. The eval function is needed to interpret these strings into Matlab varibales.

EDIT: I think the best way to avoid problems like this is to save your images in a cell from the beginning. That is, when reading in the images, read them into one cell

Im{1} = imread('Im01.xxx')
Im{2} = imread('Im02.xxx')
etc...

you can then easily iterate over the different images without using strings.

Upvotes: 0

Related Questions