Independent Component Analysis as Images - image

I have applied Independent Component Analysis on set of face images using FastICA. I have successfully retrieved independent components and mixing matrix. The values of independent components are in double and I want to display these components as images like available on web, e.g., http://research.ics.aalto.fi/ica/imageica/. I don't know how to show this.

One way you might do this more automatically is with the montage function. Or you can look at subimage. See also this StackOverflow question and answer.

Try the follwing function
function [XX,fh]=dispImgs(X,cols,gap,ihw,fh)
% Courtesy A. Leonardis, D. Skocaj
% see http://vicos.fri.uni-lj.si/danijels/downloads
[M,N]=size(X);
if nargin<2 cols=floor(sqrt(N)); end;
if nargin<3 gap=0; end;
if nargin<4 ihw=[sqrt(M),sqrt(M)]; end;
if nargin<5 fh = figure; end; % new figure
ih=ihw(1);iw=ihw(2);
maxv=max(X(:));
rows=floor(N/cols);
XX=zeros((rows*ih)+(rows-1)*gap,(cols*iw)+(cols-1)*gap)+maxv;
for i=1:N
a=(iw+gap)*mod(i-1,cols)+1;
b=(iw+gap)*mod(i-1,cols)+iw;
c=(ih+gap)*(floor((i-1)/cols))+1;
d=(ih+gap)*(floor((i-1)/cols))+ih;
XX(c:d,a:b)=reshape(X(:,i)',ih,iw);
end;
xxmax=max(XX(:));
xxmin=min(XX(:));
fh = figure(fh);
imshow((XX-xxmin)/(xxmax-xxmin));
axis off;
Example:
X: imsize by N
dispImgs( X, 8, 4, imsize );%show all N images in 8 columns with Gap=4

Related

What is the syntax of ImageResize()

I have data that change in size and want to display them in the same window. The command
void ImageResize( BasicImage im, Number num_dim, Number... )
seems like a potential fit, but the syntax is not clear at all.
Let's say I have 512x5 data set and now it needs to be 367x5.
The , Number...) indicates that this command takes a different number of parameters, all of them interpreted as number parameters. Commands which do this, usually use one of their other parameters to specify how many such parameters follow.
A typical example for this is also the SliceN command.
In this particular case, the command not only allows you to change the size of the dimensions in the image, but also the number of dimensions. It is a very useful command to f.e. change a 2D image into a 3D stack or the like.
The command ImageResize( BasicImage im, Number num_dim, Number... ) does several things:
It replaces im in-place, so the meta-data, display and window remains the same
It adjusts the dimension calibration when the dimension size is changed. Here, the assumption is, that the field-of-view before and
after the resize is the same. (The command can be used to easily scale
images as shown in the example below.)
All values of the image im are set to zero. ( If you need to keep the values, you need to act on an image clone!)
Example 1: Resizing image with bilinar interpolation
image before := GetFrontImage()
number sx, sy
before.GetSize(sx,sy)
number factor = 1.3
image after := before.ImageClone()
after.ImageResize( 2, factor*sx, factor*sy ) // Adjusts the empty container with meta-data
after = warp(before, icol/factor, irow/factor ) // interpolate data
after.ShowImage()
Example 2: Extend 2D image into 3D stack
number sx = 100
number sy = 100
image img := RealImage("2D",4,sx,sy)
img = iradius* Random()
img.ShowImage()
OKDialog("Now into a stack...")
number sz = 10
img.ImageResize(3,sx,sy,sz) // All values are zero now!
img = iradius * Random()

Define 2 plotted lines in an image as variables inside a function in Matlab?

Function [Yupper, Ylower] = Segmentation(A)
A=imread('g16.BMP');
AR=A(:,:,1);
[rows, columns] = size(AR);
avgs = mean(AR(50:165,:), 2);
avgs2 = mean(AR(165:315,:), 2);
[~,ind]= max(abs(diff(avgs)));
[~,ind2]= max(abs(diff(avgs2)));
figure, image(AR,'CDataMapping','scaled'); colormap('gray'); hold on;
Yupper=plot([1 size(AR,2)], [ind+50 ind+50], 'r', 'LineWidth', 2);
Ylower=plot([1 size(AR,2)], [ind2+165 ind2+165], 'g', 'LineWidth', 2);
end
Above is code used to segment grey scale images based on large instensity changes,I'm looking to declare the 2 plotted lines as variables so I can carry out future processing. However, If I type in whos , the lines appear as follows in the table;
YL 1x1 112 matlab.graphics.chart.primitive.Line
YU 1x1 112 matlab.graphics.chart.primitive.Line
if I remove the the plot commands I receive errors associated with brackets etc and if I manipulate the code any further it seems to mess up the correctly plotted data, can anyone help?
If what you want is to delete specific data for a figure you can do it accessing the Xdata and Ydata properties of the figure handle. You can do it in Matlab R2014a or older by:
set(YL,'XData',[])
set(YL,'YData',[])
or in Matlab R2014b or newer by:
YL.XData=[];
YL.YData=[];
Example in R2013b:
clear;clc;
y=rand(20,1)*20;
x=1:20;
img=rand(20,20);
figure
hold on
image(img,'CDataMapping','scaled'); colormap('gray');
hp=plot(x,y);
% emulating your code delay
pause(2);
set(hp,'XData',[])
set(hp,'YData',[])

how to store the image immediately after reading it in a for loop?

I tried the following program for reading multiple images(about 300 images). Now I want to store these images immediately after reading each to some location by some name as g1,g2,g3... Is it possible to do this in a loop?
Here is my attempt:
for i=1:5
m=imread(['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg']);
figure,imshow(m);
end
I highly recommend that you store them in a cell array:
for k=1:5
image_path = ['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg']; %// I have moved this to be on its own line as it will make debugging easier. You don't have to, but I think it's a good idea.
images_all{k} = imread(image_path);
end
By using eval to create variable names like g1, g2 etc you pollute your workspace with an unmanageable amount of variables. Plus if they are all in a cell array then it's really easy to apply the same function to each of them either in a loop or with cellfun.
For example if you want to convert them all to greyscale now:
images_grey = cellfun(#rgb2gray, images_all, 'UniformOutput', false);
You can simply save all them into one big matrix:
for i=1:5
images_all(:, :, :, i) = imread(['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg'])
end
After this, all images will be stored in images_all (here assume that all images are colored images, i.e. 3 channels).
Try this -
for i=1:5
img =imread(['C:\Users\shree\Desktop\1\im' num2str(i) '.jpg']);
evalc(['g' num2str(i) '=img;']);
end
figure,imshow(g1);
figure,imshow(g2);
Another approach could be to use STRUCT and store those images as fields of a struct.
Storing as a 4D matrix is another efficient way as suggested by herohuyongtao.

matlab: texture classification

I have a histology image like this:
From the image, we can observe there are two kinds of different cells.
and
Is there any way that I can separate these two types of cells into two groups?
How about using your raw image and previous code to achieve this?
% % % your old code
I=imread(file);
t1=graythresh(I);
k1=im2bw(I,t1);
k1=~k1;
se = strel('disk',1);
k0=imfill(~k1,'holes');
cc = conncomp(k0);
k0(cc.PixelIdxList{1})=0;
k1=imfill(k1,'holes');
mask=k0 | k1;
%%%%%%%%%%%%%%%%%%
This will give you:
I=rgb2hsv(I);
I=double(I);
I1=I(:,:,1); % again, the channel that can maximizing the margin between donut and full circle
Imask=(I1-0.2).*(I1-0.9)<0;
k2=mask-Imask;
k2=bwareaopen(k2,100);
This will give you:
k2=mask-Imask;
I2=zeros(size(I1,1),size(I1,2),3);
I2(:,:,1)=(k2==1)*255;
I2(:,:,3)=((I1-0.2).*(I1-0.9)<0)*255;
imshow(I2)
will finally give you (the two types are stored in two channels in the rgb image):
I would use regionprops
props=regionprops(YourBinaryImage, 'Solidity');
The objects with a high solidity will be the disks, those with a lower solidity will be the circles.
(Edit) More formally:
I=imread('yourimage.jpg');
Bw=~im2bw(I, 0.5);
BWnobord = imclearborder(Bw, 4); % clears the partial objects
Props=regionprops(BWnobord, 'All');
solidity=cell2mat({Props.Solidity});
Images={Props.Image};
Access the elements of Images where the value in solidity is higher than 0.9 and you get your disks. The circles are the other ones.
Hope it helps

Add two images in MATLAB

I am trying to overlay an activation map over a baseline vasculature image but I keep getting the same error below:
X and Y must have the same size and class or Y must be a scalar double.
I resized each to 400x400 so I thought it would work but no dice. Is there something I am missing? It is fairly straight forward for a GUI I am working on. Any help would be appreciated.
a=imread ('Vasculature.tif');
b = imresize (a, [400,400]);
c=imread ('activation.tif');
d= imresize (c, [400,400]);
e=imadd (b,d);
Could it be the bit depth or dpi?
I think one of your images is RGB (size(...,3)==3) and the other is grayscale (size(...,3)==1). Say the vasculature image a is grayscale and the activation image c is RGB. To convert a to RGB to match c, use ind2rgb, then add.
aRGB = ind2rgb(a,gray(256)); % assuming uint8
Alternatively, you could do aRGB = repmat(a,[1 1 3]);.
Or to put the activation image into grayscale:
cGray = rgb2gray(c);
Also, according to the documentation for imadd the two images must be:
nonsparse numeric arrays with the same size and class
To get the uint8 and uint16 images to match use the im2uint8 or im2uint16 functions to convert. Alternatively, just rescale and cast (e.g. b_uint8 = uint8(double(b)*255/65535);).
Note that in some versions of MATLAB there is a bug with displaying 16-bit images. The fix depends on whether the image is RGB or gray scale, and the platform (Windows vs. Linux). If you run into problems displaying 16-bit images, use imshow, which has the fix, or use the following code for integer data type images following image or imagesc:
function fixint16disp(img)
if any(strcmp(class(img),{'int16','uint16'}))
if size(img,3)==1,
colormap(gray(65535)); end
if ispc,
set(gcf,'Renderer','zbuffer'); end
end
chappjc's answers is just fine, I want to add a more general answer to the question how to solve the error message
X and Y must have the same size and class or Y must be a scalar double
General solving strategy
At which line does the error occur
Try to understand the error message
a. "... must have the same size ...":
Check the sizes of the input.
Try to understand the meaning of your code for the given (type of) input parameters. Is the error message reasonable?
What do you want to achieve?
Useful command: size A: returns the size of A
b. "... must have the same class ...":
Check the data types of the input arguments.
Which common data type is reasonable?
Convert it to the chosen data type.
Usefull command: whos A: returns all the meta information of A, i.e. size, data type, ...
Implement the solution: your favorite search engine and the matlab documentation are your best friend.
Be happy: you solved your problem and learned something new.
A simple code :
a=imread ('image1.jpg');
b=imresize (a, [400,400]);
subplot(3,1,1), imshow(b), title('image 1');
c=imread ('image2.jpg');
d= imresize (c, [400,400]);
subplot(3,1,2), imshow(d), title('image 2');
[x1, y1] = size(b) %height and wedth of 1st image
[x2, y2] = size(d) %height and wedth of 2nd image
for i = 1: x1
for j = 1: y1
im3(i, j)= b(i, j)+d(i, j);
end
end
subplot(3,1,3), imshow (im3), title('Resultant Image');

Resources