Tags

, , , , , , , , , , , , ,

Arkwood was washing his underpants in the sink again. ‘Stop doing that!’ I screamed, but he paid no heed. For he is a lovesick puppy, scrubbing up for a second date. ‘This one’s for keeps,’ he said, before turning to matters at hand.

I promised to find a star in the night sky and name it after Arkwood’s true love. ‘She will not be wooed to bed for anything less,’ he sighed. So be it, and I booted up my computer.

Up till now I’ve been using OpenCV and Python to interrogate images of space. But there are other computer vision software available, and it would do no harm to add their tools to my buckled leather belt.

Enter AForge.NET, an open source C# framework for computer vision and artificial intelligence. Downloading and running its 2.2.5 version installer on my Windows 7 Home Premium PC added a bunch of folders at C:\Program Files (x86)\AForge.NET\Framework including:

  • Release dlls
  • Source code
  • Sample apps
  • Offline documentation

Note: the source and sample projects are targeting Microsoft .NET Framework 2.0 using Visual C# Express 2010.

Now, I’ve been using the excellent and free Python Tools for Visual Studio for my OpenCV and Python projects. For AForge.NET, I will download Visual Studio Express 2013 for Windows Desktop and create a new C# Console application.

AForge.NET has libraries that cover image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc. To get started, let’s take a peek at the Mathematical Morphology Filters.

Morphology will allow me to remove noise (i.e. small objects) from my images of space, thus assist in finding a new star for Arkwood’s sweetheart. My post OpenCV Morphological Transformations in Space used OpenCV and Python to this end.

Here’s the code that I have dropped into the Program.cs of my new C# Console application:

using System.Drawing;
using AForge.Imaging.Filters;

namespace Morphology
{
    class Program
    {
        static void Main(string[] args)
        {
            // load image
            var image = (Bitmap)Image.FromFile("space.jpg");

            // apply filter
            var structuringElement = new short[5,5] {{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1}};
            
            Opening filter = new Opening(structuringElement);
            
            var filteredImage = filter.Apply(image);

            // save image 
            filteredImage.Save("filtered_space.jpg");
        }
    }
}

First up, we need to add a few dll references to our application. Simply right-click the References folder, Add Reference… and browse to the AForge.Imaging dll in the folder C:\Program Files (x86)\AForge.NET\Framework\Release. You’ll also need to add a reference to System.Drawing from Assemblies.

The code itself is fairly straightforward. We load our space.jpg image into Bitmap format. A new instance of the Opening class processes our image, using erosion and dilatation to remove noise. The filtered image is then saved to disk.

Our space image is in the bin\Debug folder of our application. It is 8 bpp (bits per pixel) grayscale – a suitable format for our filter (as indeed are 16 bpp grayscale, 24 bpp colour and 48 bpp colour).

Note that we are passing our filter a structuring element of size [5,5] – this is the same size kernel we used for our OpenCV post. If we choose not to pass a structuring element then a default size of [3,3] is used.

Okay, let’s run the code. We can inspect the image before and after our morphology filter, to see if noise has been removed.

Original image:

AForgeNET_MathematicalMorphologyFilters_Space

Filtered image:

AForgeNET_MathematicalMorphologyFilters_FilteredSpace

Yep. If you look closely you’ll see that the filter has indeed removed some space dust.

I told Arkwood the good news.

‘Oh joy!’ my puny little Belgian friend exclaimed. ‘Doris will think me a saint for finding a star and naming it after her. She will permit me to jump her bones!’

Not so fast, I told him, There is still much work to do. For now you are merely a saint with a Plastic Halo.

P.S.

There is a handy little app in the AForge.NET download which can provide detail of your image, such as its bpp: C:\Program Files (x86)\AForge.NET\Framework\Samples\Imaging\ImageViewer