loading...

Selasa, 28 Agustus 2018

BAB 4 - Operasi Dasar Olah Citra Digital

Banyak teknik dasar olah citra yang telah disediakan oleh AForge diantaranya adalah operasi konvolusi (filtering), thresholding, morphologi, dan blob, corner detection, histogram equalization, line - circle - shape detection yang dibungkus secara rapi dalam bentuk class pada package AForge.Imaging dan AForge.Imaging.Filters.

Konvolusi

Konvolusi merupakan salah satu operasi dasar untuk analisis citra yang memberikan efek tertentu pada citra. Beberapa kegunaan dari konvolusi seperti smoothing, sharpening, blur, dan deteksi tepi. Operasi Konvolusi menggunakan array 2D sebagai masukan dan array 2D square sebagai filter/kernel. Hasil dari konvolusi akan berbeda tergantung dari jenis kernelnya. Berikut contoh perhitungan dari konvolusi


Jika
x array 2D sebagai matrix input
h array 2D sebagai matrix (kernel / filter)
m, i adalah kolom dari input (x) dan kernel (h)
n, j adalah baris  dari input (x) dan kernel (h)
y sebagai ouput

Misalkan kita ingin menghitung  y[1,1] maka, ingat bahwa y[kolom,baris] bukan y[baris,kolom]

= x[0,0]*h[1,1]+x[1,0]*h[0,1]+x[2,0]*h[-1,1]
+x[0,1]*h[1,0]+x[1,1]*h[0,0]+x[2,1]*h[-1,0]
+x[0,2]*h[1,-1]+x[1,2]*h[0,-1]+x[2,2]*h[-1,-1]
Maka akan terlihat seperti berikut

Berikut untuk perhitungan yang lainnya

y[0,0]    = x[-1,-1] * h[1,1] + x[0,-1] * h[0,1] + x[1,-1] * h[-1,1]
    + x[-1,0] * h[1,0] + x[0,0] * h[0,0] + x[1,0] * h[-1,0]
    + x[-1,1] * h[1,-1] + x[0,1] * h[0,-1] + x[1,1] * h[-1,-1]
    = 0 * 1 + 0 * 2 + 0 * 1 + 0 * 0 + 1 * 0 + 2 * 0 + 0 *-1 + 4 * -2 + 5 * -1 = -13
y[1,0]    = x[0,-1] * h[1,1] + x[1,-1] * h[0,1] + x[2,-1] * h[-1,1]
    + x[0,0] * h[1,0] + x[1,0] * h[0,0] + x[2,0] * h[-1,0]
    + x[0,1] * h[1,-1] + x[1,1] * h[0,-1] + x[2,1] * h[-1,-1]
    = 0 * 1 + 0 * 2 + 0 * 1 + 1 * 0 + 2 * 0 + 3 * 0 + 4 * -1 + 5 * -2 + 6 * -1 = -20
y[2,0]    = x[1,-1] * h[1,1] + x[2,-1] * h[0,1] + x[3,-1] * h[-1,1]
    + x[1,0] * h[1,0] + x[2,0] * h[0,0] + x[3,0] * h[-1,0]
    + x[1,1] * h[1,-1] + x[2,1] * h[0,-1] + x[3,1] * h[-1,-1]
    = 0 * 1 + 0 * 2 + 0 * 1 + 2 * 0 + 3 * 0 + 0 * 0 + 5 * -1 + 6 * -2 + 0 * -1 = -17
y[0,1]    = x[-1,0] * h[1,1] + x[0,0] * h[0,1] + x[1,0] * h[-1,1]
    + x[-1,1] * h[1,0] + x[0,1] * h[0,0] + x[1,1] * h[-1,0]
    + x[-1,2] * h[1,-1] + x[0,2] * h[0,-1] + x[1,2] * h[-1,-1]
    = 0 * 1 + 1 * 2 + 2 * 1 + 0 * 0 + 4 * 0 + 5 * 0 + 0 * -1 + 7 * -2 + 8 * -1 = -18
y[1,1]    = x[0,0] * h[1,1] + x[1,0] * h[0,1] + x[2,0] * h[-1,1]
    + x[0,1] * h[1,0] + x[1,1] * h[0,0] + x[2,1] * h[-1,0]
    + x[0,2] * h[1,-1] + x[1,2] * h[0,-1] + x[2,2] * h[-1,-1]
    = 1 * 1 + 2 * 2 + 3 * 1 + 4 * 0 + 5 * 0 + 6 * 0 + 7 * -1 + 8 * -2 + 9 * -1 = -24
y[2,1]    = x[1,0] * h[1,1] + x[2,0] * h[0,1] + x[3,0] * h[-1,1]
    + x[1,1] * h[1,0] + x[2,1] * h[0,0] + x[3,1] * h[-1,0]
    + x[1,2] * h[1,-1] + x[2,2] * h[0,-1] + x[3,2] * h[-1,-1]
    = 2 * 1 + 3 * 2 + 0 * 1 + 5 * 0 + 6 * 0 + 0 * 0 + 8 * -1 + 9 * -2 + 0 * -1=-18
y[0,2]    = x[-1,1] * h[1,1] + x[0,1] * h[0,1] + x[1,1] * h[-1,1]
    + x[-1,2] * h[1,0] + x[0,2] * h[0,0] + x[1,2] * h[-1,0]
    + x[-1,3] * h[1,-1] + x[0,3] * h[0,-1] + x[1,3] * h[-1,-1]
    = 0 * 1 + 4 * 2 + 5 * 1 + 0 * 0 + 7 * 0 + 8 * 0 + 0 * -1 + 0 * -2 + 0 * -1 = 13
y[1,2]    = x[0,1] * h[1,1] + x[1,1] * h[0,1] + x[2,1] * h[-1,1]
    + x[0,2] * h[1,0] + x[1,2] * h[0,0] + x[2,2] * h[-1,0]
    + x[0,3] * h[1,-1] + x[1,3] * h[0,-1] + x[2,3] * h[-1,-1]
    = 4 * 1 + 5 * 2 + 6 * 1 + 7 * 0 + 8 * 0 + 9 * 0 +0 * -1 + 0 * -2 + 0 * -1 = 20
y[2,2]    = x[1,1] * h[1,1] + x[2,1] * h[0,1] + x[3,1] * h[-1,1]
    + x[1,2] * h[1,0] + x[2,2] * h[0,0] + x[3,2] * h[-1,0]
    + x[1,3] * h[1,-1] + x[2,3] * h[0,-1] + x[3,3] * h[-1,-1]
    = 5* 1 + 6 * 2 + 0 * 1 + 8 * 0 + 9 * 0 + 0 * 0 + 0 * -1 + 0 * -2 + 0 * -1 = 17

Jenis-Jenis Kernel

Berikut jenis kernel yang sering digunakan untuk menampilkan efek tertentu pada citra digital.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;
using AForge;
using AForge.Imaging.Filters;
using Accord.Controls;
using Accord.Imaging.Converters;
namespace Image_Viewer
{
    class Program
    {
        static void Main(string[] args)
        {


            Bitmap image = (Bitmap)Bitmap.FromFile("D:/image.jpg");
            Accord.Controls.ImageBox.Show(new Bitmap(image)).SetTitle("Original");
            int[,] sharpen = {
                            {  0, -1,  0 },
                            { -1,  5, -1 },
                            {  0, -1,  0 }
                            };
            int[,] blur = { { 1, 2, 3, 2, 1 },
                            { 2, 4, 5, 4, 2 },
                            { 3, 5, 6, 5, 3 },
                            { 2, 4, 5, 4, 2 },
                            { 1, 2, 3, 2, 1 }
                            };
            int[,] emboss = { { -2, -1,  0 },
                            { -1,  1,  1 },
                            {  0,  1,  2 }
                            };
            Program.konvolution(sharpen, "sharpen",image);
            Program.konvolution(blur, "blur", image);
            Program.konvolution(emboss, "emboss", image);
            Console.ReadLine();
        }
        public static void demo()
        {
            Bitmap image = (Bitmap)Bitmap.FromFile("D:/image.jpg");
            Accord.Controls.ImageBox.Show(new Bitmap(image)).SetTitle("Original");
            Blur filter = new Blur();
            // apply the filter
            filter.ApplyInPlace(image);
            Accord.Controls.ImageBox.Show(new Bitmap(image), PictureBoxSizeMode.Zoom).SetTitle("Blur");
        }
        public static void konvolution(int [,] kernel, string title, Bitmap image)
        {
            
            Bitmap image_result = new Bitmap(image);
            Convolution filter = new Convolution(kernel);            
            filter.ApplyInPlace(image_result);
            Accord.Controls.ImageBox.Show(new Bitmap(image_result)).SetTitle(title);


        }
    }
}

Baca Juga : http://www.softscients.web.id/2018/08/bab-3-framework-aforge-dan-accord-net.html

Menghasilkan


Cukup mudah bukan? 

Deteksi Tepi/Edge Detection

Selain berfungsi untuk melakukan filtering atau pemberian efek terhadap citra, konvolusi juga bisa digunakan untuk melakukan edge detection (deteksi tepi) dengan menggunakan class yang telah disediakan oleh AForge yaitu
Oiya, jangan lupa bahwa untuk deteksi tepi hanya berlaku untuk format
PixelFormat.Format8bppIndexed

baca juga: https://stackoverflow.com/questions/34483369/source-pixel-format-is-not-supported-by-the-filter-in-aforge-net

Homogenity Edge Detector

The filter finds objects' edges by calculating maximum difference of processing pixel with neighboring pixels in 8 direction.

Difference Edge Detector

The filter finds objects' edges by calculating maximum difference between pixels in 4 directions around the processing pixel.

Sobel Edge Detector

The filter searches for objects' edges by applying Sobel operator.

Canny Edge Detector

The filter searches for objects' edges by applying Canny edge detector.
lihat pada function DemoPenerapanDeteksiTepi()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;
using AForge;
using AForge.Imaging.Filters;
using Accord.Controls;
using Accord.Imaging.Converters;
namespace Image_Viewer
{
    class Program
    {
        static void Main(string[] args)
        {


            DemoPenerapanDeteksiTepi();
            Console.ReadLine();
        }
        public static void DemoPenerapanDeteksiTepi()
        {
            //https://stackoverflow.com/questions/34483369/source-pixel-format-is-not-supported-by-the-filter-in-aforge-net


            Bitmap image = (Bitmap)Bitmap.FromFile("D:/cat.jpg");

            if (image.PixelFormat != PixelFormat.Format8bppIndexed) //jika truecolor, maka convert dulu
            {
                Grayscale rgb2grayscale = new Grayscale(0.2125, 0.7154, 0.0721);
                image = rgb2grayscale.Apply(image);

            }

            Accord.Controls.ImageBox.Show(new Bitmap(image)).SetTitle("Original");
            // create filter
            HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
            // apply the filter
            Bitmap HO = filter.Apply(image);
            Accord.Controls.ImageBox.Show(new Bitmap(HO)).SetTitle("HomogenityEdgeDetector");

            // create filter
            DifferenceEdgeDetector filter2 = new DifferenceEdgeDetector();
            // apply the filter
            Bitmap DI = filter2.Apply(image);
            Accord.Controls.ImageBox.Show(new Bitmap(DI)).SetTitle("DifferenceEdgeDetector");

        }
        public static void DemoTampilGambar()
        {
            Bitmap image = (Bitmap)Bitmap.FromFile("D:/image.jpg");
            Accord.Controls.ImageBox.Show(new Bitmap(image)).SetTitle("Original");
            Blur filter = new Blur();
            // apply the filter
            filter.ApplyInPlace(image);
            Accord.Controls.ImageBox.Show(new Bitmap(image), PictureBoxSizeMode.Zoom).SetTitle("Blur");
        }
        
        public static void DemoPenerapanKonvolution()
        {
            Bitmap image = (Bitmap)Bitmap.FromFile("D:/image.jpg");
            Accord.Controls.ImageBox.Show(new Bitmap(image)).SetTitle("Original");
            int[,] sharpen = {
                            {  0, -1,  0 },
                            { -1,  5, -1 },
                            {  0, -1,  0 }
                            };
            int[,] blur = { { 1, 2, 3, 2, 1 },
                            { 2, 4, 5, 4, 2 },
                            { 3, 5, 6, 5, 3 },
                            { 2, 4, 5, 4, 2 },
                            { 1, 2, 3, 2, 1 }
                            };
            int[,] emboss = { { -2, -1,  0 },
                            { -1,  1,  1 },
                            {  0,  1,  2 }
                            };
            Program.konvolution(sharpen, "sharpen", image);
            Program.konvolution(blur, "blur", image);
            Program.konvolution(emboss, "emboss", image);
        }
        public static void konvolution(int [,] kernel, string title, Bitmap image)
        {
            
            Bitmap image_result = new Bitmap(image);
             Convolution filter = new Convolution(kernel);            
            filter.ApplyInPlace(image_result);
            Accord.Controls.ImageBox.Show(new Bitmap(image_result)).SetTitle(title);


        }
    }
}


Pada sesi ini, diharapkan sudah paham sedikit-demi sedikit cara kerja library yang kita gunakan.

Thresholding

Thresholding (pengambangan) mencari nilai T (ambang batas) yang digunakan untuk melakukan proses konversi citra true color dalam format black white. Ada banyak metode untuk mendapatkan nilai ambang batas sebagai acuan untuk mengkategorikan nilai suatu pixel termasuk warna hitam atau putih jika dalam format biner maka (1 dan 0) dan format grayscale (0 dan 255) salah satu yang paling banyak dikenal luas dan digunakan sebagai standar yaitu metode Otsu.

<akan di update lagi >

Tidak ada komentar: