Advertisement
Guest User

Untitled

a guest
Mar 29th, 2014
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Windows.Forms;
  6. using AForge;
  7. using AForge.Imaging;
  8. using AForge.Imaging.Filters;
  9. using AForge.Math.Geometry;
  10. using Image = System.Drawing.Image;
  11.  
  12. namespace FindBulletHoles
  13. {
  14.     public partial class MainForm : Form
  15.     {
  16.         private string imagePath = @"PATH TO YOUR IMAGE";
  17.  
  18.         public MainForm()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void MainForm_Load(object sender, EventArgs e)
  24.         {
  25.             Image img = new Bitmap(imagePath);
  26.             TargetPictureBox.Image = new Bitmap(img, new Size(430, 430));
  27.         }
  28.  
  29.         private void FilterButton_Click(object sender, EventArgs e)
  30.         {
  31.             Bitmap bmp = new Bitmap(TargetPictureBox.Image);
  32.  
  33.             EuclideanColorFiltering filter = new EuclideanColorFiltering();
  34.             filter.CenterColor = new RGB(99, 170, 94);
  35.             filter.Radius = 35;
  36.             filter.ApplyInPlace(bmp);
  37.  
  38.             TargetPictureBox.Image = bmp;
  39.         }
  40.  
  41.         private void FindCirclesButton_Click(object sender, EventArgs e)
  42.         {
  43.             Bitmap bmp = new Bitmap(TargetPictureBox.Image);
  44.             BitmapData bmpData = bmp.LockBits(
  45.                 new Rectangle(0, 0, bmp.Width, bmp.Height),
  46.                 ImageLockMode.ReadWrite,
  47.                 bmp.PixelFormat);
  48.  
  49.             BlobCounter counter = new BlobCounter();
  50.             counter.FilterBlobs = true;
  51.             counter.MinHeight = 5;
  52.             counter.MinWidth = 5;
  53.  
  54.             counter.ProcessImage(bmpData);
  55.             Blob[] blobs = counter.GetObjectsInformation();
  56.  
  57.             bmp.UnlockBits(bmpData);
  58.  
  59.             SimpleShapeChecker checker = new SimpleShapeChecker();
  60.             checker.RelativeDistortionLimit = 0.2f;
  61.  
  62.             Graphics graphics = Graphics.FromImage(bmp);
  63.  
  64.             for (int i = 0; i < blobs.Length; i++)
  65.             {
  66.                 List<IntPoint> edges = counter.GetBlobsEdgePoints(blobs[i]);
  67.  
  68.                 AForge.Point center;
  69.                 float radius;
  70.  
  71.                 if (checker.IsCircle(edges, out center, out radius))
  72.                 {
  73.                     graphics.DrawEllipse(Pens.Red,
  74.                         (float)(center.X - radius), (float)(center.Y - radius),
  75.                         (float)(radius * 2), (float)(radius * 2));
  76.                 }
  77.             }
  78.  
  79.             graphics.Dispose();
  80.             TargetPictureBox.Image = bmp;
  81.  
  82.         }
  83.  
  84.         private void FindBulletsButton_Click(object sender, EventArgs e)
  85.         {
  86.             BulletsListView.Items.Clear(); //clear list
  87.             //Filter the background so that only the green coloured holes are visible
  88.             Bitmap bmp = new Bitmap(TargetPictureBox.Image);
  89.  
  90.             //will turn every color except colours close to 99,170,94 black
  91.             EuclideanColorFiltering filter = new EuclideanColorFiltering();
  92.             filter.CenterColor = new RGB(99, 170, 94);
  93.             filter.Radius = 35;
  94.             filter.ApplyInPlace(bmp);
  95.        
  96.             BitmapData bmpData = bmp.LockBits(
  97.                 new Rectangle(0, 0, bmp.Width, bmp.Height),
  98.                 ImageLockMode.ReadWrite,
  99.                 bmp.PixelFormat);
  100.  
  101.             //use BlobCounter to count how many objects are in the image
  102.             BlobCounter counter = new BlobCounter();
  103.             counter.FilterBlobs = true;
  104.             counter.MinHeight = 5;
  105.             counter.MinWidth = 5;
  106.  
  107.             counter.ProcessImage(bmpData);
  108.             Blob[] blobs = counter.GetObjectsInformation();
  109.             bmp.UnlockBits(bmpData);
  110.  
  111.             SimpleShapeChecker checker = new SimpleShapeChecker();
  112.             checker.RelativeDistortionLimit = 0.2f;
  113.  
  114.             //make a resized copy of the original to draw to, we don't want to
  115.             //draw our markers on to a black screen
  116.             Image original = new Bitmap(imagePath);
  117.             Image clone = new Bitmap(original, new Size(430, 430));
  118.  
  119.             Graphics graphics = Graphics.FromImage(clone);
  120.  
  121.             //loop through each blob and identify the shape
  122.             for (int i = 0; i < blobs.Length; i++)
  123.             {
  124.                 List<IntPoint> edges = counter.GetBlobsEdgePoints(blobs[i]);
  125.  
  126.                 AForge.Point center;
  127.                 float radius;
  128.  
  129.                 //we have a bullet hole!
  130.                 if (checker.IsCircle(edges, out center, out radius))
  131.                 {
  132.                     //draw some markers
  133.                     graphics.DrawEllipse(Pens.Red,
  134.                         (float)(center.X - radius), (float)(center.Y - radius),
  135.                         (float)(radius * 2), (float)(radius * 2));
  136.  
  137.                     //update form
  138.                     FoundBullet(center, radius);
  139.                 }
  140.             }
  141.  
  142.             graphics.Dispose();
  143.             TargetPictureBox.Image = clone;
  144.  
  145.         }
  146.  
  147.         protected void FoundBullet(AForge.Point center, float radius)
  148.         {
  149.             ListViewItem item = new ListViewItem(
  150.                 new []
  151.                 {
  152.                     center.X.ToString(),
  153.                     center.Y.ToString(),
  154.                     (radius * 2).ToString(),
  155.                     (radius * 2).ToString()
  156.                 });
  157.  
  158.             BulletsListView.Items.Add(item);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement