2018年2月24日土曜日

画像ファイルのノルム値を比較

- input側のtextboxにファイルをまとめてドラッグ&ドロップ
- "計算"ボタンでoutput側textboxにファイル名とノルム地をカンマ区切りで表示

using OpenCvSharp;

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

        // 入力ファイルリスト
        List<string> sourceFileList = new List<string>();

        /// <summary>
        /// Ctrl-A対応
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_DiffFiles_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
                textBox_DiffFiles.SelectAll();
        }


        private void textBox_InputFiles_Enter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                e.Effect = DragDropEffects.Copy;

            }
            else
            {

                e.Effect = DragDropEffects.None;

            }
        }

        private void textBox_InputFiles_Drop(object sender, DragEventArgs e)
        {
            // 操作対象のリストをクリアする
            sourceFileList.Clear();
            textBox_InputFiles.Text = "";


            // ファイルリスト更新
            string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string file in fileName)
            {
                if (File.Exists(file))
                {
                    string ext = Path.GetExtension(file).ToUpper();
                    if (ext == ".JPG" || ext == ".BMP" || ext == ".PNG")
                    {
                        // 入力ファイルリストに追加
                        sourceFileList.Add(file);

                        // 表示更新
                        string dispStr = Path.GetFileName(file);
                        textBox_InputFiles.Text += dispStr + "\r\n";
                    }
                }
            }
        }

        private void button_Exec_Click(object sender, EventArgs e)
        {
            String outStr = "";

            foreach(String File in sourceFileList)
            {
                Mat image = new Mat(File);

                image.Canny(120, 80);
                double norm = Cv2.Norm(image);

                outStr = Path.GetFileName(File) + "," + norm.ToString() + "\r\n";

                textBox_DiffFiles.Text += outStr;
            }
        }
    }
}

2018年2月18日日曜日

ファイルをまとめてドラッグ&ドロップ

1. ファイルをまとめてテキストボックスにドラッグ&ドロップ
2. テキストボックスに↑のファイルを表示

という動作をさせます。

1. FormにTextBoxを配置
2. TextBoxのプロパティのAllowDropをtrueにする
3. 上記TextBoxに "DragEnger" , "DragDrop"イベントを追加
4. 上記イベントを実装
        private void textBoxPhotoFile_Enter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            } else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        // 入力ファイルリスト
        List<string> sourceFileList = new List<string>();

        /// <summary>
        /// 操作対象の画像ファイルをドラッグ & ドロップした際にfileListとして登録する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxPhotoFileDrop(object sender, DragEventArgs e)
        {
            // 操作対象のリストをクリアする
            sourceFileList.Clear();
            textBox_InputFile.Text = "";

            // ファイルリスト更新
            string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string file in fileName)
            {
                if (File.Exists(file))
                {
                    string ext = Path.GetExtension(file).ToUpper();
                    if(ext == ".JPG")
                    {
                        // 入力ファイルリストに追加
                        sourceFileList.Add(file);

                        // 表示更新
                        string dispStr = Path.GetFileName(file);
                        textBox_InputFile.Text += dispStr + "\r\n";
                    }
                }
            }
        }