LAMPIRAN A
DATA SAMPEL
1.
Data latihan
Pola
Citra
Pola
Citra
Pola
Citra
Pola
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Citra
Universitas Sumatera Utara
57
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Universitas Sumatera Utara
58
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Universitas Sumatera Utara
59
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Universitas Sumatera Utara
60
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Universitas Sumatera Utara
61
224 2. Data uji Pola
Citra
Pola
Citra
Pola
Citra
Pola
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Citra
Universitas Sumatera Utara
62
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60-
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Universitas Sumatera Utara
LAMPIRAN B CODING PROGRAM
1. Modul Contoh Gambar Citra /***** Display Image satu per satu ***** Grayscale, Thresholding, Resize, Normalisasi, Thinning, DBFE & Input JST *****/ #include
#include #include <string> #include #define LEBAR 60 #define TINGGI 90 #define ZONA_L 6 #define ZONA_T 9 #define NIN 69 #include "CImg.h" using namespace cimg_library; int main(){ int a, b , i, j, k, l; float NilR, NilG, NilB, lum; double input_jst[1][NIN]; int indeks=0; int threshold = 190; // Inisialisasi array input jst //Inisialisasi array input_jst for(i=0; i<1; i++) { for(j=0; j img("hangul.bmp");
//---------- Start------------------//Grayscale img.display("Gambar Awal"); for(int i=0; i
Universitas Sumatera Utara
64
lum = (NilR*0.299 + NilG*0.587 nilai grayscale img(j, i, 0) = lum; //Set img(j, i, 1) = lum; //Set img(j, i, 2) = lum; //Set
+ NilB*0.114);
//Hitung
nilai avg ke pixel (j,i) nilai avg ke pixel (j,i) nilai avg ke pixel (j,i)
} } img.display("Image Grayscale"); //Threshold for(i=0; ithreshold) //Cek nilai piksel { img(j, i, 0) = 255; //Set nilai 254 (putih/background) ke pixel (j,i), jika nilai warna > threshold img(j, i, 1) = 255; img(j, i, 2) = 255; } else { img(j, i, 0) = 0; //Set nilai 0 (hitam) ke pixel (j,i), jika nilai warna < threshold img(j, i, 1) = 0; img(j, i, 2) = 0; } } } img.display("Image Threshold"); // Resize img.resize(60,90); img.display("Image Resize"); //Normalisasi img.normalize(0,255); img.display("Image Normalisasi"); // DBFE int jml_piksel_zona=0; //variabel untuk setiap zona double jml_hor=0.0, jml_ver=0.0; //variabel untuk menyimpan jumlah nilai dari setiap zona secara horizontal dan vertikal double rata_rata_zona[ZONA_T][ZONA_L], rata_rata_hor[ZONA_T], rata_rata_ver[ZONA_L]; //array untuk menyimpan nilai semua rata-rata (zona, horizontal dan vertikal) //Inisialisasi array for(i=0; i
Universitas Sumatera Utara
65
{ for(j=0; j rata_zona = jml_piksel_zona/(panjang + tinggi - 1) rata_rata_zona[i/10][j/10] = (double)jml_piksel_zona/19; jml_hor = 0.0; //Masukkan rata_rata_zona ke array input_jst input_jst[0][indeks++] = rata_rata_hor[i]; } //cout << endl; //Hitung rata-rata zona vertikal for(i=0; i
2. Modul Pemrosesan Gambar Citra /***** Modul Image Proses ***** Grayscale, Thresholding, Resize, Normalisasi, Thinning, DBFE ***** Credit : Rian Harris (TI 2009) & Nanda Putra (TI 2008) *****/ #include #include #include <stdlib.h> #include <string.h> #define LEBAR 30 #define TINGGI 40 #define ZONA_L 6 #define ZONA_T 9 #define ZONA_U 5 #define NIN 69 #include "CImg.h" using namespace cimg_library; using namespace std; int main()
Universitas Sumatera Utara
66
{ int n, m, i, j, k, l; //n dan m untuk pengulangan images | i, j, k, l untuk pengulangan biasa char namafile[30]; //string untuk menyimpan nama file image int jumlah_image; //variabel jumlah image int pil_menu; //variabel untuk menu pelatihan atau pengujian //Variabel untuk fungsi Grayscale float NilR, NilG, NilB, lum ; //Deklarasi variable untuk RGB dan avg(nilai grayscale) //Variabel untuk fungsi Threshold int threshold; //variabel threshold cout cout cout cout cout
<< << << << <<
"Image Proses "" << endl; "1. Training" "2. Testing " "Pilihan : ";
: " << endl; << endl; << endl; cin >> pil_menu;
if(pil_menu == 1) { strcpy(folder_raw,"data_latihan_unpro"); strcpy(folder,"data_latihan"); } else if(pil_menu == 2) { strcpy(folder_raw,"data_uji_unpro"); strcpy(folder,"data_uji"); } cout << "Jumlah pola : "; cin >> jumlah_image; //input jumlah image //cout << "Threshold : "; cin >> threshold; //input nilai threshold untuk binary image //Inisialisasi array input_jst for(i=0; i<15*jumlah_image; i++) { for(j=0; j img(namafile); //Mulai semua fungsi image processing // //Grayscale
Universitas Sumatera Utara
67
for(i=0; ithreshold) //Cek nilai piksel { img(j, i, 0) = 255; //Set nilai 254 (putih/background) ke pixel (j,i), jika nilai warna > threshold img(j, i, 1) = 255; img(j, i, 2) = 255; } else { img(j, i, 0) = 0; //Set nilai 0 (hitam) ke pixel (j,i), jika nilai warna < threshold img(j, i, 1) = 0; img(j, i, 2) = 0; } } } //Resize Image img.resize(LEBAR,TINGGI); //Normalisasi img.normalize(0,255); // Diagonal Based Feature Extraction int jml_piksel_zona=0; //variabel untuk setiap zona double jml_hor=0.0, jml_ver=0.0; //variabel untuk menyimpan jumlah nilai dari setiap zona secara horizontal dan vertikal double rata_rata_zona[ZONA_T][ZONA_L], rata_rata_hor[ZONA_T], rata_rata_ver[ZONA_L]; //array untuk menyimpan nilai semua rata-rata (zona, horizontal dan vertikal) //Inisialisasi array for(i=0; i
Universitas Sumatera Utara
68
for(i=0; i
Universitas Sumatera Utara
69
} //system("pause"); } if(pil_menu == 1) system("pause"); return 0; }
3. Modul Pelatihan Jaringan #include #include #include #include #include #define #define #define #define
<math.h> <stdlib.h> NIN 69 // jumlah neuron input NH 120 // jumlah neuron hidden NOUT 15 // jumlah neuron output PATTERN 225 // jumlah pola
using namespace std; class backpropagation { // Deklarasi array neuron input, hidden, output dan target double X[PATTERN][NIN]; double H[NH]; double Y[PATTERN][NOUT]; double targetY[NOUT][NOUT]; // Deklarasi array bobot input, hidden, output dan bias double weightIH[NIN][NH]; double weightHO[NH][NOUT]; double biasH[NH]; double biasY[NOUT]; // Deklarasi array untuk perubahan bobot dan bias double deltaY[NOUT]; double deltaH[NH]; double deltaweightIH[NIN][NH]; double deltaweightHO[NH][NOUT]; public: backpropagation(); backpropagation(double, int, double, double, int); void init(double _X[PATTERN][NIN], double _targetY[NOUT][NOUT]); bool training(); void calNet(int, int); void deltaChange(int, int); void weightAndbiasChange(int); void calMSE(); void printResult(); double sigmoid(double); void saveWeight(); }; backpropagation::backpropagation() {
Universitas Sumatera Utara
70
learning_rate = 0.5; jum_epoch = 10000; error = 0.01; momentum = 0.5; jumlah_image = 1; } { learning_rate= _learning_rate; jum_epoch = _jum_epoch; error = _error; momentum = _momentum; jumlah_image = _jumlah_image; } { // Input data int a, b, i, h, o, rndnum; double beta, min, max, eucval, pembagi=10000.0; // Inisialisasi X for(a=0; a<jumlah_image*15; a++) { for(b=0; b
Universitas Sumatera Utara
71
void backpropagation::deltaChange(int p, int numberTarget) { int h, o; double sigmadeltaY; // Hitung nilai deltaY for(o=0; o
<< << << <<
"Learning rate : " << "Momentum : " << "MSE : " << "Epoch : " <<
learning_rate << endl; momentum << endl; mse << endl; epoch << endl;
void backpropagation::calMSE() { int a, b; mse = 0.0; for(a=0; a<jumlah_image*15; a++) { for(b=0; b
Universitas Sumatera Utara
72
} mse = ((mse/2))/(jumlah_image*15); cout << "mse " << mse << endl; if(mse>error) SolutionFound = false; else SolutionFound = true; } double backpropagation::sigmoid(double x) { return 1.0/(1.0+exp(-x)); } int main () { // Variabel untuk menghitung lama waktu training clock_t startT, endT; double timeT; int jum_epoch; double error; double learning_rate; double momentum; double targetY[NOUT][NOUT] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 1, {0, 0, 0, 0, 0, 0, 0, 1, 0, {0, 0, 0, 0, 0, 0, 1, 0, 0, {0, 0, 0, 0, 0, 1, 0, 0, 0, {0, 0, 0, 0, 1, 0, 0, 0, 0, {0, 0, 0, 1, 0, 0, 0, 0, 0, {0, 0, 1, 0, 0, 0, 0, 0, 0, {0, 1, 0, 0, 0, 0, 0, 0, 0, {1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}, 0}};
// Input parameter cout << "Training Jaringan Propagasi Balik Arsitektur 69-90-15" << endl; cout << endl; cout << "Jumlah pola = " << jumlah_image << endl; cout << "Jumlah Epoch = "; cin >> jum_epoch; cout << "Error = "; cin >> error; cout << "Learning rate = "; cin >> learning_rate; cout << "Momentum = "; cin >> momentum; backpropagation BP(learning_rate, jum_epoch, error, momentum, jumlah_image); in.close(); system("pause"); return 0; }
4. Modul Pengujian Jaringan
Universitas Sumatera Utara
73
#include #include #include #include #include #include #include
<stdlib.h> <math.h> <string.h>
#define NIN 69 #define NH 120 #define NOUT 15
// jumlah neuron input // jumlah neuron hidden // jumlah neuron output
using namespace std; double sigmoid(double x) { return 1.0/(1.0+exp(-x)); } int main() { int pil_menu, p, i, h, o, a, b, TargetHuruf, generalisasi=0, counter_output; char file_input[20]; // Deklarasi array bobot input, hidden, output dan bias double weightIH[NIN][NH]; double weightHO[NH][NOUT]; double biasH[NH]; double biasY[NOUT]; cout << "Pengujian JST Arsitektur 69-120-15 :" << endl; cout << "" << endl; cout << "0. Info Testing " << endl; cout << "1. Memorisasi " << endl; cout << "2. Generalisasi" << endl; cout << "" << endl; cout << "Pilihan : "; cin >> pil_menu; if(pil_menu == 1) { strcpy(file_input,"data_latihan.dat"); } else if (pil_menu == 2) { strcpy(file_input,"data_uji.dat"); } else if (pil_menu == 0) //////////////////////// // Proses Testing for(p=0; p<jumlah_image*15; p++) { TargetHuruf = p / jumlah_image; // Operasi pada hidden layer for(h=0; h
Universitas Sumatera Utara
74
H[h] += input_jst[p][i] * weightIH[i][h]; } H[h] += biasH[h]; // Fungsi Aktivasi H[h] = sigmoid(H[h]); } // Operasi pada output layer for(o=0; o= 0.5) { Y[p][o] = 1; cout << "1 "; counter_output++; } else if (Y[p][o] < 0.5) { Y[p][o] = 0; cout << "0 "; } } cout << " Target = "; if (TargetHuruf == 0) { cout << "bi"; } else if (TargetHuruf == 1) { cout << "cha"; } else if (TargetHuruf == 2) { cout << "do"; } else if (TargetHuruf == 3) { cout << "ga"; } else if (TargetHuruf == 4) {
Universitas Sumatera Utara
75
cout << "gyu"; } else if (TargetHuruf == 5) { cout << "hu"; } else if (TargetHuruf == 6) { cout << "jyo"; } else if (TargetHuruf == 7) { cout << "ko"; } else if (TargetHuruf == 8) { cout << "seo"; } else if (TargetHuruf == 9) { cout << "nyeo"; } else if (TargetHuruf == 10) { cout << "o"; } else if (TargetHuruf == 11) { cout << "pi"; } else if (TargetHuruf == 12) { cout << "ra"; } else if (TargetHuruf == 13) { cout << "seu"; } else if (TargetHuruf == 14) { cout << "ta"; } cout << " Output = "; /////////////////////// cout << endl; //cout << "Threshold = " << threshold << endl; cout << "Jumlah pola = " << jumlah_image << endl; cout << "Generalisasi = " << generalisasi << "/" << jumlah_image*15 << " = " << (generalisasi*100)/(jumlah_image*15) << "%" << endl; in_data_jst.close(); in_weight.close(); system("pause"); return 0; }
Universitas Sumatera Utara
76
5. Modul Main Program #include #include #include #include #define #define #define #define
<stdlib.h> MENU_PROSES "image_proses.exe" MENU_TRAINING "Training_JST.exe" MENU_TESTING "Testing_JST.exe" MENU_PROSES_DISPLAY "display_image.exe"
using namespace std; void show_menu() { cout << endl; cout << "Main Menu -" << endl; cout << "=====================" << endl; cout << "1. Image Proses" << endl; cout << "2. Training JST" << endl; cout << "3. Testing JST" << endl; cout << "4. Contoh Image Proses" << endl; cout << "9. Tentang Saya" << endl; cout << "0. Exit" << endl << endl; } int main() { system("TITLE Pengenalan Kata Tulisan Tangan Huruf Korea"); int no_menu; do { show_menu(); cout << "Menu? "; cin >> no_menu; if(no_menu == 1) { cout << endl << "IMAGE PREPROCESSING" << endl; cout << "===================" << endl; system(MENU_PROSES); } else if(no_menu == 2) { cout << endl << "TRAINING" << endl; cout << "========" << endl; system(MENU_TRAINING); } else if(no_menu == 3) { cout << endl << "TESTING" << endl; cout << "========" << endl; system(MENU_TESTING); } else if(no_menu == 4) {
Universitas Sumatera Utara
77
cout << endl << "IMAGE PREPROCESSING EXAMPLE" << endl; cout << "===========================" << endl; system(MENU_PROSES_DISPLAY); } else if(no_menu == 9) { cout << endl; cout << "Rian Harris Nasution - 091402071" << endl; cout << "S1 Teknologi Informasi USU" << endl; cout << "Fakultas Ilmu Komputer dan Teknologi Informasi" << endl; cout << "Universitas Sumatera Utara" << endl; cout << endl; system("pause"); } else if(no_menu == 0) { break; } else no_menu = 99; }while(no_menu != 0); cout << "Thanks :)" << endl; system("pause"); return 0; }
Universitas Sumatera Utara