Professional Documents
Culture Documents
y = x(1:2:end);
y1 = x(2:2:end);
Sobel filter:
A=imread('peppers.png');
B=rgb2gray(A);
C=double(B);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image
%B(i,j)=abs(Gx)+abs(Gy);
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
figure,imshow(B); title('Sobel gradient');
Canny detector
img
sig
% The default values for (sig, mLow, mHigh) are (1, 0.5, 2.5)
% The function displays the image and also returns:
%
sFinal
thresh
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% CS223b HW# 1a: Canny Edge Detector
% Rohit Singh & Mitul Saha:
{rohitsi, mitul}@stanford.edu
%
% Good parameters:
%
elephant.jpg :
canny(eim,
1.5, 2.6, 1)
macbeth.jpg
hecface.jpg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
if (nargin < 1)
error(' Need a NxNx3 or NxN image matrix');
elseif (nargin ==1)
mLow = 0.5; mHigh = 2.5; sig = 1;
elseif (nargin == 2)
mHigh = 2.5; sig = 1;
elseif (nargin == 3)
sig = 1;
end
%sig = 1;
%mLow = 0.5;
%mHigh = 2.5;
%img
= imread('macbeth.jpg');
%img
= imread('elephant.jpg');
%img
imread('hecface.jpg');
origImage = img;
if (ndims(img)==3)
img =double(rgb2gray(img));
end
%smoothen ??
%G = gauss(sig);
%img = conv2(img, G'*G,'same');
dG=dgauss(sig);
sy = conv2(img, dG ,'same');
sx = conv2(img, dG','same');
[m, n]=size(img);
% crop off the boundary parts...the places where the convolution was partial
sx = sx(offset+1:m-offset, offset+1:n-offset);
sy = sy(offset+1:m-offset, offset+1:n-offset);
% norm of gradient
sNorm = sqrt( sx.^2 + sy.^2 );
% direction of gradient
sAngle = atan2( sy, sx) * (180.0/pi);
sAorig = sAngle;
binDist =
[dummy, b] = histc(sAngle,binDist);
sDiscreteAngles = b;
[m,n] = size(sDiscreteAngles);
sEdgepoints = zeros(m,n);
sFinal = sEdgepoints;
lowT
= mLow * mean(sNorm(:));
% NON-MAXIMAL SUPPRESSION
% for each kind of direction, interpolate.... and see if the current point is
% is the local maximum in that direction
%in the following comments, assume that we are currently at (0,0) and
%we have to interpolate from the 8 surrounding pixels, also, we are
%using MATLAB's single index feature...
(1- Gy/Gx)*(magtd(0,0)-magtd(-1,0))
(1- Gy/Gx)*(magtd(0,0)-magtd(1,0))
%HYSTERESIS PART...
sEdgepoints = sEdgepoints*0.6;
x = find(sEdgepoints > 0 & sNorm < lowT);
sEdgepoints(x)=0;
x = find(sEdgepoints > 0 & sNorm
>= highT);
sEdgepoints(x)=1;
%sFinal(sEdgepoints>0)=1;
oldx = [];
x = find(sEdgepoints==1);
while (size(oldx,1) ~= size(x,1))
oldx = x;
v = [x+m+1, x+m, x+m-1, x-1, x-m-1, x-m, x-m+1, x+1];
sEdgepoints(v) = 0.4 + sEdgepoints(v);
y = find(sEdgepoints==0.4);
sEdgepoints(y) = 0;
y = find(sEdgepoints>=1);
sEdgepoints(y)=1;
x = find(sEdgepoints==1);
end
x = find(sEdgepoints==1);
sFinal(x)=1;
figure(1);
imagesc(sFinal); colormap(gray); axis image;