1

So I am supposed to apply a Sobel filter on an image in Octave, but I am not allowed to use any functions from the image package. I wrote the code but my output is just a black image. Here is what I have so far:

%Sobel Gradient

kx= [1 ,0 ,-1; 2,0,-2; 1, 0 ,-1];
ky= [1,2,1; 0,0, 0; -1, -2 ,-1];

H = conv2(kx,im2double(my_img),'same');
V = conv2(ky,im2double(my_img),'same');
E = sqrtm(H.*H + V.*V);
figure 4
imshow(E, [])

Thank you for the help!

daiana05
  • 11
  • 5

1 Answers1

2

Few minor mistakes, and your problem is fixed:

1) you convolve your image by the kernel, not the opposite. Mainly because you are using 'same' and that will output the size of the first input, 3x3.

2) you dont want a matrix square root, just the normal elementwise square root.

my_img=imread('cameraman.tif') % available in MATLAB, not sure octave
kx= [1 ,0 ,-1; 2,0,-2; 1, 0 ,-1];
ky= [1,2,1; 0,0, 0; -1, -2 ,-1];

H = conv2(im2double(my_img),kx,'same');
V = conv2(im2double(my_img),ky,'same');
E = sqrt(H.*H + V.*V); % or sqrt(H.^2+V.^2)
figure
imshow(E, [])

enter image description here

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • 1
    Have my vote too, for being 8,342,602 times better at Octave than me, although multiplying that by my big, fat zero doesn't come to much ;-) – Mark Setchell Apr 06 '18 at 15:36