Edge detection with Sobel

조회 수: 20 (최근 30일)
KaMATLAB
KaMATLAB 2021년 1월 1일
댓글: Image Analyst 2024년 1월 22일
Hi, I am trying to execute an image processing project with sobel edge detection technique. I want to write a function edgy that takes an orginal input image and process an output image. Both the input and output are grayscales images which means they are matrices of date structure unint8 values. However, whenever i run the code given below by calling the function edgy to give output edg as given in the code below, the fucntion call returns errors. Please what is the problem with the code? Any advice on the bug in this code is appreicated. Thank you
function edg=edgy(A)
C=double(A);
B=uint8(C)
edg=imshow(B)
[i,j]=size(C)
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);
C(i,j)=sqrt(Gx.^2+Gy.^2);
end
end

채택된 답변

Image Analyst
Image Analyst 2021년 1월 2일
I made a few improvements. This seems to work fine:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
grayImage = imread('cameraman.tif');
subplot(2, 1, 1);
imshow(grayImage);
edg = edgy(grayImage);
subplot(2, 1, 2);
imshow(edg, []);
fprintf('Done running %s.m.\n', mfilename);
function edg = edgy(originalImage)
[rows, columns, numberOfColorChannels] = size(originalImage);
% Convert to gray scale if necessary
if numberOfColorChannels == 3
originalImage = rgb2gray(originalImage);
end
C = double(originalImage);
Gx = zeros(rows, columns);
Gy = zeros(rows, columns);
for row = 1 : rows - 2
for col = 1 : columns - 2
%Sobel mask for x-direction:
Gx=((2*C(row+2,col+1)+C(row+2,col)+C(row+2,col+2))-(2*C(row,col+1)+C(row,col)+C(row,col+2)));
%Sobel mask for y-direction:
Gy=((2*C(row+1,col+2)+C(row,col+2)+C(row+2,col+2))-(2*C(row+1,col)+C(row,col)+C(row+2,col)));
%The gradient of the image
%B(i,j)=abs(Gx)+abs(Gy);
C(row,col)=sqrt(Gx.^2+Gy.^2);
end
end
edg = C;
end
  댓글 수: 1
KaMATLAB
KaMATLAB 2021년 1월 2일
thank you so much fo this

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 1월 1일
Why not just call conv2() with the proper kernel??? Why do it manually like that?
  댓글 수: 3
Muniba
Muniba 2024년 1월 22일
I have followed the same code but get an error as Undefined function 'edgy' for input arguments of type 'uint8'.
Image Analyst
Image Analyst 2024년 1월 22일
@Muniba there is no function called edgy in MATLAB. Did you by chance mean edge? If not, then you'll have to somehow write or obtain the edgy function and make sure it's defined in your script or in its own m-file in the current folder or on the search path.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by