Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating How much curve in a straight line into a image using processing in C#

I have a images like below enter image description here enter image description here

Here there is a line. This line is not straight everywhere. In the middle part of the line there is a curve. My work is to calculate that how much the line is straight or how much the line is curved ??? How can i do that using C# . I searched everywhere but cant get any idea how to do that. Have anyone any idea how to do that?? My aim is not only to detect a line but also to calculate how much curve the line is??

like image 971
ImonBayazid Avatar asked Dec 12 '25 06:12

ImonBayazid


1 Answers

This is just a sample using the EmguCV wrapper over OpenCV image processing library detecting the lines. Of course this is not 100% accurate, you will have to tweak a little more. When you get the lines you may calculate the curve.

Or another way is to implement your own algorithm to detect the lines.

Using this algorithm I got 16 lines from the provided image. With some tweak you can get more accurate number.

var bmp = new Bitmap(pathToImage);

                //Load the image from file and resize it for display
        using (Image<Bgr, Byte> img = new Image<Bgr, byte>(bmp))
        {
            //Convert the image to grayscale and filter out the noise
            using (Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp())
            {
                Gray cannyThreshold = new Gray(180);

                #region Canny and edge detection

                Gray cannyThresholdLinking = new Gray(120);
                Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);
                LineSegment2D[] lines = cannyEdges.HoughLinesBinary(
                    1, //Distance resolution in pixel-related units
                    Math.PI/60.0, //Angle resolution measured in radians.
                    20, //threshold
                    30, //min Line width
                    10 //gap between lines
                    )[0]; //Get the lines from the first channel

                #endregion
            }
        }
like image 185
Mihai Hantea Avatar answered Dec 13 '25 20:12

Mihai Hantea