Gilad
Gilad

Reputation: 6575

Sobel Edge detection – matlab

hello as part of my Homework. i need to calculate and display the edge magnitude image and the edge direction image of image balls1.tif, using Sobel Edge detection.

Do not use matlab's edge function. You may use conv2. Display a binary edge image (1 edge pixel, 0 no edge) of strong edge pixels (above a threshold). Determine a threshold that eliminates the ball shadows.

here is my main.m

addpath(fullfile(pwd,'TOOLBOX'));
addpath(fullfile(pwd,'images'));

%Sobel Edge Detection 
Image = readImage('balls1.tif');
showImage(Image);
message = sprintf('Sobel Edge Detection');
sobelEdgeDetection(Image);
uiwait(msgbox(message,'Done', 'help'));
close all

here is my SobeEdgeDetection.m

function [ output_args ] = SobelEdgeDetection( Image )

maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;

resX = conv2(Image, maskX);
resY = conv2(Image, maskY);

magnitude = sqrt(resX.^2 + resY.^2);
direction = atan(resY/resX);
thresh = magnitude < 101;
magnitude(thresh) = 0;
showImage(magnitude);

end

my questions are:
1. i what is the direction used for ? and how can i display it?
2. is there a better way to get a threshold to eliminate the ball shadows. i used trial and error....

enter image description here

these are my result as far as showing the magnitude:

enter image description here

Upvotes: 4

Views: 29268

Answers (2)

mmgp
mmgp

Reputation: 19221

According to the second part of your homework you have solved it, i.e., you eliminated the shadows.

For the first question: the direction can be used in many different ways. Here is the simplest way: make pretty pictures with it. A more useful reason to consider it is when you are doing non-maximum suppression, but since you are not manually doing it then there isn't much immediate use for it. To visualize the results of the gradient direction it is simply matter of establishing colors for each direction you consider. To further simplify the visualization also suppose you reduce the directions to increments of 30 degrees up to 180, starting from 0. That way if you have a direction of 35 degrees, for example, you consider it as 30 degrees (since it is the nearest one in your reduced list). Next we see an image and a visualization of its gradient direction considering Sobel and the discretization to steps of 30 degrees (black is indicating 0 degree direction).

enter image description here enter image description here

Automatically determining good thresholds is usually not an easy task. For example, you could start with the one provided by the Otsu method, and decrease or increase its value based on some other histogram analysis according to the problem you are trying to solve.

Upvotes: 2

Mayank Jain
Mayank Jain

Reputation: 2564

Here's the answer to your first question :

In Sobel Edge Detection Algo. the direction obtained is basically the gradient.

Gradient in image processing is defined as the direction in which change for intensity is maximum. Change can be increase in intensity or decrease in intensity. Also, this change is calculated for each pixel,this means that for each pixel maximum change in intensity is measured. resX (in your question's example,SobelEdgeDetection.m) signifies changes in X-direction and resY defines change in Y direction.

See see it practically just fire this command in command window of Matlab: imshow(resX);

Also try, imshow(resY)

Upvotes: 1

Related Questions