private static void LineSegmentDetectorSample() { var img = new Mat("data/shapes.png", ImreadModes.GrayScale); var lines = new Mat(); var view = img.Clone(); var detector = LineSegmentDetector.Create(); detector.Detect(img, lines); detector.DrawSegments(view, lines); Window.ShowImages(view); }
private void button2_Click(object sender, EventArgs e) { if (File.Exists(textBox1.Text)) { Bitmap bmp = new Bitmap(textBox1.Text); //Bitmap bmpT = (Bitmap)bmp.GetThumbnailImage(500, (int)((float)bmp.Height / ((float)bmp.Width / 500)), null, IntPtr.Zero); //use this image if tou want to autorotate by text lines using (LineSegmentDetector lsd = new LineSegmentDetector(MakeGrayscaleBitmap(bmp))) { lsd.FindLines(0.6); //lsd.FindLines(0.5); //specified scale for image using (Graphics gr = Graphics.FromImage(bmp)) { gr.SmoothingMode = SmoothingMode.AntiAlias; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; if (lsd.Lines.Length > 0) { foreach (LSDLine ll in lsd.Lines) { LSDLine l = new LSDLine(ll.P1, ll.P2); using (Pen thick_pen = new Pen(Color.Red, 2)) { gr.DrawLine(thick_pen, ll.P1, ll.P2); } } } } LSDLine hl = new LSDLine(new Point(0, 0), new Point(1, 0)); var lgroup = lsd.Lines.GroupBy(x => (int)x.GetExteriorAngleDegree(hl), new simAngleLines()); var lgroupG = lgroup.GroupBy(m => m.Key); var ang = (from i in lgroupG orderby i.Sum(x => x.Count()) descending select i.Key).ElementAtOrDefault(0);//avg angle of lines foreach (var u in lgroup) { Debug.WriteLine(u.Count() + " : " + u.Key); } this.Text = "Lines count = " + lsd.Lines.Length.ToString() + ", avg angle=" + ang.ToString(); pictureBox1.Image = bmp; //=RotateImage(bmp,ang); //use this to rotate image by avg lines angle } } }