梯度幅度的单位和极限是多少? [英] What are the units and limits of gradient magnitude?

查看:262
本文介绍了梯度幅度的单位和极限是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像梯度幅度的单位和界限是什么?例如,我知道如何获取图像的梯度大小(请参见下文).然后生成的Mat将包含源图像中每个边缘的边缘强度(大小).

What are the units and limits of a image gradient magnitude? For example I know how to get the gradient magnitude of an image (see below). And the resulting Mat will contain the edge strength (magnitude) of each edge in my source image.

但是边缘强度/幅度"的单位是什么?梯度方向以度/弧度为单位,量级以什么单位?在OpenCV中,幅值的限制是什么?是0到1,即一条边的强度/大小在0到1之间,而1完全垂直吗?

But what are the units of that 'Edge Strength/Magnitude'? Gradient orientation is in degrees/radians units, what units is magnitude in? And what are the limits of the Magnitude in OpenCV? Is it 0 to 1, ie a edge has a strength/magnitude of anywhere between 0 and 1 where 1 is completely vertical?

因此,如果我要在直方图中绘制幅度; x轴表示边缘强度/陡度,y轴表示具有该强度/陡度的像素数?我说得对吗?

So if I were to plot the magnitude in a histrogram; the x axis represents the edge strength/steepness and the y naxis represents the number of pixels with that strength/steepness? Am I correct?

Mat sX, sY, mag;
Sobel(src, sX, CV_32F, 1, 0, 1);
Sobel(src, sY, CV_32F, 0, 1, 1);

magnitude(sX, sY, mag);

// So mag now contains the image gradient magnitude 
// of the all the edges I pulled out by sobel.
// What are the units and limits of 'edge strength'/magnitude?
// For example are the limits 0 to 1?

推荐答案

单位

您正在获取函数的一些近似导数.如果函数是f(x),那么请记住,您正在查看f中的 change ,而不是x中的 change .假设函数是基于时间r(t)的位置,则导数的单位是位置(距离)与时间(时间)之差的差.那么图像的单位是什么?好吧,它们是某些位置的光度值.亮度的变化只是亮度,位置的变化是距离.因此,导数的单位是光度/距离.

Units

You're taking some approximate derivative of a function. If the function is, say, f(x), then remember you're looking at the change in f over the change in x. Say the function is position based on time r(t), then the units of the derivative are difference of position (distance) over difference of time (time). So what are the units of an image? Well, they are luminosity values at certain positions. The change in luminosity value is just a luminosity value, and change in position is a distance. So the units of the derivative is luminosity/distance.

由于我们正在处理图像,因此最小的距离是一个像素,最大的变化可能是从白色到黑色(反之亦然),因此这些将对应于最大的渐变.但是,Sobel可以处理任意矩阵,它们的最大值和最小值可能在0到1或0到255之外.

Since we're working with an image, the smallest distance is one pixel and the largest change possible is from white to black (or vice versa), so those would correspond with the largest gradients. But Sobel can work with arbitrary matrices whose min and max values could be well outside of 0 to 1 or 0 to 255.

请注意,您可能会得到负值的斜率:以像素为单位的距离始终为正,但是从白色到黑色以及从黑色到白色的变化具有相反的符号. Sobel计算完这些导数后,您将与 angle 分开计算幅值.您可以根据每个方向上的渐变强度对xy方向进行加权来计算角度,并且需要使用符号返回0到360之间的任意角度.

Notice that you could get negative values for the slope: the distance in pixels is always positive, but the change from white to black and black to white have opposite signs. After Sobel calculates those derivatives, you'll calculate the magnitude separately from the angle. You can calculate the angle based on weighting the x and y directions by the strength of the gradient in each direction, and it needs the sign to return any angle between 0 and 360.

如果您希望所有边缘的幅度为正值,非边缘为0,则可以采用 L1-norm (即abs(x) + abs(y)),也可以采用欧几里得语 L2-范数,其中

If you want a positive value for the magnitude of all edges, and 0 for non-edges, you can either take the L1-norm, which is abs(x) + abs(y), or take the Euclidean or L2-norm with the magnitude function which is sqrt(G(x)^2 + G(y)^2), like you would to calculate the hypotenuse of a triangle. Direct adding would mean some of the gradients were positive, and some were negative, leaving you with a grey image showing black and white edges.

Sobel运算符只计算像素附近的导数,而不只是比较两个像素,而是比较六个像素,然后对其加权,然后将它们全部加起来,因此它可能比图像中的值高一点.而且,浮点图像不会在0或1处被截断,因此,您可以发送具有更大值的图像并获得更大的值.除了数据类型可以容纳的最大值外,操作员没有虚拟最大值. Sobel运算符还可以在进行梯度计算之前进行一些平滑操作以去除小边缘,但是该平滑运算符不会缩放这些值.

The Sobel operator simply calculates the derivative in a neighborhood of pixels, not just comparing two, but six pixels, and weights them, adding them all up---so it can be a bit higher than values in your image. And moreso, floating point images don't get truncated at 0 or 1 so, you could send images with much larger values and get much larger values out. There is no virtual maximum for the operator outside of the maximum values that data type can hold. The Sobel operator also does some smoothing before the gradient calculation to remove small edges, but the smoothing operator does not scale the values.

OpenCV Sobel的文档显示了值运算符乘以您的图片.具体来说,对于x方向,每个3x3像素邻域都将逐元素相乘

The OpenCV docs for Sobel shows the values the operator multiplies your image by. Specifically, for the x direction, each 3x3 pixel neighborhood gets elementwise multiplied by

-1 0 1
-2 0 2
-1 0 1

加起来.如果您图像类型的最大可能值为M,最小值为m,则渐变中的最大正值为

and summed. If the largest possible value of your image type is M and the smallest value is m then the largest positive value in your gradient is

(1+2+1)*M - (1+2+1)*m = 4*M - 4*m

最大负值也是

-(1+2+1)*M + (1+2+1)*m = -4*M + 4*m

在每个方向上的渐变都是相同的.因此,从Sobel开始的每个方向上的渐变范围将为[-4M+4m, 4M-4m].

This is the same for your gradient in each direction. So, the range of your gradient in each direction from Sobel will be [-4M+4m, 4M-4m].

您将以L1或L2范数以某种方式将这些幅度中的两个相加.假设您坚持使用L2范数,那么按照L2范数的定义,合并幅度的最大值将简单地是

You'll be adding two of these magnitudes together in some way, either with the L1- or L2-norm. Supposing you stick with the L2-norm, the maximum of the combined magnitude would simply be, following the L2-norm definition,

MAX = sqrt((4*M - 4*m)**2 + (4*M - 4*m)**2) 
    = sqrt(2 * (4*M - 4*m)**2)
    = sqrt(2 * 16 * (M - m)**2) 
    = sqrt(32) * (M - m)

由于L1或L2范数对待正负值相等(它们与0的距离很重要),因此Sobel算子中的最小值在组合大小上给出的响应与最大值相同.当然,您的响应的某些点可能为0,这给出了Sobel响应,因此总和的幅度也为0,所以0将是最小值.

Since the L1 or L2 norm treats positive and negative values equal (their distance from 0 is what matters), the smallest values in the Sobel operator give the same response in the combined magnitude as the largest values do. Of course some points of your response may be 0, which gives Sobel responses and thus summed magnitudes of 0 as well, so 0 will be the minimum value.

如对此问题的另一个答案所述,我们实际上无法在X和同时显示Y方向,如果您计算出实际的最大值,则最终会小一些:

as noted in another answer to this question, we can't actually achieve a maximum in the X and Y direction simultaneously, and if you work out what the maximum can actually be, it ends up being a bit smaller:

sqrt(20) * (M - n)

因此,您可以将梯度归一化为0到1 w.r.t.之间的值.您的图片类型除以最大值.这样一来,您就可以比较多张图像的边缘强度.

Thus you can normalize the gradient to specifically be between 0 and 1 w.r.t. your image type by dividing by the maximum. This would allow you to compare intensity of edges across multiple images.

或者您可以只使用normalize函数,但是最终值将取决于您的图像,因此您无法在图像之间比较相等的值.

Or you could just use the normalize function, but the final values will depend on your images, so you cannot compare equal values across images.

这篇关于梯度幅度的单位和极限是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆