MATLAB: Not getting the segmented image i.e image is not displayed,code is running,please check if some mistakes is there in it

edge detectionhsvimage processingImage Processing Toolbox

function e =tedge(hsv)
h=hsv(:,:,1);
s=hsv(:,:,2);
v=hsv(:,:,3);
for i=1:183
for j=1:276
h(i,j)=h(i,j)+1;
s(i,j)=s(i,j)+1;
v(i,j)=v(i,j)+1;
L=h(i,j)+s(i,j)+v(i,j);
a1=acos((s(i,j).^2.-h(i,j).^2.-v(i,j).^2)./2.*h(i,j).*v(i,j));
a2=acos((h(i,j).^2.-s(i,j).^2.-v(i,j).^2)./2.*s(i,j).*v(i,j));
a3=acos((v(i,j).^2.-h(i,j).^2.-s(i,j).^2)./2.*h(i,j).*s(i,j));
end
end
I=L.*a;
Th=0.4;
w0=[-1,-2,-1;0,0,0;1,2,1];
w90=[-1,0,1;-2,0,2;-1,0,1];
w45=[-2,-1,0;-1,0,1;0,1,2];
w135=[0,-1,-2;1,0,-1;2,1,0];
for i=1:3
g0=imfilter(h,w0);
g90=imfilter(h,w90);
g45=imfilter(h,w45);
g135=imfilter(h,w135);
end
for i=2:size(L,1)-1
for j=2:size(L,1)-1
L(1)=L(i-1,j+1)+2.*L(i,j+1)+L(i+1,j+1)-L(i-1,j-1)-2.*L(i,j-1)-L(i+1,j-1);
L(2)=L(i+1,j-1)+2.*L(i+1,j)+L(i+1,j+1)-L(i-1,j-1)-2.*L(i-1,j)-L(i-1,j+1);
L(3)=L(i,j+1)+2.*L(i+1,j+1)+L(i+1,j)-L(i-1,j)-2.*L(i-1,j-1)-L(i,j-1);
L(4)=L(i,j+1)+2.*L(i-1,j+1)+L(i-1,j)-L(i+1,j)-2.*L(i+1,j)-L(i,j-1);
end
end
if L(1)>Th
e=L(1);
else if L(2)>Th
e=L(2);
else if L(3)>Th
e=L(3);
else if L(4)>Th
e=L(4);
end
end
end
end
x0=abs(g0);
y90=abs(g90);
x45=abs(g45);
y135=abs(g135);
for i=1:4
Ga1=x0(i)+1;
end
for i=1:4
Ga2=y90(i)+1;
end
for i=1:4
Ga3=x45(i)+1;
end
for i=1:4
Ga4=y135(i)+1;
end
if Ga1>=Th
e=Ga1;
else if Ga2>=Th
e=Ga2;
else if Ga3>=Th
e=Ga3;
else if Ga4>=Th
e=Ga4;
end
end
end
end
This the algorithm of the code
Algorithm is given here
a) Each pixel value in the image will plus one;
b) Building corresponding triangle of all pixels and calculating triangular perimeter and three angles;
c) For each pixel, Sobel operator in four directions template and neighborhood pixel convolution, and calculate angle and perimeter difference;
d) Compared triangle perimeter corresponding with neighborhood to original triangle perimeter, if the difference is more big, so make the value of pixel is one, otherwise turn to e;
e) If the difference of triangle perimeter is not big, but the difference of corresponding angle is large, so make the value of pixel is one, otherwise turn to f;
f) If the difference of triangle perimeter and angle are both not very big, but they keep poor in threshold requirement range, so make the value of pixel is one, otherwise make it is zero to detect the image edges.
Because I=L calculate is larger, the actual implementation process is separately apply L and , if L is larger, then use L as the criterion of whether pixel is edge points; otherwise use .
Error
Not displaying the image code is running here is the algorithm for this code just check out wether the code is according to the algorithm
end

Best Answer

Your loops such as
for i=1:4
Ga2=sum(y90(i));
end
are wrong. In each iteration you are overwriting Ga2, so the final value of Ga2 would be as if you had only done the last iteration. Meanwhile, y90(i) names a scalar value, and sum() of a scalar value is going to be just the value itself, so the sum() is not having any effect and your current code is equivalent to
Ga2 = y90(4);