Sensor Garis Menggunakan Webcam

Berikut project yang akan menggunakan library image processing AForge.NET dengan bahasa pemrograman C# dan menggunakan software Visual C# Express 2010. Berikut tampilan GUI-nya.

Project ini bermaksud sebagai sensor pendeteksi garis pada robot pengikut garis (line follower robot) sebagai pengganti sensor proximity photodioda.

Berikut kodenya.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;

namespace line_tracker2
{
    public partial class Form1 : Form
    {
        //reference sudah ditambahkan
        private FilterInfoCollection VideoCaptureDevices;//mengumpulkan setiap perangkat video yg terdeteksi
        private VideoCaptureDevice FinalVideoSource;//menampung perangkat video yang akan digunakan
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //pertama add reference semua Aforge.video.dll dan Aforge.video.directshow.dll
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            //sekarang perangkat video sudah tersimpan pada array di atas
            foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
            {
                comboBox1.Items.Add(VideoCaptureDevice.Name);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //tambahkan kode berikut untuk menentukan perangkat sumber dari video
            FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
            FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
            FinalVideoSource.Start();
        }
        private Rectangle objectRect;
        int x1, y1,x1_bawah, y1_bawah;
        void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            //sekarang kita tambahkan kode berikut untuk menampilkan new frame event (frame dari webcam) untuk menampilkan gambar pada picturebox
            Bitmap image = (Bitmap)eventArgs.Frame.Clone();//bitmap boxing
            Bitmap image_unfilter = (Bitmap)eventArgs.Frame.Clone();
            Bitmap image_bawah = (Bitmap)eventArgs.Frame.Clone();//bitmap boxing
            Bitmap image_unfilter_bawah = (Bitmap)eventArgs.Frame.Clone();

            Crop filter = new Crop(new Rectangle(0, 0, image.Width, (image.Height/8)));
            // apply the filter
            image = filter.Apply(image);
            image_unfilter = filter.Apply(image_unfilter);

            Crop filter_bawah = new Crop(new Rectangle(0, 320, image_bawah.Width, (image_bawah.Height / 8)));
            // apply the filter
            image_bawah = filter_bawah.Apply(image_bawah);
            image_unfilter_bawah = filter_bawah.Apply(image_unfilter_bawah);

            // create filter
            ColorFiltering filtercr = new ColorFiltering();
            // set color ranges to keep
            filtercr.Red = new IntRange(200, 255);
            filtercr.Green = new IntRange(200, 255);
            filtercr.Blue = new IntRange(200, 255);
            filtercr.FillOutsideRange = true;
            // apply the filter
            filtercr.ApplyInPlace(image);
            filtercr.ApplyInPlace(image_bawah);

            // create blob counter and configure it
            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinWidth = 20;                    // set minimum size of
            blobCounter.MinHeight = 20;                   // objects we look for
            blobCounter.FilterBlobs = true;               // filter blobs by size
            blobCounter.ObjectsOrder = ObjectsOrder.Size; // order found object by size
            // locate blobs
            blobCounter.ProcessImage(image);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            // draw rectangle around the biggest blob
            if (rects.Length > 0)
            {
                objectRect = rects[0];
                Graphics g = Graphics.FromImage(image_unfilter);

                using (Pen pen = new Pen(Color.YellowGreen, 3))
                {
                    g.DrawRectangle(pen, objectRect);
                }

                g.Dispose();
                // calculate X,Y coordinates of object's center
                x1 = (objectRect.Left + objectRect.Right - image.Width) / 2;
                y1 = (image.Height - (objectRect.Top + objectRect.Bottom)) / 2;
            }

            blobCounter.ProcessImage(image_bawah);
            Rectangle[] rects_bawah = blobCounter.GetObjectsRectangles();
            // draw rectangle around the biggest blob
            if (rects_bawah.Length > 0)
            {
                objectRect = rects_bawah[0];
                Graphics g = Graphics.FromImage(image_unfilter_bawah);

                using (Pen pen = new Pen(Color.YellowGreen, 3))
                {
                    g.DrawRectangle(pen, objectRect);
                }

                g.Dispose();
                // calculate X,Y coordinates of object's center
                x1_bawah = (objectRect.Left + objectRect.Right - image_bawah.Width) / 2;
                y1_bawah = (image_bawah.Height - (objectRect.Top + objectRect.Bottom)) / 2;
            }

            pictureBox1.Image = image_unfilter;
            pictureBox2.Image = image_unfilter_bawah;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //kita tambahkan kode untuk membuat webcam berhenti bekerja
            //kapanpun form telah ditutup maka webcam akan berhenti bekerja secara automatis
            if (FinalVideoSource.IsRunning)
            {
                FinalVideoSource.Stop();
            }
            //selesai
            //sekarang kita jalankan
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = String.Format("koordinat_atas: x={0}y={1} koordinat_bawah: x={2}y={3} delta_x = {4}", x1, y1, x1_bawah, y1_bawah,(x1-x1_bawah));
        }
    }
}

Berikut link file download dari project di atas. file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s