梯度大小的单位和界限是什么?
图像梯度幅度的单位和限制是什么?例如,我知道如何获得图像的梯度幅度(见下文).生成的 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 中 Magnitude 的限制是什么?它是 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 n轴表示具有该强度/陡度的像素数?我说的对吗?
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 而不是 change 在 x
中.假设函数是基于时间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 计算出这些导数之后,您将分别计算 magnitude 和 angle.您可以根据每个方向的梯度强度加权x
和y
方向来计算角度,并且它需要符号返回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)
,或采用 Euclidean 或 L2-norm 与 magnitude
函数,即 sqrt(G(x)^2 + G(y)^2)
,就像计算三角形的斜边一样.直接添加意味着一些渐变是正的,一些是负的,给你留下一个显示黑白边缘的灰色图像.
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.
如 another answer 中所述,我们实际上无法在 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.
相关文章