×
☰ Menu

Image Edge Detection Using Sobel Filtering and Canny Filtering 

 

Matlab Code: 

clc;
clear;
I=imread('lena.jpg'); 
Sobel=sob1(I);
j=im2bw(I);
Canny_img = edge(j,'Canny');
subplot(2,2,1);imshow(I); title('Original Image');
subplot(2,2,2);imshow(Sobel); title('Sobel edge');
subplot(2,2,3);imshow(Canny_img); title('canny edge');

sob1 function: 

function output_image = sob1(input_image)

%input_image = imread('[name of input image file].[file format]');
  
% Displaying Input Image
input_image = uint8(input_image);

% Convert the RGB image to the grayscale image
input_image = rgb2gray(input_image);
  
% Convert the image to double
input_image = double(input_image);
  
% Pre-allocate the filtered_image matrix with zeros
filtered_image = zeros(size(input_image));
  
% Sobel Operator Mask
Mx = [-1 0 1; -2 0 2; -1 0 1];
My = [-1 -2 -1; 0 0 0; 1 2 1];

for i = 1:size(input_image, 1) - 2
    for j = 1:size(input_image, 2) - 2
  
        % Gradient approximations
        Gx = sum(sum(Mx.*input_image(i:i+2, j:j+2)));
        Gy = sum(sum(My.*input_image(i:i+2, j:j+2)));
                 
        %  magnitude of vector
        filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);
         
    end
end
  
% Displaying Filtered Image
filtered_image = uint8(filtered_image);

  
% Define a threshold value
thresholdValue = 100; % varies between [0 255]
output_image = max(filtered_image, thresholdValue);
output_image(output_image == round(thresholdValue)) = 0;
  
% Displaying Output Image
output_image = im2bw(output_image);

 

Output: 


To create a program to display grayscale images using read and write operation. 
To create a program to perform arithmetic operations in images.
To create a program to perform logical operations in images.  
To shows image rotation, scaling, and translation using Geometric transformations. 
Create a program for image enhancement using histogram equalization.
To perform the Two-dimensional Fourier, transform operation in an image.
Image Edge Detection Using Sobel Filtering and Canny Filtering 
To perform the following operations in an image. 
a.    erosion, 
b.    dilation, 
To perform the following operations in an image. 
a.    opening, 
b.    closing,