Sliding window for matlab - matlab

The intention is to estimate in a [3 3] sliding window. 0.5*[(A(i)-A(5))^2] is computed where a(i) is the pixels around center pixel a(5).
The mean of each of these 8 half square differences is stored in the center pixel's location.
To tackle this conv2 and nlfilter were used on a training example matrix as follows.
clc;
close all;
clear all;
A = [1 2 3 4 5 6;5 4 6 3 2 1;2 3 2 1 4 5];
kernel = [-1 -1 -1; -1 8 -1; -1 -1 -1];
outputImage = conv2(A, kernel);
fun = #(x) mean(x(:));
B= nlfilter (outputImage,[3 3],fun);
The initial idea was to calculate the difference and store it in the location of the pixels instead of the center pixel. Then use a sliding window to take mean of those differences and replace the center pixel.
It was apparent that my logic was flawed.
Though i was able to calculate the difference(i first tried to see simple difference was possible for me) i had to deal with data being overwritten. moreover this method will create a matrix larger than the original which is also wrong.

the function mean and the kernel you are using are both linear and do not represent the non-linear operation you are trying to achieve.
One way of using conv and mean is by computing the 8 differences as different output channels
ker = cell(1,8);
for ii=1:8
ker{ii} = zeros(3);
ker{ii}(2,2) = 1; %// for a(5)
if ii < 5
ker{ii}(ii) = -1;
else
ker{ii}(ii+1) = -1;
end
end
interim = zeros( [size(A,1) size(A,2), numel(ker)] ); % allocate room for intermidiate results
for ii=1:numel(ker)
interim(:,:,ii) = conv2(A, ker{ii}, 'same' ); %//'same' takes care of output size for you
end
Now interim holds each of the different a(5)-a(i) we are ready for the non-linear operation
interim = interim.^2;
B = 0.5 * interim;

Related

How to do data dimensionailty reduction?

I have a set of 25 images of label 'Infected' and 25 images of label 'Normal'.
I am trying to extract the dual-tree complex wavelet transform based coefficients as features for each of the images.
My code to obtain coefficients using DT-CWT ia as follows:
I = imread('infected_img1.jpg'); %read image
I = rgb2gray(I); %rgb ro gray-scale
L = 6; %no. of levels for wavelet decomposition
I = reshape(I',1,size(I,1)*size(I,2)); %change into a vector
I = [I,zeros(1,2^L - rem(length(I),2^L))]; %pad zeros to make dim(I) a multiple of 2^L
I = double(I);
dt = dddtree('cplxdt',I,L,'dtf3'); %perform DT-CWT
dt_Coeffs = (dt.cfs{L}(:,:,1) + 1i*dt.cfs{L}(:,:,2)); %extract coefficents at Level 6
Now, since I have 24 more images to extract coefficients from, I do this block for each of the images. My ultimate aim is to append all coefficient vectors generated in each iteration to form a matrix. But each image tends to give a different sized coefficient vector.
I want to know about some dimension reduction method that can reduce each vector to a uniform size and at the same time preserve its information.
Can anyone suggest methods with a good amount of clarity?
As I mentioned in my comment,
You can't shrink something (i.e. remove information) and still preserve all of the information.
Instead you can pad all of the vectors to the length of the largest vector and then concatenate them to create a matrix. You could program your own method but in the spirit of not reinventing the wheel I've previously used padcat(). It is very easy to use and pads with NaN but you can easily change this to 0.
Here's an example usage which also contains a handy conversion from NaN to 0.
>> a = [1 2 3 4];
>> b = [1 2 3];
>> c = padcat(a, b);
c =
1 2 3 4
1 2 3 NaN
>> c(isnan(c)) = 0
c =
1 2 3 4
1 2 3 0

How can I plot filled rectangles as a backdrop for a desired target in MATLAB?

I have two datasets, one of which is a target position, and the other is the actual position. I would like to plot the target with a +/- acceptable range and then overlay with the actual. This question is only concerning the target position however.
I have unsuccessfully attempted the built in area, fill, and rectangle functions. Using code found on stackoverflow here, it is only correct in certain areas.
For example
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
X=[t,fliplr(t)]; %create continuous x value array for plotting
Y=[y1,fliplr(y2)]; %create y values for out and then back
fill(X,Y,'b');
The figure produced looks like this:
I would prefer it to be filled within the red boxes drawn on here:
Thank you!
If you would just plot a function y against x, then you could use a stairs plot. Luckily for us, you can use the stairs function like:
[xs,ys] = stairs(x,y);
to create the vectors xs, ys which generate a stairs-plot when using the plot function. We can now use these vectors to generate the correct X and Y vectors for the fill function. Note that stairs generates column vectors, so we have to transpose them first:
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
X=[ts.',fliplr(ts.')]; %create continuous x value array for plotting
Y=[ys1.',fliplr(ys2.')]; %create y values for out and then back
fill(X,Y,'b');
Again, thank you hbaderts. You answered my question perfectly, however when I applied it to the large data set I needed for, I obtained this image
https://dl.dropboxusercontent.com/u/37982601/stair%20fill.png
I think it is because the fill function connects vertices to fill?
In any case, for the potential solution of another individual, combined your suggested code with the stair function and used the area function.
By plotting them on top of one another and setting the color of the lower area to be white, it appears as the rectangular figures I was after.
%sample code. produces image similar to o.p.
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1];
y1 = y+1;
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
area(ts,ys1,'FaceColor','b','EdgeColor','none')
hold on
area(ts,ys2,'FaceColor','w','EdgeColor','none')
https://dl.dropboxusercontent.com/u/37982601/stair%20area.png
Thanks again for your help and for pointing me in the right direction!

how to take an average of a 2x2 pixel block?

I have an image and I have to take the average of the color of 2x2 pixel.
take the average color of a 2x2 pixel block, then compute the distance between the colors in the image and the colors available for our use.
I have no idea what taking the average of 2x2 pixel block means. How to solve this?
Thank you.
You can process non-overlapping blocks of an image using blockproc
Im = imread('coins.png'); %// example image
fun = #(block_struct) mean( block_struct.data(:) ); %// anonymous function to get average of a block
B = blockproc(Im,[2 2],fun); %// process 2 by 2 blocks
imshow(B,[]); %// show resulting image
One other method I can suggest is to use combination of colfilt with the 'sliding' flag and mean as the function to operate on. The 'distinct' flag is what you actually need to use, but if you see the conversation between myself and #eigenchris, we couldn't get it to work. Still, eigenchris has shown that this is 300x faster than blockproc.
Therefore, assuming your image is stored in im, you can simply do:
out = uint8(colfilt(im, [2 2], 'sliding', #mean));
out2 = out(1:2:end,1:2:end);
The reason why you would need to subsample the results is because when we apply a sliding option, you have overlapping blocks processing the image at a time. Because you want distinct blocks, you only need 1/4 of the image because you have decomposed the image into 2 x 2 blocks. Within a 2 x 2 block, if you did the sliding option, you would have three other results that are not required, and so doing the subsampling above by a factor of two eliminates those three other blocks that give results.
Note that you'll need to cast the result as the output will be of type double.
Going with the discussion between myself and eigenchris, you can replace colfilt with the canonical imfilter to replicate the first line of the above code. I managed to get an 8x speedup on my machine when comparing the two together. Therefore:
out = imfilter(im, [0.25 0.25; 0.25 0.25], 'replicate');
out2 = out(1:2:end,1:2:end);
In terms of speedup, I wrapped each call in an anonymous function, then used timeit to time the functions:
>> f = #() uint8(colfilt(im, [2 2], 'sliding', #mean));
>> g = #() imfilter(im, [0.25 0.25; 0.25 0.25], 'replicate');
>> T = timeit(f);
>> T2 = timeit(g);
>> T/T2
ans =
7.5421
As you can see, there is roughly a 8x speedup over colfilt... most likely because it's calling im2col and col2im under the hood.
Using loops: (another method, just for knowing)
A = imread('cameraman.tif');
i = 1; j = 1;
[rows, cols] = size(A);
C(rows/2,cols/2) = 0;
for x = 1:2:rows
for y = 1:2:cols
block = [A(x,y),A(x+1,y),A(x,y+1),A(x+1,y+1)];
C(i,j) = mean(block(:));
j = j+1;
end
i = i+1;
j = 1;
end
C = uint8(C);
figure;
imshow(A); %// show original image
figure;
imshow(C); %// show resulting image

finding spectral radius of the jacobi iteration matrix

I am using Matlab to find the spectral radius of the Jacobi iteration matrix where A=[4 2 1;1 3 1;1 1 4].
I can't seem to input the correct commands to get the size of the error after 5 iterations. Can someone help me?
Here are a list of commands that I put into Matlab so far:
A=[4 2 1;1 3 1;1 1 4]
A =
4 2 1
1 3 1
1 1 4
D=diagonal(diagonal(A));L=(A,-1);U=(A,1);
b=([3 -1 4])
x0j=zeros([0 0 0]);
x=D\(-(U+L)*x0j+b);r=b-A*x %Jacobi iteration.
------------------------------------------------------------------------------
Error using *
Inputs must be 2-D, o enter code here r at least one input must be scalar.
To compute element wise TIMES, use TIMES (.*) instead.
The spectral radius of a matrix is the maximum of the modulus of its eigenvalues. It can be simply computed using max(abs(eig(·))).
However, as others have noticed, your whole code seems pretty mixed up and not actually valid Matlab code, so your problem is not really to compute the spectral radius, is it? The algorithm is very straightforward and easy to implement:
% diagonal part of A and rest
D = diag(diag(A));
R = A - D;
% iteration matrix and offset
T = - inv(D) * R;
C = inv(D) * b;
% spectral radius condition
rho = max(abs(eig(T)));
if rho >= 1
error('no convergence')
end
% initial guess
x = randn(size(b));
% iteration
while norm(A * x - b) > 1e-15
x = T * x + C;
end
Note that I used inv(D) to directly follow the description in Wikipedia, but the inverse of a diagonal matrix can be easily computed using diag(1 ./ diag(D)).
I don't really see why one would need to separate R into an upper and lower part. I suppose it has to do with numerical efficiency, but then, Matlab is a very efficient high-level language for matrix computations already. So actually there is no need to implement the Jacobi algorithm in it explicitly when you can simply write A \ b – except for educational purposes I guess.

plot gmdistribution.fit together with data

I have a histogram of data (x,y) which is a mixed of two Gaussians (simple case i.e in one dimension). To get a fit to it, i use
obj= gmdistribution.fit(data,2)
that gives me the
obj =
Gaussian mixture distribution with 2 components in 1 dimensions
Component 1:
Mixing proportion: 0.499387
Mean: -0.4789
Component 2:
Mixing proportion: 0.500613
Mean: -0.4786
Now, how can I plot this together with the original histogram? I am aware of these 2 links but they dont help me so much:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317868
http://www.mathworks.de/de/help/stats/gmdistribution.fit.html
Any clear help is highly appreciated!
I guess this might be a bit late, but its been updated on matlabs site. Just in case
MU1 = [1 2];
SIGMA1 = [2 0; 0 .5];
MU2 = [-3 -5];
SIGMA2 = [1 0; 0 1];
X = [mvnrnd(MU1,SIGMA1,1000);mvnrnd(MU2,SIGMA2,1000)];
scatter(X(:,1),X(:,2),10,'.')
hold on
options = statset('Display','final');
obj = gmdistribution.fit(X,2,'Options',options);
10 iterations, log-likelihood = -7046.78
h = ezcontour(#(x,y)pdf(obj,[x y]),[-8 6],[-8 6]);
You could do the same for 3d using scatter3.