Codementor Events

Basic Concept of Computer Vision

Published Jan 04, 2020Last updated Feb 05, 2024
Basic Concept of Computer Vision

Tutorial - Computer Vision using C# and AForge Imaging

This tutorial shows how computers or machines visualize an image. In this algorithm the developers have used the C# Programming Language and the AForge Imaging Platform. The program allows the user to visualize how a computer/machine could find or determine a pattern using the concept of binarization.
Save.png

Understanding the Computer Vision Concept

Flowchart.JPG
The concept shows how a computer/machine visualizes an image given that it is binarized. The process of Binarization means that the only present values are 1's and 0's. Visually speaking in an image there should be only Black or White. But before you could binarize an image you should apply the concept of Grayscaling wherein the concept of RGB is nullified thus using the same values for Red, Green and Blue creating the effect of making the image have different shades of Gray.
Process.JPG

Code Proper - Image Processor.cs

Finally this part shows how the algorithm works for the users to visualize the binariation concept.
Download Project here: Image Processor v1.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;

namespace ImageProcessor
{
    public partial class ImageProcessor : Form
    {
        public ImageProcessor()
        {
            InitializeComponent();
            this.Text = "Image Processor v" + Assembly.GetEntryAssembly().GetName().Version; 
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
                    LogAction("Successfully Opened an Image!");
                }
            }
            catch (Exception ex)
            { throw ex; }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Bitmap image = (Bitmap)pictureBox1.Image;
            image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
            if (checkBox1.Checked)
            {
                Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
                image = filter.Apply(image);
                pictureBox2.Image = image;
                LogAction("Grayscaling was successfully applied!");
                ActivateButtons(checkBox1.Checked);
            }
            else
            {
                pictureBox2.Image = image;
                LogAction("Grayscaling was successfully reversed!");
                ActivateButtons(checkBox1.Checked);
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            try
            {
                label1.Text = "Threshold Level: " + trackBar1.Value;
                Bitmap image = (Bitmap)pictureBox1.Image;
                image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                if (checkBox1.Checked)
                {
                    Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);
                    Threshold filter = new Threshold(trackBar1.Value);
                    image = grayfilter.Apply(image);
                    image = filter.Apply(image);
                    LogAction("Successfully applied Threshold Level: " + trackBar1.Value);
                }
                pictureBox2.Image = image;
            }
            catch (Exception ex) { throw ex; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WaitCallback del = delegate
            {
                Invoke(new Action(() =>
                {
                    try
                    {
                        Bitmap image = (Bitmap)pictureBox2.Image;
                        image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                        if (checkBox1.Checked)
                        {
                            Erosion filter = new Erosion();
                            filter.ApplyInPlace(image);
                            LogAction("Erosion was successfully applied!");
                        }
                        pictureBox2.Image = image;
                    }
                    catch (Exception ex) { throw ex; }
                }));
            };
            ThreadPool.QueueUserWorkItem(del);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WaitCallback del = delegate
            {
                Invoke(new Action(() =>
                {
                    try
                    {

                        Bitmap image = (Bitmap)pictureBox2.Image;
                        image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                        if (checkBox1.Checked)
                        {
                            Dilatation filter = new Dilatation();
                            filter.ApplyInPlace(image);
                            LogAction("Dilatation was successfully applied!");
                        }
                        pictureBox2.Image = image;

                    }
                    catch (Exception ex) { throw ex; }
                }));
            };
            ThreadPool.QueueUserWorkItem(del);
        }

        private void ActivateButtons(bool Activation)
        {
            try
            {
                button1.Enabled = Activation;
                button2.Enabled = Activation;
                trackBar1.Enabled = Activation;
            }
            catch (Exception ex) { throw ex; }
        }

        private void LogAction(string Log)
        {
            try
            {
                richTextBox1.AppendText(DateTime.Now.ToString("MM/dd/yyyy - hh:mm:ss") + " " + Log);
                richTextBox1.AppendText(Environment.NewLine);
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                richTextBox1.ScrollToCaret();
            }
            catch (Exception ex) { throw ex; }
        }
    }
}

Operation of Image Processor

How to Operate the Image Processor Project

  • Go to File>Open> then find a supported image file.
  • Put a check on the Grayscale Overlay.
  • Apply Binarization on a certain Threshold (Until target/s are seen).
  • Use Erosion or Dilatation for further exposure.
Discover and read more posts from Jonathan James Acoba
get started
post commentsBe the first to share your opinion
Show more replies