2D Convolution - Sobel Filter. What is wrong?
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
img = imread('Davis_Hall.jpg');
% 'Davis_Hall.jpg':color image
img =double(rgb2gray(img));
Gx = double([-1 0 1;-2 0 2;-1 0 1]);
Gx=rot90(Gx,2);
Gy = double([-1 -2 -1; 0 0 0; 1 2 1]);
Gy=rot90(Gy,2);
I= img;
[r,c]=size(I);
Fx = zeros(r,c);
Fy = zeros(r,c);
I = padarray(I,[1 1]);
for i=2:r-1
for j=2:c-1
Fx(i,j)=sum(sum(Gx.*I(i-1:i+1,j-1:j+1)));
Fy(i,j)=sum(sum(Gy.*I(i-1:i+1,j-1:j+1)));
end
end
img=uint8(img);
FMag=sqrt(Fx.^2+Fy.^2);
figure(1)
imshow(img);
title('Original Image');
figure(2)
imshow((abs(Fx))./max(max(Fx)));
title('Gradient in X direction');
figure(3)
imshow(abs(Fy)./max(max(Fy)));
title('Gradient in Y direction');
figure(4)
imshow(FMag./max(max(FMag)));
title('Gradient Magnitude');
% IT IS Not Allowed to use: imfilter, conv2, filter2, conv
0 comentarios
Respuestas (2)
David Wilson
el 12 de Sept. de 2019
Here's my (old) code for a sobel filter:
img = imread('Davis_Hall.jpg');
% 'Davis_Hall.jpg':color image
X =double(rgb2gray(img));
%% Start
Bx = [-1,0,1;-2,0,2;-1,0,1]; % Sobel Gx kernel
By = Bx'; % gradient Gy
Yx = filter2(Bx,X); % convolve in 2d
Yy = filter2(By,X);
G = sqrt(Yy.^2 + Yx.^2); % Find magnitude
Gmin = min(min(G)); dx = max(max(G)) - Gmin; % find range
G = floor((G-Gmin)/dx*255); % normalise from 0 to 255
image(G); axis('image')
colormap gray
Gives the following:
3 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!