• Tidak ada hasil yang ditemukan

Buku dan Ebook

Dalam dokumen PENGOLAHAN CITRA DENGAN OPENCV (Halaman 30-39)

Fatta, Hanif Al. 2007. Konversi Format Citra RGB Ke Format Grayscale Menggunakan Visual Basic. Nursyamsu, Ardi. 2011. Masuki Dunia Hacker Dengan C++. Jakarta: Jasakom.

Wibowo, Esther dan Erick Kurniawan. 2008. Histogram.

Hidayatno, Achmad dkk. 2010. Analisis Deteksi Tepi Pada Citra Berdasarkan Perbaikan Kualitas Citra

Website

http://opencv.willowgarage.com/documentation/cpp/ http://docs.opencv.org/ http://www.codeblues.in/programming.php https://sammypatikawa.wordpress.com/2012/05/11/greyscale/ http://www.mathworks.com/support/solutions/en/data/1-ECUGQX/ http://blog.aguskurniawan.net/post/opencv-210-with-visual-studio-2010.aspx http://myopencv.wordpress.com/2009/06/14/image-enhancement-using-hsv-color-space/

http://mahisaajy.blogspot.com/ 30

LAMPIRAN

imgproc.cpp

// imgproc.cpp : Defines the entry point for the console application. //

#include "stdafx.h" /*

This code calculates the image histogram and displays it as a black and white image with the bars in white.

The base of the image holds the intensity values with the 256 values distributed evenly across the width of the image.

*/ #include <stdio.h> #include <cv.h> #include <iostream> #include <conio.h> #include <cxcore.h> #include <highgui.h> #include <stdlib.h> #include "cv.h" using namespace std; using namespace cv;

//Returns the index of the maximum element in the array int find_max(int * hist,int n){

int i_max=0;

for( int i=0;i<n;i++) if(hist[i]>hist[i_max])

i_max=i;

printf("Nilai matriks yang paling sering muncul = %d\n",i_max); return (i_max);

}

//Returns the index of the maximum element in the array int find_max2(int * hist2,int n)

{ int i_max2=0;

for(int i=0;i<n;i++)

if(hist2[i]>hist2[i_max2]) i_max2=i;

printf("Nilai matriks yang paling sering muncul = %d\n",i_max2); return (i_max2); } void histogram(){ IplImage * img; IplImage * gray; IplImage * histogram;

int height, width, step, channels,k,i_max; int * hist;

uchar * data; uchar * * gray_arr; uchar * * histo_arr;

img = cvLoadImage("me.jpg");

http://mahisaajy.blogspot.com/ 31

width = img->width; // width of image

step = img->widthStep; // no. of array element for one row of image data channels = img->nChannels; // no. of channels.... R,G,B, alpga , gama... data = (uchar *)img->imageData;

hist = (int *)calloc(256,sizeof(int));

histo_arr =(uchar * * )malloc(sizeof(uchar * )*(height + 1)); gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1)); // Convert the RGB image to a grayscale image

for(int i=0; i<height; i++) {

histo_arr[i]=(uchar *)malloc(sizeof(uchar)*(width + 1)); gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1)); for(int j=0; j<width; j++) {

// we know that ISO grayscale image is 11%BLUE + 56% GREEN + 33% RED... so converitng 1d array into 2d array

gray_arr[i][j] = (0.11*data[i*step + j*channels] + 0.56*data[i*step + j*channels + 1] + 0.33*data[i*step + j*channels + 2]);

histo_arr[i][j] = 0; }

}

printf("\nUntuk dapat mengetahui intensitas matriks,\n\ Anda dapat melihat program notepad yang terbuka atau \n\ bisa juga dengan melihat file matriks-histogram.txt");

printf("\n\nUntuk melihat bentuk histogramnya,\n\ Anda harus menutup (close) program notepad yang terbuka,\n\ Baru setelah itu window histogram akan muncul");

FILE * pFile;

pFile = fopen("matriks-histogram.txt","w"); //Construct the histogram array

for(int i=0;i<height;i++) {

fprintf(pFile,"baris %d --> ",(i+1)); for (int j=0;j<width;j++) {

//gray_arr[i][j]+=80; k=gray_arr[i][j];

fprintf (pFile, "| %d | ", gray_arr[i][j]); //printf("| %d | ", gray_arr[i][j]); // tambahan hist[k]++; } fprintf(pFile,"\n"); //printf("\n"); // tambahan } fclose (pFile); printf("\n"); // tambahan system("notepad.exe matriks-histogram.txt" ); printf("\n+---Info seputar gambar---+\n");

//Find the maximum height of a bar to scale it according to the height of the image.

i_max=find_max(hist,256);

printf("MAX=%d\ni=%d",hist[i_max],i_max);

http://mahisaajy.blogspot.com/ 32

for(int i=(height-1);i>0;i--) { for (int j=0;j<256;j++) { if(i<=((hist[j]*height)/hist[i_max])) { histo_arr[height-i][j*(width/256)]=255; } } }

printf("\nUkuran gambar = %d x %d",width,height); printf("\nPanjang Gambar (height) = %d pixel ",height); printf("\nLebar Gambar (width) = %d pixel \n",width);

gray = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); histogram = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); for(int i=0; i<height; i++) {

for(int j=0; j<width; j++) {

gray->imageData[i*gray->widthStep + j*gray->nChannels] = gray_arr[i][j];

} }

for(int i=0; i<height; i++) { for(int j=0; j<width; j++) {

histogram->imageData[i*histogram->widthStep + j*histogram->nChannels] = histo_arr[i][j];

} }

cvNamedWindow("Original Image", CV_WINDOW_NORMAL); cvMoveWindow("Original Image", 100, 100);

cvShowImage("Original Image", img);

cvNamedWindow("GrayScaled Image", CV_WINDOW_NORMAL); cvMoveWindow("GrayScaled Image", 500, 100);

cvShowImage("GrayScaled Image", gray);

cvNamedWindow("Histogram Image",CV_WINDOW_NORMAL); cvMoveWindow("Histogram Image",100,100); cvShowImage("Histogram Image",histogram); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&gray); cvReleaseImage(&histogram); free(gray_arr); free(histo_arr); } void histbefore() { IplImage * img; IplImage * gray; IplImage * histogram;

int height, width, step, channels,k,i_max; int * hist;

uchar * data; uchar * * gray_arr; uchar * * histo_arr;

img = cvLoadImage("me.jpg");

http://mahisaajy.blogspot.com/ 33

width = img->width; // width of image

step = img->widthStep; // no. of array element for one row of image data channels = img->nChannels; // no. of channels.... R,G,B, alpga , gama... data = (uchar *)img->imageData;

hist = (int *)calloc(256,sizeof(int));

histo_arr =(uchar * * )malloc(sizeof(uchar * )*(height + 1)); gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1)); // Convert the RGB image to a grayscale image

for(int i=0; i<height; i++) {

histo_arr[i]=(uchar *)malloc(sizeof(uchar)*(width + 1)); gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1)); for(int j=0; j<width; j++) {

// we know that ISO grayscale image is 11%BLUE + 56% GREEN + 33% RED... so converitng 1d array into 2d array

gray_arr[i][j] = (0.11*data[i*step + j*channels] + 0.56*data[i*step + j*channels + 1] + 0.33*data[i*step + j*channels + 2]);

histo_arr[i][j] = 0; }

}

printf("\nUntuk dapat mengetahui intensitas matriks,\n\ Anda dapat membuka file matriks-histogram-before.txt\n");

FILE * pFile;

pFile = fopen("matriks-histogram-before.txt","w"); //Construct the histogram array

for(int i=0;i<height;i++) {

fprintf(pFile,"baris %d --> ",(i+1)); for (int j=0;j<width;j++) {

k=gray_arr[i][j];

fprintf (pFile, "| %d | ", gray_arr[i][j]); hist[k]++;

}

fprintf(pFile,"\n"); }

fclose (pFile);

printf("\n+---Info seputar gambar (sebelum perbaikan)---+\n");

//Find the maximum height of a bar to scale it according to the height of the image.

i_max=find_max(hist,256);

printf("MAX=%d\ni=%d",hist[i_max],i_max);

// Construct the image displaying the histogram. for(int i=(height-1);i>0;i--) { for (int j=0;j<256;j++) { if(i<=((hist[j]*height)/hist[i_max])) { histo_arr[height-i][j*(width/256)]=255; } } }

http://mahisaajy.blogspot.com/ 34

printf("\nPanjang Gambar (height) = %d pixel ",height);

printf("\nLebar Gambar (width) = %d pixel \n",width);

gray = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); histogram = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1); for(int i=0; i<height; i++) {

for(int j=0; j<width; j++) {

gray->imageData[i*gray->widthStep + j*gray->nChannels] = gray_arr[i][j];

} }

for(int i=0; i<height; i++) { for(int j=0; j<width; j++) {

histogram->imageData[i*histogram->widthStep + j*histogram->nChannels] = histo_arr[i][j];

} }

cvNamedWindow("Histogram Image Before",CV_WINDOW_NORMAL); cvMoveWindow("Histogram Image Before",100,100);

cvShowImage("Histogram Image Before",histogram); free(gray_arr); free(histo_arr); /////////////////////////////////////////////////////////////////////////////// } void perbaikancitra() { int i,j,k; int heightc,widthc,stepc,channelsc,height,width; int temp=0; int units=0; uchar *data,*datac; i=j=k=0; IplImage *img=cvLoadImage("me.jpg",1);

IplImage *convert=cvCreateImage( cvGetSize(img), 8, 3 ); IplImage *result=cvCreateImage( cvGetSize(img), 8, 3 ); printf("Masukkan value brightness yang diinginkan = "); scanf("%d", &units); height = img->height; width = img->width; if (units != 0){ printf("\nTinggi gambar=%d\n",height); printf("\nLebar gambar=%d\n",width); } if(img==NULL ) {

puts("unable to load the frame");exit(0); }

http://mahisaajy.blogspot.com/ 35

cvNamedWindow("Result",CV_WINDOW_AUTOSIZE); heightc = convert->height; widthc = convert->width; stepc=convert->widthStep; channelsc=convert->nChannels;

datac = (uchar *)convert->imageData; cvCvtColor(img,convert,CV_BGR2HSV);

for(i=0;i< (heightc);i++) for(j=0;j<(widthc);j++) {/*Here datac means data of the HSV Image*/

/*Here i want to Increase the saturation or the strength of the Colors in the Image and

then I would be able to perform a good color detection*/

temp=datac[i*stepc+j*channelsc+1]+units;/*increas the saturaion component is the second arrray.*/

/*Here there is a small problem...when you add a value to the data and if it exceeds 255

it starts all over again from zero and hence some of the pixels might go to zero. So to stop this we need to include this loop i would not try to explain the loop but

please try and follow it is easy to do so..*/ if(temp>255) datac[i*stepc+j*channelsc+1]=255; else datac[i*stepc+j*channelsc+1]=temp;/*you may

please remove and see what is happening if the if else loop is not there*/} cvCvtColor(convert, result, CV_HSV2BGR);

cvShowImage("Result", result); cvShowImage("original", img); cvSaveImage("meresult.jpg",result);

histbefore(); // panggil fungsi histbefore ////////////////////////////////////////// // ini program untuk memunculkan histogram after //IplImage * result;

IplImage * gray2; IplImage * histogram2;

int height2, width2, step2, channels2; int l,i_max2; int * hist2; uchar * data2; uchar * * gray_arr2; uchar * * histo_arr2; result = cvLoadImage("meresult.jpg"); height2 = result->height; width2 = result->width; step2 = result->widthStep; channels2 = result->nChannels; data2 = (uchar *)result->imageData;

http://mahisaajy.blogspot.com/ 36

hist2 = (int *)calloc(256,sizeof(int));

histo_arr2 =(uchar * * )malloc(sizeof(uchar * )*(height2 + 1)); gray_arr2 = (uchar * * )malloc(sizeof(uchar * )*(height2 + 1)); // Convert the RGB image to a grayscale image

for(int i=0; i<height2; i++) {

histo_arr2[i]=(uchar *)malloc(sizeof(uchar)*(width2 + 1)); gray_arr2[i] = (uchar * )malloc(sizeof(uchar)*(width2 + 1)); for(int j=0; j<width2; j++) {

// we know that ISO grayscale image is 11%BLUE + 56% GREEN + 33% RED... so converitng 1d array into 2d array

gray_arr2[i][j] = (0.11*data2[i*step2 + j*channels2] + 0.56*data2[i*step2 + j*channels2 + 1] + 0.33*data2[i*step2 + j*channels2 + 2]);

histo_arr2[i][j] = 0; }

}

FILE * pFile;

pFile = fopen("matriks-histogram-after.txt","w"); //Construct the histogram array

for(int i=0;i<height2;i++) {

fprintf(pFile,"baris %d --> ",(i+1)); for (int j=0;j<width2;j++) {

gray_arr2[i][j]+=units; l=gray_arr2[i][j];

fprintf (pFile, "| %d | ", gray_arr2[i][j]); hist2[l]++;

}

fprintf(pFile,"\n"); }

fclose (pFile);

printf("\n+---Info seputar gambar (setelah perbaikan)---+\n"); i_max2=find_max2(hist2,256);

printf("MAX=%d\ni=%d",hist2[i_max2],i_max2); // Construct the image displaying the histogram. for(int i=(height2-1);i>0;i--) { for (int j=0;j<256;j++) { if(i<=((hist2[j]*height2)/hist2[i_max2])) { histo_arr2[height2-i][j*(width2/256)]=255; } } }

printf("\nUkuran gambar = %d x %d",width,height); printf("\nPanjang Gambar (height) = %d pixel ",height); printf("\nLebar Gambar (width) = %d pixel \n",width);

gray2 = cvCreateImage(cvSize(width2, height2), IPL_DEPTH_8U, 1); histogram2 = cvCreateImage(cvSize(width2,height2),IPL_DEPTH_8U,1); for(int i=0; i<height2; i++) {

http://mahisaajy.blogspot.com/ 37

for(int j=0; j<width2; j++) { gray2->imageData[i*gray2->widthStep + j*gray2->nChannels] = gray_arr2[i][j]; } }

for(int i=0; i<height2; i++) { for(int j=0; j<width2; j++) {

histogram2->imageData[i*histogram2->widthStep + j*histogram2->nChannels] = histo_arr2[i][j]; // *histogram2

} }

cvNamedWindow("Histogram Image After",CV_WINDOW_NORMAL); cvMoveWindow("Histogram Image After",100,100);

cvShowImage("Histogram Image After",histogram2); free(gray_arr2); free(histo_arr2); //////////////////////////////////////////////////////// cvWaitKey(0); cvDestroyWindow("original"); cvDestroyWindow("Result"); } void deteksitepi() { Mat src, src_gray; Mat grad;

char* window_name = "Sobel Demo - Simple Edge Detector"; int scale = 1; int delta = 0; int ddepth = CV_16S; int c; /// Load an image src = imread("me.jpg"); if( !src.data ) {

cout << "Gak ada source gan!"; getch();

system("exit"); //return -1; }

GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT ); /// Convert it to gray

cvtColor( src, src_gray, CV_RGB2GRAY ); /// Create window

http://mahisaajy.blogspot.com/ 38

/// Generate grad_x and grad_y

Mat grad_x, grad_y;

Mat abs_grad_x, abs_grad_y; /// Gradient X

//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT ); Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT ); convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y

//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT ); Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT ); convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)

addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad ); imshow( window_name, grad );

waitKey(0); }

void about() {

printf("Selamat menikmati program ini hehe\nSpecial Thanks for our team : \nAjy, Alicia, Endru, Sita, Windy\n");

}

void main() { int pil;

printf("Aplikasi Pengolahan Citra v1.0\n"); printf("1. Konversi image ke histogram \n"); printf("2. Perbaikan Citra \n");

printf("3. Deteksi Tepi \n"); printf("4. Tentang program \n"); printf("5. Keluar \n");

printf("Masukkan pilihan [1-5] ? "); scanf_s("%d",&pil); switch (pil) {

case 1: histogram(); break; case 2: perbaikancitra(); break; case 3: deteksitepi(); break; case 4: about(); break; case 5: exit(0);

default: "Pilihan tidak ada"; }

system("PAUSE"); }

Dalam dokumen PENGOLAHAN CITRA DENGAN OPENCV (Halaman 30-39)

Dokumen terkait