Wednesday, 4 December 2013

OpenCV Tutorial - Sobel Edge-Detection

Advertisement

Halo semuanya, kali ini saya ingin bagi-bagi ilmu mengenai pemrosesan citra digital. Ini sih sebenarnya tuntutan kuliah juga, tapi apa salahnya saya taro di blog saya.

Kali ini saya ingin share tentang Sobel Edge-Detection SECARA MANUAL (tidak menggunakan fungsi yang sekali panggil langsung dapat). Hasilnya ntar akan seperti ini gan

Hasil Sobel edge-detection
Hasil Sobel edge-detection

Yak, langsung saja, ini dia kodingannya.

#include 
#include
#include
#include

using namespace std;

//Fungsi untuk menghitung gradien terhadap suatu sumbu
void hitungSobelAxis(CvMat *matInput, CvMat *output, int x[3][3] ) {

int temp;

for (int i = 1; i < matInput->rows - 1; i++) {
for (int j = 1; j < matInput -> cols - 1; j++) {
//Hitung hasil masking
temp = (cvGetReal2D(matInput, i-1, j-1) * x[0][0]) + (cvGetReal2D(matInput, i-1, j) * x[0][1]) + (cvGetReal2D(matInput, i-1, j+1) * x[0][2])
+ (cvGetReal2D(matInput, i, j-1) * x[1][0]) + (cvGetReal2D(matInput, i, j) * x[1][1]) + (cvGetReal2D(matInput, i, j+1) * x[1][2])
+ (cvGetReal2D(matInput, i+1, j-1) * x[2][0]) + (cvGetReal2D(matInput, i+1, j) * x[2][1]) + (cvGetReal2D(matInput, i+1, j+1) * x[2][2]);
//Masukkan hasil masking pada output citra
cvSetReal2D(output, i-1, j-1, temp);
}
}
}

//Setelah menghitung gradien x dan y, hitung Magnitude
void hitungSobel(CvMat *x, CvMat *y, CvMat *hasil) {
for (int i = 0; i < hasil-> rows; i++) {
for (int j = 0; j < hasil->cols; j++) {
//hitung magnitude
cvSetReal2D(hasil, i, j,
//Magnitude = |Gx| = sqrt(Gx^2 + Gy^2)
sqrt(cvGetReal2D(x, i, j) * cvGetReal2D(x, i, j) +
cvGetReal2D(y, i, j) * cvGetReal2D(y, i, j))
);
}
}
}

void threshold(CvMat *in) {
for (int i=0; i rows; i++)
{
for (int j=0; jcols; j++)
{
//Jika nilai pixel citra asli < 128
//Ubah menjadi 0 (hitam)
if (cvGetReal2D(in, i, j) < 128)
{
cvSetReal2D(in, i, j, 0);
}

//Jika nilai pixel citra asli >= 128
//Ubah menjadi 255 (putih)
else
{
cvSetReal2D(in, i, j, 255);
}
}
}
}

//Fungsi menampilkan citra
void tampilkan(CvMat *out, char* stringTemp) {
cvNamedWindow(stringTemp, CV_WINDOW_AUTOSIZE);
cvShowImage(stringTemp, out);
}


int main()
{
CvMat *matInput = cvLoadImageM("cat.jpg", CV_LOAD_IMAGE_GRAYSCALE);
CvMat *outX, *outY, *out;

char *original = "Original";
char *stringX = "Hasil X";
char *stringY = "Hasil Y";
char *stringHasil = "Hasil";

//untuk citra gradien
outX = cvCreateMat(matInput->rows - 1, matInput->cols - 1, CV_8U);
outY = cvCreateMat(matInput->rows - 1, matInput->cols - 1, CV_8U);

//untuk citra hasil edge-detection
out = cvCreateMat(matInput->rows - 1, matInput->cols - 1, CV_8U);

//Kernel sumbu X dan sumbu Y
int kernelX[3][3] = {{-1,0,1}, {-2,0,2}, {-1,0,1}};
int kernelY[3][3] = {{-1,-2,-1}, {0,0,0}, {1,2,1}};

hitungSobelAxis(matInput, outX, kernelX);
hitungSobelAxis(matInput, outY, kernelY);

hitungSobel(outX, outY, out);

tampilkan(matInput, original);
threshold(out);
tampilkan(out, stringHasil);

cvWaitKey(0);
}


Kalau ada yang ingin ditanyakan, komen saja ya gan (bisa, tidak bisa jawab tidak dijamin :P)


EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)