Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Windows.Forms;
- using AForge;
- using AForge.Imaging;
- using AForge.Imaging.Filters;
- using AForge.Math.Geometry;
- using Image = System.Drawing.Image;
- namespace FindBulletHoles
- {
- public partial class MainForm : Form
- {
- private string imagePath = @"PATH TO YOUR IMAGE";
- public MainForm()
- {
- InitializeComponent();
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- Image img = new Bitmap(imagePath);
- TargetPictureBox.Image = new Bitmap(img, new Size(430, 430));
- }
- private void FilterButton_Click(object sender, EventArgs e)
- {
- Bitmap bmp = new Bitmap(TargetPictureBox.Image);
- EuclideanColorFiltering filter = new EuclideanColorFiltering();
- filter.CenterColor = new RGB(99, 170, 94);
- filter.Radius = 35;
- filter.ApplyInPlace(bmp);
- TargetPictureBox.Image = bmp;
- }
- private void FindCirclesButton_Click(object sender, EventArgs e)
- {
- Bitmap bmp = new Bitmap(TargetPictureBox.Image);
- BitmapData bmpData = bmp.LockBits(
- new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.ReadWrite,
- bmp.PixelFormat);
- BlobCounter counter = new BlobCounter();
- counter.FilterBlobs = true;
- counter.MinHeight = 5;
- counter.MinWidth = 5;
- counter.ProcessImage(bmpData);
- Blob[] blobs = counter.GetObjectsInformation();
- bmp.UnlockBits(bmpData);
- SimpleShapeChecker checker = new SimpleShapeChecker();
- checker.RelativeDistortionLimit = 0.2f;
- Graphics graphics = Graphics.FromImage(bmp);
- for (int i = 0; i < blobs.Length; i++)
- {
- List<IntPoint> edges = counter.GetBlobsEdgePoints(blobs[i]);
- AForge.Point center;
- float radius;
- if (checker.IsCircle(edges, out center, out radius))
- {
- graphics.DrawEllipse(Pens.Red,
- (float)(center.X - radius), (float)(center.Y - radius),
- (float)(radius * 2), (float)(radius * 2));
- }
- }
- graphics.Dispose();
- TargetPictureBox.Image = bmp;
- }
- private void FindBulletsButton_Click(object sender, EventArgs e)
- {
- BulletsListView.Items.Clear(); //clear list
- //Filter the background so that only the green coloured holes are visible
- Bitmap bmp = new Bitmap(TargetPictureBox.Image);
- //will turn every color except colours close to 99,170,94 black
- EuclideanColorFiltering filter = new EuclideanColorFiltering();
- filter.CenterColor = new RGB(99, 170, 94);
- filter.Radius = 35;
- filter.ApplyInPlace(bmp);
- BitmapData bmpData = bmp.LockBits(
- new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.ReadWrite,
- bmp.PixelFormat);
- //use BlobCounter to count how many objects are in the image
- BlobCounter counter = new BlobCounter();
- counter.FilterBlobs = true;
- counter.MinHeight = 5;
- counter.MinWidth = 5;
- counter.ProcessImage(bmpData);
- Blob[] blobs = counter.GetObjectsInformation();
- bmp.UnlockBits(bmpData);
- SimpleShapeChecker checker = new SimpleShapeChecker();
- checker.RelativeDistortionLimit = 0.2f;
- //make a resized copy of the original to draw to, we don't want to
- //draw our markers on to a black screen
- Image original = new Bitmap(imagePath);
- Image clone = new Bitmap(original, new Size(430, 430));
- Graphics graphics = Graphics.FromImage(clone);
- //loop through each blob and identify the shape
- for (int i = 0; i < blobs.Length; i++)
- {
- List<IntPoint> edges = counter.GetBlobsEdgePoints(blobs[i]);
- AForge.Point center;
- float radius;
- //we have a bullet hole!
- if (checker.IsCircle(edges, out center, out radius))
- {
- //draw some markers
- graphics.DrawEllipse(Pens.Red,
- (float)(center.X - radius), (float)(center.Y - radius),
- (float)(radius * 2), (float)(radius * 2));
- //update form
- FoundBullet(center, radius);
- }
- }
- graphics.Dispose();
- TargetPictureBox.Image = clone;
- }
- protected void FoundBullet(AForge.Point center, float radius)
- {
- ListViewItem item = new ListViewItem(
- new []
- {
- center.X.ToString(),
- center.Y.ToString(),
- (radius * 2).ToString(),
- (radius * 2).ToString()
- });
- BulletsListView.Items.Add(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement