LHT by Accord + AForge



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Accord;
using Accord.Controls;
using Accord.Imaging;
using AForge;
using AForge.Imaging.Filters;

namespace LHT_Moddagy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
        }

        // LHT Calc button ----------------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            HoughLineTransformation lineTransform = new HoughLineTransformation();

            Bitmap sourceImage = (Bitmap)pictureBox1.Image;

            // create the filter
            BradleyLocalThresholding filter = new BradleyLocalThresholding();
            // apply the filter
            filter.ApplyInPlace(sourceImage);

            // create filter
            Invert filtInvert = new Invert();
            // apply the filter
            filtInvert.ApplyInPlace(sourceImage);

            // apply Hough line transofrm
            lineTransform.ProcessImage(sourceImage);
            Bitmap houghLineImage = lineTransform.ToBitmap();

            // get lines using relative intensity
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.1);

            foreach (HoughLine line in lines)
            {
                // get line's radius and theta values
                int r = line.Radius;
                double t = line.Theta;

                // check if line is in lower part of the image
                if (r < 0)
                {
                    t += 180;
                    r = -r;
                }

                // convert degrees to radians
                t = (t / 180) * Math.PI;

                // get image centers (all coordinate are measured relative to center)
                int w2 = pictureBox1.Image.Width / 2;
                int h2 = pictureBox1.Image.Height / 2;

                double x0 = 0, x1 = 0, y0 = 0, y1 = 0;

                if (line.Theta != 0)
                {
                    // non-vertical line
                    x0 = -w2; // most left point
                    x1 = w2;  // most right point

                    // calculate corresponding y values
                    y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                    y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);
                }
                else
                {
                    // vertical line
                    x0 = line.Radius;
                    x1 = line.Radius;

                    y0 = h2;
                    y1 = -h2;
                }

                UnmanagedImage img1 = UnmanagedImage.FromManagedImage(sourceImage);

                // draw line on the image
                Drawing.Line(img1,
                    new Accord.IntPoint((int)x0 + w2, h2 - (int)y0),
                    new Accord.IntPoint((int)x1 + w2, h2 - (int)y1),
                    Color.Red);

                pictureBox2.Image = img1.ToManagedImage();
            }
        }
    }
}

댓글

이 블로그의 인기 게시물

Draw Circle on PictureBox when Button Click - Winform

2D FFT of Gray Image by AForge