2D Convolution - Sobel Filter. What is wrong?

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

Respuestas (2)

David Wilson
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: DavisH.png

3 comentarios

Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019
IT IS Not Allowed to use: imfilter, conv2, filter2, conv
David Wilson
David Wilson el 12 de Sept. de 2019
Ah, then this is homework?
Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019
It is a graded excercise\online course.

Iniciar sesión para comentar.

Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019

0 votos

Here is my results:
untitled.jpg

10 comentarios

Bruno Luong
Bruno Luong el 12 de Sept. de 2019
Why you think there is anything wrong?
I see aliasing but it might cause to the way you capture/attach image here.
Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019
It is a graded excercise\online course.
I didn't pass.
Well sorry the only thing I detect is
Gx=rot90(Gx,2);
Gy=rot90(Gy,2);
That reverse the sign of the gradient. But there is no consequence on edge detection.
Bruno Luong
Bruno Luong el 12 de Sept. de 2019
Editada: Bruno Luong el 12 de Sept. de 2019
I know now what induce you the error. Using CONVOLUTION with flipped the kernel == filter with "straight" kernel.
In your double for-loop you perform filtering (and not convolution), so you must not flip (rotate 180 degree = flip-x/flip-y) Gx, Gy.
Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019
I have commented the rotation ... Still have the error.
Picture1.jpg
Nisreen Sulayman
Nisreen Sulayman el 12 de Sept. de 2019
Picture2.jpg
Bruno Luong
Bruno Luong el 12 de Sept. de 2019
Editada: Bruno Luong el 12 de Sept. de 2019
The second error I spot is the way you deal with padding and boundary
IMO the loop and padding should be (not tested)
[r,c]=size(I);
Fx = zeros(r,c);
Fy = zeros(r,c);
I = padarray(I,[1 1],'replicate'); % lass abrute than 0
for i=1:r
for j=1:c
Fx(i,j)=sum(sum(Gx.*I(i:i+2,j:j+2)));
Fy(i,j)=sum(sum(Gy.*I(i:i+2,j:j+2)));
end
end
Nisreen Sulayman
Nisreen Sulayman el 13 de Sept. de 2019
Editada: Nisreen Sulayman el 13 de Sept. de 2019
Didn't work!!
Maybe there is something wrong related to the "greader"
We have a good results ... still didn't accept the answer!! (even with first code I have a good results!!)
OR THERE is that kind of silly bug which I couldn't spot.
How to normalize a convolved image?
I have got these messages after running
%read the image
img = imread('Davis_Hall.jpg');
I =double(rgb2gray(img));
%Gx = double([-1 0 1;-2 0 2;-1 0 1]);
Gx=[-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=[-1 -2 -1; 0 0 0;1 2 1];
Gy=rot90(Gy,2);
[r,c]=size(I);
Fx = zeros(r,c);
Fy = zeros(r,c);
FMag=zeros(r,c);
I = padarray(I,[1 1],0,'both');
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)));
FMag(i,j)=sqrt(power(Fx(i,j),2)+power(Fy(i,j),2));
end
end
Fx=Fx(2:r-1,2:c-1);
Fy=Fy(2:r-1,2:c-1);
FMag=FMag(2:r-1,2:c-1);
img=uint8(img);
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');
%Fx may have negative values and values which are greater than 255, hence normalize before visualiz
%Fy may have negative values and values which are greater than 255, hence normalize before visualizin
%FMag may have values which are greater than 255, hence normalize before visualizing

Iniciar sesión para comentar.

Categorías

Preguntada:

el 11 de Sept. de 2019

Comentada:

el 20 de Sept. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by