What is the meaning of these matrices in Perona & Malik filter - matlab

I was searching for the implementation of Perona & Malik filter in Matlab, when i found this link "the link has an implementation for Perona and Malik filter using Matlab"
but there is the matrices that i didn't understand what is the use of them:
% 2D convolution masks - finite differences.
hN = [0 1 0; 0 -1 0; 0 0 0];
hS = [0 0 0; 0 -1 0; 0 1 0];
hE = [0 0 0; 0 -1 1; 0 0 0];
hW = [0 0 0; 1 -1 0; 0 0 0];
hNE = [0 0 1; 0 -1 0; 0 0 0];
hSE = [0 0 0; 0 -1 0; 0 0 1];
hSW = [0 0 0; 0 -1 0; 1 0 0];
hNW = [1 0 0; 0 -1 0; 0 0 0];
any one has an idea what are these matrices, or what do they mean?

These are the kernels for getting the gradients in North, East, South and West directions, and also for dialgonals (NE = North East etc.).
hN = [0 1 0; 0 -1 0; 0 0 0];
or nicely formatted:
0 1 0
0 -1 0
0 0 0
Convolution of this kernel with an image yields the gradient in "North direction". Basically, this is a mask of factors that you apply to all your 3x3 areas of your image and since all except two entries are 0, what you get is the pixel at the top minus that in the center, thus the gradient in y-direction.

Related

How to fix the plot of the symbolic eigenvalues of the 1st matrix (matlab)?

I'm calculating the eigenvalues of a 8x8-matrix including a symbolic variable "W". Plotting the 8 eigenvalues as functions of W returns a strange result in the plot which looks like someone rode his bike over my diagram.
For the 2nd matrix, where I just set some off-diagonal elements equal to 0, everything works fine. But I don't know what the problem with the 1st one is.
syms W;
w0=1/780;
wl=1/1064;
h=1; % for now this seems unnecessary, but I want to change this value later on
% This is the 1st matrix which causes some strange plotting results
A=h*[w0+3*wl 2*W 0 0 0 sqrt(3)*W 0 0;
2*W 4*wl 0 0 0 0 0 0;
0 0 2*wl+w0 sqrt(3)*W 0 0 0 sqrt(2)*W;
0 0 sqrt(3)*W 3*wl 0 0 0 0;
0 0 0 0 wl+w0 sqrt(2)*W 0 0;
sqrt(3)*W 0 0 0 sqrt(2)*W 2*wl 0 0;
0 0 0 0 0 0 w0 W;
0 0 sqrt(2)*W 0 0 0 W wl];
% This is the 2nd matrix for which everything is working fine
B=h*[w0+3*wl 2*W 0 0 0 0 0 0;
2*W 4*wl 0 0 0 0 0 0;
0 0 2*wl+w0 sqrt(3)*W 0 0 0 0;
0 0 sqrt(3)*W 3*wl 0 0 0 0;
0 0 0 0 wl+w0 sqrt(2)*W 0 0;
0 0 0 0 sqrt(2)*W 2*wl 0 0;
0 0 0 0 0 0 w0 W;
0 0 0 0 0 0 W wl];
X = eig(A);
X2 = eig(B);
eva22 = X2(1);
eva1 = X(1);
figure(1);
fplot(X2,[-0.002 0.002]);
hold on;
fplot(X,[-0.002 0.002]);
hold off;
xlabel('Rabi frequency [THz]','FontSize',11);
ylabel('dressed states','FontSize',11);
grid on;
box on;
I'm expecting the plot for matrix A to just be similar to the plot of matrix B, but somehow it doesn't work properly. I'd appreciate some tips and tricks how to fix this.
The second plot looks like that because the eigenvalues of B are imaginary. When using plot(), it plots the real part of complex numbers by default, but apparently fplot() doesn't. You can do fplot(real(X), [-0.002 0.002]) instead to plot just the real part of the eigenvalues (assuming that's what you want).

plot cube in matlab

I want to plot a cube of side length 10, that would be symmetrical from -5 to 5 and not -6 to 4.
xc=1; yc=1; zc=1; % coordinated of the center
L=10; % cube size (length of an edge)
alpha=0.8; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
C='blue'; % unicolor
X = L*(X-0.5) + xc;
Y = L*(Y-0.5) + yc;
Z = L*(Z-0.5) + zc;
fill3(X,Y,Z,C,'FaceAlpha',alpha); % draw cube
axis equal
I just set the center to (0,0,0) to get the desired result:
xc=0; yc=0; zc=0; % coordinates of the center

How to plot 3d directed graph in matlab

I have an upper triangular adjacent matrix which represents a set of nodes which are connected. Every node is defined by three geographical coordinates: x y z.
My goal is to plot the network in order to see what it looks like by taking into account also the direction of the edges.
If I do not take into account the z coordinate, I am able to display the result easily:
The lines of code to get this result are:
A = [0 1 1 0 0 0 0;
0 0 1 1 0 0 0;
0 0 0 1 1 1 0;
0 0 0 0 1 1 0;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 0];
xyz = [ 0 0 0;
-15 20 5;
17 24 -3;
-5 36 7;
-14 50 -8;
16 56 3;
3 70 -1];
F = digraph(A);
figure
p = plot(F,'XData',xyz(:,2),'YData',xyz(:,1)); axis equal;
highlight(p,1,'NodeColor','g'); highlight(p,size(A,1),'NodeColor','r');
view([0 90])
How should I modify my code in order to assign to the graph also the z coordinates so I can have a 3d graph? (remember I want to display the edge direction too!!).
What I tried to do is this:
p = plot3(F,'XData',xyz(:,2),'YData',xyz(:,1),'ZData',xyz(:,3));
but I had no success.
Cool problem. I had some spare time and produced this:
close all
clear all
A = [0 1 1 0 0 0 0;
0 0 1 1 0 0 0;
0 0 0 1 1 1 0;
0 0 0 0 1 1 0;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 0];
xyz = [ 0 0 0;
-15 20 5;
17 24 -3;
-5 36 7;
-14 50 -8;
16 56 3;
3 70 -1];
figure; hold on
for jj=1:size(A,1) %cycle on nodes
conn=find(A(jj,:)); %find connections for each node
if numel(conn>0) %if there are non null connections
for kk=1:numel(conn) %plot them
a=conn(kk);
lh=quiver3(xyz(jj,1),xyz(jj,2),xyz(jj,3),...
xyz(a,1)-xyz(jj,1),xyz(a,2)-xyz(jj,2),xyz(a,3)-xyz(jj,3),0,'maxheadsize',0.5);
set(lh,'linewidth',4);
set(lh,'color',[1,0,0]);
end
end
end
scatter3(xyz(:,1),xyz(:,2),xyz(:,3),800,'b','.') %plot nodes
%number the nodes
for ii=1:size(xyz,1)
text(xyz(ii,1),xyz(ii,2),xyz(ii,3),num2str(ii),'Color','k','FontWeight','bold',...
'FontSize',14, 'HorizontalAlignment','right', 'VerticalAlignment','bottom')
end
xlabel('x')
ylabel('y')
zlabel('z')
view(-15,18)
grid on
That I think looks pretty much like what you want:
EDIT
If you want the arrow head to be at the midpoint of the two nodes you can use:
line([xyz(a,1) xyz(jj,1)],[xyz(a,2) xyz(jj,2)],[xyz(a,3) xyz(jj,3)],'color',[1 0 0],'linewidth',3)
lh=quiver3(xyz(jj,1),xyz(jj,2),xyz(jj,3),...
xyz(a,1)-xyz(jj,1),xyz(a,2)-xyz(jj,2),xyz(a,3)-xyz(jj,3),.5,'maxheadsize',0.5);
This draws a line first and the superimpose an arrow that reaches the midpoint (note the 0.5scaling factor in the quivercommand):
In the newest MATLAB Release (R2016b), it's now possible to plot a node-link graph in 3d by choosing a different layout method, or specifying x, y and z coordinates directly. In your example, replace the plotting line by
p = plot(F,'XData',xyz(:,2),'YData',xyz(:,1),'ZData',xyz(:,3));
Resulting plot:

How to sum the 8-neighbor pixel values of the current pixel.

I have a binary image. I want to find the pixel value = 1 and label it as the current pixel. Then, I want to sum its 8-neighbor pixel values. If the summation of the 8-neighbor pixel values of the current pixel = 1, then mark that current pixel with marker. Some part of a binary image as follows:
0 0 0 0 0
0 1 0 0 0
0 0 1 1 0
0 0 0 0 1
0 0 0 0 0
I tried the following matlab code but it has some errors (at this line -> Sums = sum(currentPix, nOffsets);). How can I fix it?
Sums = 0;
S = size(BW,1);
nOffsets = [S, S+1, 1, -S+1, -S, -S-1, -1, S-1]'; %8-neighbors offsets
BW_Out = BW;
for row=1:S
for col=1:S
if BW(row,col),
break;
end
end
idx = sub2ind(size(BW),row,col);
neighbors = bsxfun(#plus, idx, nOffsets);
currentPix = find(BW==1); %if found 1, define it as current pixel
while ~isempty(currentPix)
% new current pixel list is set of neighbors of current list.
currentPix = bsxfun(#plus, currentPix, nOffsets);
currentPix = currentPix(:);
Sums = sum(currentPix, nOffsets); %error at this line
if (Sums==1) %if the sum of 8-neighbor values = 1, mark ROI
plot(currentPix,'r*','LineWidth',1);
end
% Remove from the current pixel list pixels that are already
currentPix(BW_Out(currentPix)) = [];
% Remove duplicates from the list.
currentPix = unique(currentPix);
end
end
I think you can actually do this in one line (after defining a kernel that is)
I = [0 0 0 0 0
0 1 0 0 0
0 0 1 1 0
0 0 0 0 1
0 0 0 0 0];
K = [1 1 1;
1 0 1;
1 1 1;];
(conv2(I,K,'same')==1) & I
ans =
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
Breaking this up:
M = conv2(I,K, 'same'); %// convolving with this specific kernel sums up the 8 neighbours excluding the central element (i.e. the 0 in the middle)
(M==1) & I %// Only take results where there was a 1 in the original image.

Response Surface Methodology-Third Order

I am trying to use RSM and calculate 3rd order polynomials.for quadratic below is given in Matlab Help:
stats = regstats(rsmOutput,rsmMatrix,'quadratic','beta');
b = stats.beta; % Model coefficients
How can I calculate 3rd order coefficients? My reason is that with quadratic I have rsquare of 93% and my observed responses is third order.
For
stats = regstats(y,X,model,whichstats)
the 'model' can be a matrix of model terms accepted by the 'x2fx' function. See x2fx for a description of this matrix and for a description of the order in which terms appear. You can use this matrix to specify other models including ones without a constant term.
modelMatrix = [0 0 0;
1 0 0;
0 1 0;
0 0 1;
1 1 0;
1 0 1;
0 1 1;
2 0 0;
0 2 0;
0 0 2;
1 1 1;
2 1 0;
2 0 1;
1 2 0;
1 0 2;
0 2 1;
3 0 0;
0 3 0;
0 0 3];
stats = regstats(rsmOutput,rsmMatrix,modelMatrix,'beta');