コード例 #1
0
 public void exBlockClickHandler(object sender, EventArgs e)
 {
     if (image != null)
     {
         OpenFileDialog ofd = new OpenFileDialog();
         ofd.Multiselect = false;
         ofd.Title       = "Choose an Image to Comparewith";
         DialogResult result = ofd.ShowDialog();
         if (result == DialogResult.OK)
         {
             string filename = ofd.FileName;
             try
             {
                 Bitmap   searchImage = new Bitmap(filename);
                 Bitmap   sourceImage = new Bitmap(path);
                 CompView mov         = this.WorkItem.SmartParts.AddNew <CompView>();
                 mov.panAndZoomPictureBox1.Image = searchImage;
                 SmartPartInfo spi =
                     new SmartPartInfo("Search Image", "MyOwnDescription");
                 this.WorkItem.Workspaces[WorkspaceNames.TabWorkspace].Show(mov, spi);
                 // collect reference points using corners detector (for example)
                 SusanCornersDetector scd    = new SusanCornersDetector(30, 18);
                 List <IntPoint>      points = scd.ProcessImage(sourceImage);
                 // create block matching algorithm's instance
                 ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching();
                 // process images searching for block matchings
                 List <BlockMatch> matches = bm.ProcessImage(sourceImage, points, searchImage);
                 // draw displacement vectors
                 BitmapData data = sourceImage.LockBits(
                     new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                     ImageLockMode.ReadWrite, sourceImage.PixelFormat);
                 foreach (BlockMatch match in matches)
                 {
                     // highlight the original point in source image
                     Drawing.FillRectangle(data,
                                           new Rectangle(match.SourcePoint.X - 1, match.SourcePoint.Y - 1, 3, 3),
                                           Color.Yellow);
                     // draw line to the point in search image
                     Drawing.Line(data, match.SourcePoint, match.MatchPoint, Color.Red);
                     // check similarity
                     if (match.Similarity > 0.98f)
                     {
                     }
                 }
                 sourceImage.UnlockBits(data);
                 CompView mov2 = this.WorkItem.SmartParts.AddNew <CompView>();
                 mov2.panAndZoomPictureBox1.Image = sourceImage;
                 SmartPartInfo spi2 =
                     new SmartPartInfo("Results", "MyOwnDescription");
                 this.WorkItem.Workspaces[WorkspaceNames.TabWorkspace].Show(mov2, spi2);
             }
             catch
             {
                 MessageBox.Show("Images Should be of same height and width");
                 return;
             }
         }
     }
 }
コード例 #2
0
        private void initiateButton_Click(object sender, RoutedEventArgs e)
        {
            // create grayscale filter (BT709)
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            // apply the filter
            img1 = filter.Apply(img1);
            img2 = filter.Apply(img2);

            Stopwatch st = new Stopwatch();
            st.Start();

            // collect reference points using corners detector (for example)
            SusanCornersDetector scd = new SusanCornersDetector(30, 18);
            List<IntPoint> points = scd.ProcessImage(img1);

            // create block matching algorithm's instance
            ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching(12, 36);
            // process images searching for block matchings
            List<BlockMatch> matches = bm.ProcessImage(img1, points, img2);

            st.Stop();
            TimeSpan elapsed = st.Elapsed;
            timedisp.Text = "Elapsed time = " + elapsed.ToString();

            // draw displacement vectors
            BitmapData data = img1.LockBits(
                new System.Drawing.Rectangle(0, 0, img1.Width, img1.Height),
                ImageLockMode.ReadWrite, img1.PixelFormat);

            foreach (BlockMatch match in matches)
            {
                // highlight the original point in source image
                AForge.Imaging.Drawing.FillRectangle(data,
                    new System.Drawing.Rectangle(match.SourcePoint.X - 1, match.SourcePoint.Y - 1, 3, 3),
                    System.Drawing.Color.Yellow);
                // draw line to the point in search image
                AForge.Imaging.Drawing.Line(data, match.SourcePoint, match.MatchPoint, System.Drawing.Color.Red);

                // check similarity
                if (match.Similarity > 0.98f)
                {
                    // process block with high similarity
                }
            }

            img1.UnlockBits(data);
            bi = Compatibility.Compatibility.BitmapToBitmapImage(img1);
            image1.Source = bi;
        }