简体   繁体   English

使用c#计算BMP图像中的行长的公式

[英]Formula For Calculating Length Of Line In BMP Image using c#

I want to calculate the length of some lines in this BMP Image . 我想计算此BMP 图像中某些行的长度。

I do not know the formula how to calculate these type of lines ? 我不知道公式如何计算这些类型的线? Or there is any other formula to calculate length in image processing. 或者,还有其他公式可以计算图像处理中的长度。

By searching I came to know that we have to +1 in length if line if it go 1 pixel long vertically or horizontally. 通过搜索,我知道如果线垂直或水平地长1像素,则线的长度必须为+1。 What if length is increasing diagonally ? 如果长度沿对角线增加怎么办? If any one can provide a mathematical formula that would be great. 如果有人可以提供一个数学公式,那将是很好的。 Thanks. 谢谢。

The length when the direction is 1 pixel in diagonal is 1.4142135623730950488016887242097. 方向为对角线1像素时的长度为1.4142135623730950950488016887242097。

The math behind this factor is plain vector math, let me explain. 让我解释一下,这个因素背后的数学是简单的矢量数学。 According to vector math we can compute the length of a 2D vector as Math.Sqrt(Vector.X * Vector.X + Vector.Y * Vector.Y)) . 根据向量数学,我们可以将2D向量的长度计算为Math.Sqrt(Vector.X * Vector.X + Vector.Y * Vector.Y)) If we take as origin (0,0) and as target (1,1) we know the distance vector between origin and target is (1,1). 如果我们将原点(0,0)和目标(1,1)设为目标,则我们知道原点和目标之间的距离向量为(1,1)。 So, if we compute the length of this vector we get: sqrt(1 * 1 + 1 * 1) what reduced is sqrt(2) and yields a result of 1.4142135623730950488016887242097. 因此,如果我们计算此向量的长度,我们将得到: sqrt(1 * 1 + 1 * 1)减少的是sqrt(2)并得出1.4142135623730950950488016887242097的结果。

You can use vector math to calculate a bunch of pixels instead of adding this factor to achieve a better precission, to do this just apply the previous math: 您可以使用向量数学来计算一堆像素,而不是添加此因子以获得更好的精度,而只需应用先前的数学即可:

distanceVector = targetVector - originVector
distance = sqrt(distanceVector.X * distanceVector.X + distanceVector.Y * distanceVector.Y)

If you use the System.Numeric package then you can use the integrated functions: 如果使用System.Numeric包,则可以使用集成功能:

Vector2 origin = new Vector2(originPixelX, originPixelY);
Vector2 target = new Vector2(targetPixelX, targetPixelY);

Vector2 distanceVector = target - origin;
float length = distanceVector.Length();

EDIT: 编辑:

To simplify your problem there is a very easy solution which will give you the best precission. 为了简化您的问题,有一个非常简单的解决方案,它将为您提供最佳的解决方案。

If you sum the total of straight pixels and diagonal pixels in two variables, let's name those straightPixels and diagonalPixels you can do this: 如果将两个变量中的直像素和对角像素的总和相加,我们将其命名为straightPixels和对角像素,则可以执行以下操作:

var length = straightPixels + (new Vector2(diagonalPixels, diagonalPixels)).Length();

If you don't want to use the System.Numeric you can do: 如果您不想使用System.Numeric,则可以执行以下操作:

var length = straightPixels + Math.Sqrt(diagonalPixels * diagonalPixels + diagonalPixels * diagonalPixels);

Very simple and effective. 非常简单有效。

If you want only lenght of diagonal for 1 pixel up and right, then it is very simple . 如果您只希望对角线长度为1像素左右,那么这很简单 So it will be square of two. 因此它将是两个的平方。

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

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