using OpenCV.OpenCV;
using OpenCvSharp;
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;

namespace OpenCV.Forms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private IplImage GetImage()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() != DialogResult.OK)
                return null;

            return new IplImage(dlg.FileName);
        }

        private void btnCanny_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null) 
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.CannyEdge(src);

            CVDialog dlg = new CVDialog();
            dlg.Source = src;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }

        private void btnSobel_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null)
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.SobelEdge(src);

            CVDialog dlg = new CVDialog();
            dlg.Source = src;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }

        private void btnLapace_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null)
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.LaplaceEdge(src);

            CVDialog dlg = new CVDialog();
            dlg.Source = src;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }

        private void btnContour_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null)
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.FindContour(src);

            CVBinDialog dlg = new CVBinDialog();
            dlg.Source = src;
            dlg.Binary = converter.InterimBinaryImage;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }

        private void btnScanContour_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null)
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.ScanContour(src);

            CVBinDialog dlg = new CVBinDialog();
            dlg.Source = src;
            dlg.Binary = converter.InterimBinaryImage;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            IplImage src = GetImage();
            if (src == null)
                return;

            OpenCVClass converter = new OpenCVClass();
            IplImage converted = converter.Binary(src, 150);

            CVDialog dlg = new CVDialog();
            dlg.Source = src;
            dlg.Converted = converted;
            dlg.ShowDialog();

            converter.Dispose();
        }
    }
}
