I want to calculate the length of some lines in this BMP Image.
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. What if length is increasing diagonally ?
If any one can provide a mathematical formula that would be great.
The length when the direction is 1 pixel in diagonal is 1.4142135623730950488016887242097.
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)). If we take as origin (0,0) and as target (1,1) we know the distance vector between origin and target is (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.
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:
Vector2 origin = new Vector2(originPixelX, originPixelY); Vector2 target = new Vector2(targetPixelX, targetPixelY); Vector2 distanceVector = target - origin; float length = distanceVector.Length();
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:
var length = straightPixels + (new Vector2(diagonalPixels, diagonalPixels)).Length();
If you don't want to use the System.Numeric you can do:
var length = straightPixels + Math.Sqrt(diagonalPixels * diagonalPixels + diagonalPixels * diagonalPixels);
Very simple and effective.