Finding local maxima for 3d matrix - matlab

I am trying to implement Hough transform in Matlab to find circles in a picture.
In the accumulator matrix, the global maximum is 105 at A(32,31,24). So I'm able to get this: max circle
The problem is, how can i find the local maxima to find the rest of the circles?
I wrote this to find A(i,j,k) which is bigger than the 26 adjacent points (26-Connected voxel neighborhood):
[i j k]=find(A~=0) ;
f=0;
for s=1:size(i)
if(i(s)~=100&&j(s)~=100&&k(s)~=141&&i(s)~=1&&j(s)~=1&&k(s)~=1)
if (A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)-1))
f=f+1
i(s)
j(s)
k(s)
end
end
end
Why i never got these correct i,j,k and f is always 0? I think i should at least find (32,31,24) and f=1?
Can anyone help me ?
Thank you so much!
The complete code is here:
im=imread('C:\Users\dell\Desktop\tp-complet\Hough\four.png');
sigma = 0.3;
gausFilter = fspecial('gaussian',[5 5],sigma);
sobelFilter=fspecial('sobel');
img=imfilter(im,gausFilter,'replicate');
ims=edge(img,'sobel');
rmax=size(im,1);
cmax=size(im,2);
radmax=round(sqrt(rmax^2+cmax^2));
for i=1:rmax
for j=1:cmax
for k=1:radmax
A(i,j,k)=0;
end
end
end
[r c]=find(ims==1);
length=size(r);
for k=1:length
for l=1:rmax
for m=1:cmax
if((l~=r(k))&&(m~=c(k)))
x=sqrt((l-r(k))^2+(m-c(k))^2);
x=round(x);
A(l,m,x)=A(l,m,x)+1;
end
end
end
end
[i j k]=find(A~=0) ;
f=0;
for s=1:size(i)
if(i(s)~=100&&j(s)~=100&&k(s)~=141&&i(s)~=1&&j(s)~=1&&k(s)~=1)
if (A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)-1))
f=f+1
i(s)
j(s)
k(s)
end
end
end

I don't have the image processing toolbox, so cannot readily run your code. However, a quick review indicates to me that you're using the find function incorrectly. When provided with 3 outputs (as you have it), find doesn't return the 3D indices, but rather row, column, and a vector of the values. That means that k is just a giant vector of 1's, and so your for loop's if k(s)~=1 statement is never satisfied. You should do something like [i,j,k]=ind2sub(size(A),find(A~=0)) if you instead want the 3D indices.
Also, FYI, this:
for i=1:rmax
for j=1:cmax
for k=1:radmax
A(i,j,k)=0;
end
end
end
Can be replaced with A=zeros(rmax,cmax,radmax);.

Related

Function presents data wrong, Monte Carlo method

I am trying to write a program that does integration with monte carlo method. One of its features is to place dots on the graph with different colours, blue or red depending on the if statement. The if statement is put in a "for" loop and i dont know why but it seems like the first option is ignored after first iteration. The whole thing looks like this :
but it should look like this :
In addition i dont know why but it looks like the plot makes some additional empty space at the top
The whole code is not finished yet, its just a matter of a few lines but these dots are so annoying that I want to figure out whats wrong first. Heres the code.
function p=montecarlo(f, a, b, n, t)
%f is a function provided by user
%a and b is a range
%n is the amount of random points
%t is a t=a:01:b vector to draw a plot
upper=max(f(t));
lower=min(f(t));
x=a+(b-a).*(rand(n,1)) %generates vector of random numbers from a to b
y=lower+(upper-lower).*(rand(n,1)) %generates vector of ranom numbers from min to max
hold on
for i=1:n
if y(i)>=f(i)
plot(x(i),y(i),'bo')
else
plot(x(i),y(i),'ro')
end
plot(t,f(t),'k')
end
end
Arguments provided to the function : f= x.^2+3*x+5, a= -4 , b= 2, n= 1000 .
The problem is simple. The statement:
if y(i)>=f(i)
is wrong. What you want to do is compare the random value y(i) with the function value at the corresponding point x(i), so it should be:
if y(i)>=f(x(i))
plot(x(i),y(i),'bo')
else
plot(x(i),y(i),'ro')
end

Iterating a function in MATLAB

Incredibly simple question, but I think I'm unable to come up with the correct terminology to google search it.
If I have a snippet of code that relies on three independent variables:
code(x,y,z)
That produces two values, i.e.:
output1, output2
How do I go about iterating like so (pseudocode):
for x
for y
for z
code(x,y,z)
end
end
end
And have data I can parse to generate 3D graphs such as
surf(x,y,output1)
A naive solution I came up with was just to create a bin of n length and then iterating one variable n times to come up with a 2D graph, i.e:
x_axis = zeros(1,25)
for m = 1:25
xm = x + 1
x_axis(m) = xm
code(x,y,z)
Even a referral to some documentation would be extremely helpful.
Thanks!
Brute force approach:
for x=[1:50]
for y=[1:50]
for z=[1:50]
result(y,x,z)=code(x,y,z);
end
end
end
More paradigmatic approach (in MATLAB) is to meshgrid it, and pump those in.
[XX,YY,ZZ]=meshgrid([1:50],[1:50],[1:50]);
result=code(XX,YY,ZZ);

On 1x2 convolution and combining gradients

So I'm trying to understand convolution and the process on making gradients and I wanted to just see the horizontal gradient of the 1x2 operator on an image named I1. When I tried to use this code I only get a black screen so I'm trying to figure out what went wrong here,sans using conv of course. (I'm also going to try out Sobel too, so I'd like some tips on how to get that going.)
I1 = uint8(round(sum(C1,3)/3));
figure,imshow(I1);
Kern =[-1,1];
Omega = zeros([size(I1,1) size(I1,2)]);
for i=1:ROWS
for j=1:COLS
Work = double(I1(i,j)).*Kern;
Omega(i,j) = sum(Work(:));
end
end
figure,imshow(uint8(Omega));
The problem is that you're only using 1 pixel from I1 to multiply by your kernel. Since you're using one value, the end effect is:
a.*[-1 1]
which gives you
[-a a]
When you sum this, obviously you get zero. To fix this, you'll need to use the same number of pixels from I1 as you have elements in your kernel (in this case, 2). This will also mean that you need to adjust your loop indices:
for i=1:ROWS-1 % avoid accessing outside image
for j=1:COLS-1
Work = double(I1(i,j:j+1)).*Kern; % j:j+1 gives us 2 pixels
Omega(i,j) = sum(Work(:));
end
end
You can also condense the two lines inside the loop into one:
Kern = [-1;1]; % make Kern a column vector
...
for i=1:ROWS-1
for j=1:COLS-1
Omega(i,j) = double(I1(i,j:j+1))*Kern; % vector multiplication, not elementwise
end
end
Another thing you might want to try is use imagesc(Omega) instead of imshow. imagesc will scale the values of the image so it's more visible.

Difference between using (j,i) vs (i,j) in MATLAB

So I have this code, where my response Shrinkage is affected by 5 variables namely ORT,LT,PBT,RA and L. I am plotting a surface for response when 2 variables vary while others are constant. I am getting the wrong graph when i use (i,j) while defining the shrinkage. This problem is solved when i use (j,i) instead of (i,j). I wish to know why this is happening.
ORT=0:1.8:90;
LT=100:4:300;
PBT=54;
RA=0;
L=50;
i=1;
j=1;
Shrinkage=zeros(50,50);
for i=1:50
for j=1:50
Shrinkage***(i,j)***=-0.610-(0.000653*ORT(i))+(0.002238 *LT(j))-
(0.000255*RA)+(0.00497*L)+(0.01411*PBT)+(0.000003*ORT(i)*LT(j))-
(0.000044*LT(j)*PBT)-(0.000009*RA*RA)-(0.000001*LT(j)*LT(j))-
(0.000011*L*L)+(0.000010*ORT(i)*L)+(0.000007*RA*L)-(0.000078*PBT*L);
end
end
A=linspace(0,90,50);
B=linspace(100,300,50);
[ORT,LT]=meshgrid(A,B);
surfc(ORT,LT,Shrinkage);
This is issue with Matrix Indices vs. Axis Indices.
Pay attention when you use this or that (meshgrid vs. ndgrid).

How to create a vector of the results in a for loop

I have a problem with the following code. I want to store all the values I am creating in the for loop below so that I can make a plot of it. I have tried several things, but nothing works. Does anyone know a simple method to create a vector of the results and then plot them?
dx=0.1;
t=1;
e=1;
for x=-1:dx:1
lower_bound=-100;
upper_bound=x/(sqrt(4*t*e));
e=1;
u=(1/sqrt(pi))*quad(#integ,lower_bound,upper_bound);
plot(x,u)
hold on
end
hold off
I would like to use as much of this matlab code as possible.
dx=0.1;
t=1;
e=1;
xval=[-1:dx:1].';
upper_bound = zeros(numel(xval),1);
u = zeros(numel(xval),1);
for ii=1:numel(xval)
x = xval(ii)
lower_bound=-100;
upper_bound(ii,1)=x/(sqrt(4*t*e));
u(ii,1)=(1/sqrt(pi))*quad(#integ,lower_bound,upper_bound(ii));
end
figure;
plot(xval,u)
by adding the (ii) behind your statements it saves your variables in an array. I did not use that on your lower_bound since it is a constant.
Note that I first created an array xval and called that with integers in ii, since subscriptindices must be positive integers in MATLAB. I also initialised both upper_bound and u by creating a zero matrix before the loop executes. This is handy since extending an existing vector is very memory and time consuming in MATLAB and since you know how big they will get (same number of elements as xval) you might as well use that.
I also got the plot call outside the loop, to prevent you from plotting 21 blue lines in 1 plot.