using System;
using System.Windows.Forms;
using OpenCV.OpenCV;
using OpenCvSharp;

namespace OpenCV
{
    public partial class Form1 : Form
    {
        IplImage src;

        string fileName;

        public Form1()
        {
            InitializeComponent();
        }

        private string FindFileName()
        {
            string path = "";

            OpenFileDialog dialog = new OpenFileDialog();
            DialogResult dialogResult = dialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
                path = dialog.FileName;

            return path;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                fileName = FindFileName();
                if (String.IsNullOrEmpty(fileName))
                    return;

                src = new IplImage(fileName);
                OpenCVClass Converter = new OpenCVClass();

                pictureBoxIpl1.ImageIpl = src;

                Converter.ConvKernel = new IplConvKernel(3, 3, 1, 1, ElementShape.Rect);
                pictureBoxIpl2.ImageIpl = Converter.MorphologyImage(src, MorphologyOperation.BlackHat, 10);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cv.ReleaseImage(src);
            if (src != null)
                src.Dispose();
        }
    }
}
