예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary <string, Image <Bgr, byte> > bgrImages = new Dictionary <string, Image <Bgr, byte> >();

            //--Initialize openFiledialogue---
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter           = "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
            openFileDialog1.FilterIndex      = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Title            = "Image Selection";
            openFileDialog1.Multiselect      = true;
            //--------------------------------

            DialogResult dialogresult = openFileDialog1.ShowDialog();

            //---Save Images in Dictionary and display in ListBox----
            if (dialogresult == System.Windows.Forms.DialogResult.OK)
            {
                if (imagesloaded)
                {
                    //reset and clear everything if images were loaded before
                    reset();
                    cleanLoadedImages.Clear();
                    LoadedImages.Clear();
                    workImages.Clear();
                    listBox1.Items.Clear();
                    //listBox1.Update();
                }

                int currentImage = 0;
                // Read the files
                listBox1.BeginUpdate();
                foreach (String file in openFileDialog1.FileNames)
                {
                    // Create a PictureBox.
                    try
                    {
                        //Convert Images to Emgu Format
                        Image  tempImage  = Image.FromFile(file);
                        Bitmap tempBitmap = new Bitmap(tempImage);
                        Emgu.CV.Image <Bgr, byte> cleanImage  = new Emgu.CV.Image <Bgr, byte>(tempBitmap);
                        Emgu.CV.Image <Hsv, Byte> loadedImage = new Emgu.CV.Image <Hsv, byte>(tempBitmap);
                        string loadedImageName = "Image:" + currentImage;
                        //Save Images
                        listBox1.Items.Add(loadedImageName);
                        cleanLoadedImages.Add(loadedImageName, cleanImage);
                        LoadedImages.Add(loadedImageName, loadedImage.SmoothMedian(mediansize));
                        if (checkBox1.Checked)
                        {
                            bgrImages.Add(loadedImageName, cleanImage.SmoothGaussian(gaussiansize));
                        }
                    }

                    catch (Exception ex)
                    {
                        // Could not load the image - probably related to Windows file system permissions.
                        MessageBox.Show("Cannot load the image: " + file.Substring(file.LastIndexOf('\\'))
                                        + ".\n\n" + ex.Message);
                    }
                    currentImage++;
                }
                listBox1.EndUpdate();
                imagesloaded = true;

                if (checkBox1.Checked)
                {
                    LoadedImages = BackgroundSubtractor.getWithoutBackground(bgrImages);
                }
                listBox1.SelectedIndex = 0;
            }
            //----------------------------------------
        }