Sobel edge detection

         The gradient of the image is calculated for each pixel position in the image.






























The procedure and the MATLAB  code for sobel edge detection without using MATLAB built-in function:






































MATLAB CODE:


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');
Sobel Gradient

%Define a threshold value
Thresh=100;
B=max(B,Thresh);
B(B==round(Thresh))=0;

B=uint8(B);
figure,imshow(~B);title('Edge detected Image');












Edge detected Image



Edge detected Image(Threshold value:35)
The edge detected image can be obtained from the sobel gradient by
using a threshold value.



  • If the sobel gradient values are lesser than the threshold value then replace it with the threshold value.
    if f < threshold value then
    f = threshold value.

























To avoid complex computation, the gradient can also be computed using the formula:




The Image obtained from computing X-direction derivative:













The Image obtained from computing Y-direction derivative:

Also Check  Sobel Edge Detection - Part 2







42 comments:

  1. why did you substract 2 in the loops
    for i=1:size(C,1)-2
    for j=1:size(C,2)-2
    ????

    ReplyDelete
  2. @Mukesh Mann
    From every (i,j)th position a 3x3 window is formed. On subtracting 2, the index out of bounds is avoided.

    ReplyDelete
  3. Hello, why are the results smaller than the original images? I tried to find it out but I couldn't. Or is it because of the sobel procedure itself? But I couldn't find any reference for this.

    ReplyDelete
  4. Can you help me write point processing functions in matlab without using built-in functions???

    ReplyDelete
  5. How can i design a highpass filter for reducing noise from an image in Matlab without using image processing toolbox?
    can anybody help?

    ReplyDelete
  6. Shahnewz- depends on what type of noise u wanna remove... if u wanna get rid of high freq noise then u gotta apply fourier transform and remove high frequency components.

    ReplyDelete
  7. 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)));
    sir i'm not able to understand dis part.can u explain it with example?
    suppose C is 3x3 size array then what could be value of j.

    ReplyDelete
  8. @vishal

    Contact me through mail. Will try to explain it with an example.

    ReplyDelete
  9. Thats really awesome code you have!!! But I have a question, so i am trying to manipulate or modify an image using the sobel filter along a slider in GUI. So, can you maybe explain a little bit about how you can link the sobel filter to the slider?

    ReplyDelete
  10. aaron:

    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)));

    i m not really understand this part. would you please explain with example?

    thank you

    ReplyDelete
  11. @Sheldon Cooper
    That line is the convolution between Sobel 3x3 horizontal mask and the image matrix. Let's say we consider the element C(i,j) from the original image matrix. To calculate the coresponding element in Gx matrix, Gx(i,j), we take the 8 neighbours surrounding the element C(i,j) and C(i,j), we multiply each of them with the coresponding element of the Sobel mask, and we sum these multiplications: Gx(i,j)=C(i-1,j-1)*S(1,1)+C(i-1,j)*S(1,2)*C(i-1,j+1)*S(1,3)+... (9 multiplications).

    ReplyDelete
  12. Can you tell me why you had to use double precision of the intensity image?

    ReplyDelete
  13. can we use the Edge detected Image as an input for occupancy sensing? I want to know details of it

    ReplyDelete
  14. Hello! Very good article. I have a question for you. Do you know how to create a subpixel sobel edge detector? I'm trying to do that.

    ReplyDelete
  15. anyone have first derivative example matlab coding? can share? thank you

    ReplyDelete
  16. @Kesava Vijayan
    check this post http://angeljohnsy.blogspot.com/2013/07/edge-detection-fundamentals.html
    This may help you.

    ReplyDelete
  17. any one have block diagram of Sobel edge detection

    ReplyDelete
  18. can anyone have block diagram of Sobel edge detection ??

    ReplyDelete
  19. sir
    please tell me that , how should i will decide which filter is useful for my image. please tell me the parameter on which its depend.
    thank you in advance.

    ReplyDelete
  20. This piece of code works like magic.

    ReplyDelete
  21. how to identyfy the color in matlab

    ReplyDelete
  22. how to find sobel gradient direction by using gx & gy values

    ReplyDelete
  23. 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)));
    sir i'm not able to understand dis part.can u explain it?

    ReplyDelete
  24. how to find sub pixesl value...please tell me

    ReplyDelete
  25. in this part B(i,j)=sqrt(Gx.^2+Gy.^2); why it isnt B(i+1,j+1)=sqrt(Gx.^2+Gy.^2); is there any problem at this point?

    ReplyDelete
  26. @Gabriela

    before the multiplication between the sobel mask and image is done, is the sobel mask not supposed to be flipped vertically and horizontally because I think that's what convolution entails

    ReplyDelete
  27. It looks like you have mixed up the derivatives in the x- and y-directions.

    ReplyDelete
  28. I found in a journal that use sobel approximation with a threshold value of 0.02 for obtain edge map. How to get that threshold ?
    because as I know, I only can use threshold with range 0 - 255 (based on maximum gray value) from sobel detection.

    ReplyDelete
  29. Some detection techniques is given in http://imageprocessing4u.blogspot.in/. Is it useful?

    ReplyDelete
  30. HI Sir

    Kindly disclose to me that , in what manner should I will choose which channel is helpful for my picture. if you don't mind disclose to me the parameter on which its depend.
    much obliged to you ahead of time.
    Filter Operator

    ReplyDelete
  31. B(B==round(Thresh))=0; how to explain this statement ? thanks a lot

    ReplyDelete
  32. @Joseph

    Putting zeros to the pixels that has the value 'Thresh'.

    ReplyDelete
  33. 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)));
    I AM NOT UNDERSTAND CAN U EXPLAIN : THANK YOU

    ReplyDelete
  34. how to convert to 5*5 sobel mask operator......above code

    ReplyDelete
  35. How to display a GX image AND GY

    ReplyDelete
  36. HOW TO show image
    gx and gy

    ReplyDelete
  37. This is a nice and informative blog post. Thanks for sharing with us this great post.

    ReplyDelete
  38. good topic about edge detection

    ReplyDelete