简体   繁体   English

Sobel Edge检测– MATLAB

[英]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. 我需要使用Sobel边缘检测来计算和显示图像ball1.tif的边缘幅度图像和边缘方向图像。

Do not use matlab's edge function. 不要使用matlab的edge函数。 You may use conv2. 您可以使用conv2。 Display a binary edge image (1 edge pixel, 0 no edge) of strong edge pixels (above a threshold). 显示强边缘像素(高于阈值)的二进制边缘图像(1个边缘像素,0个无边缘)。 Determine a threshold that eliminates the ball shadows. 确定消除球阴影的阈值。

here is my main.m 这是我的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 这是我的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 ? 1.我的方向是什么? and how can i display it? 我怎么显示它?
2. is there a better way to get a threshold to eliminate the ball shadows. 2.有没有更好的方法来达到消除球阴影的阈值。 i used trial and error.... 我用了反复试验...

在此处输入图片说明

these are my result as far as showing the magnitude: 这些是我的结果,显示出幅度:

在此处输入图片说明

Here's the answer to your first question : 这是您第一个问题的答案:

In Sobel Edge Detection Algo. 在Sobel Edge Detection算法中。 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. resX(在您的问题的示例中为SobelEdgeDetection.m)表示X方向的更改,而resY定义Y方向的更改。

See see it practically just fire this command in command window of Matlab: imshow(resX); 看到它实际上只是在Matlab的命令窗口中触发该命令:imshow(resX);

Also try, imshow(resY) 也尝试一下,imshow(resY)

According to the second part of your homework you have solved it, ie, 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). 为了进一步简化可视化效果,还假设您将方向从0开始减少至30度,直至180度。例如,如果您的方向为35度,则将其视为30度(因为它是最近的在您的简化清单中)。 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). 接下来,我们将看到图像以及考虑了Sobel并将其离散化为30度(黑色表示0度方向)的渐变方向的可视化效果。

在此处输入图片说明在此处输入图片说明

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. 例如,您可以从Otsu方法提供的方法开始,然后根据要解决的问题基于其他一些直方图分析来减小或增加其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM