image processing - image

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

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

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.

Related

Importing Stack of Images

So I have the code to import a stack of images, but I am getting an error: Subscripted assignment dimension mismatch.
myPath = 'E:\folder name\'; %'
fileNames = dir(fullfile(myPath, '*.tif'));
width = 1400;
height = 1050;
nbImages = length(fileNames);
C=uint8(zeros(width, height, nbImages));
for i=1:length(fileNames)
C(:,:,i)=imread(cat(2,'E:\folder name\',fileNames(i).name));
i
end
I understand that the error is originating from the for loop, but I don't know of any other way to fill in an empty matrix with images.
Your images must not be all the same size. You can handle this by using explicit assignment for the first two dimensions. This will zero-pad any images which are smaller than the rest.
im = imread(...);
C(1:size(im, 1), 1:size(im, 2), i) = im;
Also, there is a good chance that your images have multiple color channels (the third dimension), so you'll likely want to concatenate along the fourth dimension rather than the third.
C(:,:,:,i) = imread(...)
Obviously it all depends what you want to do with the images, but in general, if you want a "stack" of images (or a "stack" of anything, really), then it sounds like you should be collecting them as a cell array instead.
Also, the correct way to create safe filenames is using the fullfile command
e.g.
C = cell(1, length(nbImages));
for i = 1 : length (fileNames)
C{i} = imread (fullfile ('E:','folder name', fileNames(i).name));
end
If you really want to concatenate to a 3D matrix from your cell array, assuming you have checked this is possible, you can do this very easily using comma-separated-list generator syntax:
My3DMatrix = cat(3, C{:});

Filling Multiple Images in Matlab

I have the following code to import multiple images from one directory into a struct in Matlab, here is an example of the images.
myPath= 'E:\conduit_stl(smooth contour)\Collagen Contour Slices\'; %'
fileNames = dir(fullfile(myPath, '*.tif'));
C = cell(length(fileNames), 1);
for k = 1:length(fileNames)
filename = fileNames(k).name;
C{k} = imread(filename);
se = strel('disk', 2, 0);
C = imclose(C, se);
filled = imfill(C,'holes');
end
Though now I would like to perform a fill on all the images, later finding the centroids. However, when attempting this, an error stating: "Expected input number 1, I1 or BW1, to be one of these types: double, ... etc" I tried converting the images into double precision, though that just resulted in: "Conversion to double from cell is not possible."
This is most likely due to the structure type, the images are 'housed' in, but I have no idea concerning that.
Help on this would be greatly appreciated.
So to elaborate on my previous comments, here are a few things to change with your code:
C is not a structure but a cell array. The content of a cell array is access with {curly brackets}. If all your images are the same size, then it is more efficient to store them into a numeric array instead of a cell array. Since they seem to be logical images, your array would have 3 dimensions:
[height, width, numberofimages]
You could therefore start your code with:
myPath= 'E:\conduit_stl(smooth contour)\Collagen Contour Slices\'; %'
fileNames = dir(fullfile(myPath, '*.tif'));
%// if your images are of type uint8
C(height,width,length(fileNames)) = uint8(0);
C_filled = C; %// initialize new array to stored filled images
Also, since you are using the same structuring elements for your morphological operation on all the images, you can define it once outside the loop.
So your code could look like this:
se = strel('disk', 2, 0);
for k = 1:length(fileNames)
C(:,:,k) = imread(fileNames(k).name);
C_filled(:,:,k) = imfill(imclose(C(:,:,k), se),'holes');
end

How to make gif images from a set of images in matlab?

How to make '.gif' image from a set of '.jpg' images (say: I1.jpg, I2.jpg,..., I10.jpg) in matlab?
Ok here is a simple example. I got an image with a unicorn on it and remove 2 part to create 3 different images, just for the sake of creating an animated gif. Here is what it looks like:
clear
clc
%// Image source: http:\\giantbomb.com
A = rgb2gray(imread('Unicorn1.jpg'));
B = rgb2gray(imread('Unicorn2.jpg'));
C = rgb2gray(imread('Unicorn3.jpg'));
ImageCell = {A;B;C};
figure;
subplot(131)
imshow(A)
subplot(132)
imshow(B)
subplot(133)
imshow(C)
%// Just to show what the images look like (I removed spots to make sure there was an animation created):
%// Create file name.
FileName = 'UnicornAnimation.gif';
for k = 1:numel(ImageCell)
if k ==1
%// For 1st image, start the 'LoopCount'.
imwrite(ImageCell{k},FileName,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(ImageCell{k},FileName,'gif','WriteMode','append','DelayTime',1);
end
end
As you see, its not that different from the example on the Mathworks website. Here my images are in a cell array but yours might be in a regular array or something else.That should work fine; when I open 'UnicornAnimation.gif' it is indeed a nice animation!
Hope that helps!

Image mosaic-ing in Matlab

I am trying to do some image analysis over tiles of a big tif image. I have already done the processing required in each of these tiles and in this step I have to create one mosaic out of these tifs. I read somewhere that I can use 'cat' function for this reason. As I am not really pro in programming I found it easy and tried to apply it. The tiles I have are about 154 tifs and I tried the cat over 4 of them and it works and now I should expand it over all the files. My problem now is to apply it over all the tifs. The codes for 4 of them was:
img1 = imread ('E:...\'a1.tif','tif');
img2 = imread ('E:...\'a2.tif','tif');
img3 = imread ('E:...\'a3.tif','tif');
img4 = imread ('E:...\'a4.tif','tif');
image1 = cat(2,img1,img3);
image2 = cat(2,img2,img4);
image3 = cat(1,image2,image1);
imshow(image3)
as you see in the code two by two should be stitch horizontally and the result would be stitch vertically to have the final image. M question is how through these amount of images I define which ones should be stitch first horizontally and then the resulted images stitch vertically. I would be really thankful if you guys could help me with it. Any other approaches would be welcome.
If tiles are of the same size you could concatenate them by operating on them as if they were plain matrices:
% dots-and-linebreak used for prettier formatting
concatenated_image = [ img1, img3; ...
img2, img4 ];
All tiles till ; are concatenated horizontally in one row, and rows are concatenated vertically to form final image.
Applying this principle you could join any number of preloaded images by doing this:
NUMBER_OF_IMAGES = 152; % divisible by 4
IMAGES_PER_LINE = 4;
concatenated_image = [];
for ii = 0 : (NUMBER_OF_IMAGES/IMAGES_PER_LINE)-1
one_row = [];
for jj = 1 : IMAGES_PER_LINE
% concatenate next image in this line
one_row = [ one_row eval(['img' num2str(4*ii+jj)]) ];
end
% add constructed row to the existing image
concatenated_image = [ concatenated_image; one_row ];
end
Instead of using variables named like imgN you should prefer using Matlab's cell arrays. You could then load all the images with:
imgs = {};
for ii = 1:154
imgs{ii} = imread (['E:...\a' num2str(ii) '.tif'],'tif');
end
Then you would have to change the above concatenation code to:
...
one_row = [ one_row imgs{4*ii+jj} ];
...

Matlab: How can I display several outputs in the same image?

Let's say my image is img=zeros(100,100,3), my outputs are several ellipse which i get using a created function [ret]=draw_ellipse(x,y,a,b,angle,color,img), I can display one ellipse using imshow(ret).For the moment, I'm trying to show serval ellipse in the image. But i don't know how to code it. will ‘for loop’ work or I need to hold them?
If this is related to what you were doing in your previous question, then what you need to do is to pass the result of one iteration as input to the next.
So assuming that the function [ret]=draw_ellipse(x,y,a,b,angle,color,img) you mentioned takes as input an image img and returns the same image with an ellipse drawn on it, you could do this:
%# ellipses parameters
%#x = {..}; y = {..};
%#a = {..}; b = {..};
%#angle = {..}; color = {..};
img = zeros(200,100,'uint8'); %# image to start with
for i=1:10
img = draw_ellipse(x{i},y{i}, a{i},b{i}, angle{i}, color{i}, img);
end
imshow(img)
I'm a bit unsure of what you want. You want to show several ellipse in one image, like plotting several graphs with hold on?
There is no equivalent command for images, but a simple solution is to add the ellipses into one image and show that one:
several_ellipse = ellipse1 + ellipse2 + ellipse3;
imshow(several_ellipse)
Presumably you want to pass ret as the final input to the next call to draw_ellipse.

Resources