Click here to Skip to main content
15,888,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My project is to detect the lines on a picture, I know using houghline function(openCVsharp), but the result gives me many parallel lines. Could you guide me on how to merge these parallel lines?

C#
var gray = OpenCvSharp.Cv2.ImRead(filename, OpenCvSharp.ImreadModes.Grayscale);
var binary = gray.Threshold(0, 255, OpenCvSharp.ThresholdTypes.BinaryInv | OpenCvSharp.ThresholdTypes.Otsu);

var element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5),
                            new OpenCvSharp.Point(-1, -1));

var Morp_out = new Mat();
Cv2.MorphologyEx(binary, Morp_out,MorphTypes.Open,element);        
        var Ero_out = new Mat();
Cv2.Erode(Morp_out, Ero_out, element);
        using (new Window("Erode Image", WindowMode.AutoSize, Ero_out))
        {
            Window.WaitKey(0);
        }
 var processed_img = Ero_out;
 double min_Line_length = 100;
 double max_line_gap = 10;
 double rho = 1;
 double theta = Math.PI / 180;
 int threshold = 100;
 LineSegmentPoint[] setH_P = Cv2.HoughLinesP(processed_img, rho, theta, threshold, min_Line_length, max_line_gap);
 Mat Img_outPut = processed_img.EmptyClone();
        foreach  (LineSegmentPoint s in setH_P)
        {
            Img_outPut.Line(s.P1, s.P2, Scalar.White, 1);
        }
 using (new Window("HoughLinesP", WindowMode.AutoSize, Img_outPut))
        {
            Window.WaitKey(0);
        }


What I have tried:

My idea is
compare all possible line segment pairs, then check the distance between all possible endpoints, then compare it with a threshold, and draw it. However, I do not have any experience with programming, so it is kind of difficult for me to implement it
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900