Ostatnio aktywny 1698157880

Rewizja 92598d553b204d1017be058c94e3e0b876417295

1.m Surowy
1clear; clc;
2
3% Load image (Type : UINT8)
4gambar1 = imread('Banana/1.jpg');
5gambar2 = imread('Banana/2.jpg');
6gambar3 = imread('Banana/3.jpg');
7gambar4 = imread('Banana/4.jpg');
8gambar5 = imread('Banana/5.jpg');
9gambar6 = imread('Banana/6.jpg');
10gambar7 = imread('Banana/7.jpg');
11gambar8 = imread('Banana/8.jpg');
12gambar9 = imread('Banana/9.jpg');
13gambar10 = imread('Banana/10.jpg');
14gambar11 = imread('Kiwi/1.jpg');
15gambar12 = imread('Kiwi/2.jpg');
16gambar13 = imread('Kiwi/3.jpg');
17gambar14 = imread('Kiwi/4.jpg');
18gambar15 = imread('Kiwi/5.jpg');
19gambar16 = imread('Kiwi/6.jpg');
20gambar17 = imread('Kiwi/7.jpg');
21gambar18 = imread('Kiwi/8.jpg');
22gambar19 = imread('Kiwi/9.jpg');
23gambar20 = imread('Kiwi/10.jpg');
24gambar21 = imread('Orange/1.jpg');
25gambar22 = imread('Orange/2.jpg');
26gambar23 = imread('Orange/3.jpg');
27gambar24 = imread('Orange/4.jpg');
28gambar25 = imread('Orange/5.jpg');
29gambar26 = imread('Orange/6.jpg');
30gambar27 = imread('Orange/7.jpg');
31gambar28 = imread('Orange/8.jpg');
32gambar29 = imread('Orange/9.jpg');
33gambar30 = imread('Orange/10.jpg');
34
35% Konversi UINT8 menjadi double agar dapat di operasikan komputasinya
36gambar1 = double(gambar1);
37gambar2 = double(gambar2);
38gambar3 = double(gambar3);
39gambar4 = double(gambar4);
40gambar5 = double(gambar5);
41gambar6 = double(gambar6);
42gambar7 = double(gambar7);
43gambar8 = double(gambar8);
44gambar9 = double(gambar9);
45gambar10 = double(gambar10);
46gambar11 = double(gambar11);
47gambar12 = double(gambar12);
48gambar13 = double(gambar13);
49gambar14 = double(gambar14);
50gambar15 = double(gambar15);
51gambar16 = double(gambar16);
52gambar17 = double(gambar17);
53gambar18 = double(gambar18);
54gambar19 = double(gambar19);
55gambar20 = double(gambar20);
56gambar21 = double(gambar21);
57gambar22 = double(gambar22);
58gambar23 = double(gambar23);
59gambar24 = double(gambar24);
60gambar25 = double(gambar25);
61gambar26 = double(gambar26);
62gambar27 = double(gambar27);
63gambar28 = double(gambar28);
64gambar29 = double(gambar29);
65gambar30 = double(gambar30);
66
67% 1,2,3 itu layer, RGB itu warnanya
68gambar1R = gambar1(:,:,1); % layer 1
69gambar1G = gambar1(:,:,2); % layer 2
70gambar1B = gambar1(:,:,3); % layer 3
71
72gambar2R = gambar2(:,:,1);
73gambar2G = gambar2(:,:,2);
74gambar2B = gambar2(:,:,3);
75
76gambar3R = gambar3(:,:,1);
77gambar3G = gambar3(:,:,2);
78gambar3B = gambar3(:,:,3);
79
80gambar4R = gambar4(:,:,1);
81gambar4G = gambar4(:,:,2);
82gambar4B = gambar4(:,:,3);
83
84gambar5R = gambar5(:,:,1);
85gambar5G = gambar5(:,:,2);
86gambar5B = gambar5(:,:,3);
87
88gambar6R = gambar6(:,:,1);
89gambar6G = gambar6(:,:,2);
90gambar6B = gambar6(:,:,3);
91
92gambar7R = gambar7(:,:,1);
93gambar7G = gambar7(:,:,2);
94gambar7B = gambar7(:,:,3);
95
96gambar8R = gambar8(:,:,1);
97gambar8G = gambar8(:,:,2);
98gambar8B = gambar8(:,:,3);
99
100gambar9R = gambar9(:,:,1);
101gambar9G = gambar9(:,:,2);
102gambar9B = gambar9(:,:,3);
103
104gambar10R = gambar10(:,:,1);
105gambar10G = gambar10(:,:,2);
106gambar10B = gambar10(:,:,3);
107
108gambar11R = gambar11(:,:,1);
109gambar11G = gambar11(:,:,2);
110gambar11B = gambar11(:,:,3);
111
112gambar12R = gambar12(:,:,1);
113gambar12G = gambar12(:,:,2);
114gambar12B = gambar12(:,:,3);
115
116gambar13R = gambar13(:,:,1);
117gambar13G = gambar13(:,:,2);
118gambar13B = gambar13(:,:,3);
119
120gambar14R = gambar14(:,:,1);
121gambar14G = gambar14(:,:,2);
122gambar14B = gambar14(:,:,3);
123
124gambar15R = gambar15(:,:,1);
125gambar15G = gambar15(:,:,2);
126gambar15B = gambar15(:,:,3);
127
128gambar16R = gambar16(:,:,1);
129gambar16G = gambar16(:,:,2);
130gambar16B = gambar16(:,:,3);
131
132gambar17R = gambar17(:,:,1);
133gambar17G = gambar17(:,:,2);
134gambar17B = gambar17(:,:,3);
135
136gambar18R = gambar18(:,:,1);
137gambar18G = gambar18(:,:,2);
138gambar18B = gambar18(:,:,3);
139
140gambar19R = gambar19(:,:,1);
141gambar19G = gambar19(:,:,2);
142gambar19B = gambar19(:,:,3);
143
144gambar20R = gambar20(:,:,1);
145gambar20G = gambar20(:,:,2);
146gambar20B = gambar20(:,:,3);
147
148gambar21R = gambar21(:,:,1);
149gambar21G = gambar21(:,:,2);
150gambar21B = gambar21(:,:,3);
151
152gambar22R = gambar22(:,:,1);
153gambar22G = gambar22(:,:,2);
154gambar22B = gambar22(:,:,3);
155
156gambar23R = gambar23(:,:,1);
157gambar23G = gambar23(:,:,2);
158gambar23B = gambar23(:,:,3);
159
160gambar24R = gambar24(:,:,1);
161gambar24G = gambar24(:,:,2);
162gambar24B = gambar24(:,:,3);
163
164gambar25R = gambar25(:,:,1);
165gambar25G = gambar25(:,:,2);
166gambar25B = gambar25(:,:,3);
167
168gambar26R = gambar26(:,:,1);
169gambar26G = gambar26(:,:,2);
170gambar26B = gambar26(:,:,3);
171
172gambar27R = gambar27(:,:,1);
173gambar27G = gambar27(:,:,2);
174gambar27B = gambar27(:,:,3);
175
176gambar28R = gambar28(:,:,1);
177gambar28G = gambar28(:,:,2);
178gambar28B = gambar28(:,:,3);
179
180gambar29R = gambar29(:,:,1);
181gambar29G = gambar29(:,:,2);
182gambar29B = gambar29(:,:,3);
183
184gambar30R = gambar30(:,:,1);
185gambar30G = gambar30(:,:,2);
186gambar30B = gambar30(:,:,3);
187
188[n,p] = size(gambar1R);
189
190k = 1;
191for i=1:n
192 for j=1:p
193 vgambar1R(k,1) = gambar1R(i,j);
194 vgambar1G(k,1) = gambar1G(i,j);
195 vgambar1B(k,1) = gambar1B(i,j);
196 mat_gambar1 = [vgambar1R, vgambar1G, vgambar1B];
197
198
199 vgambar2R(k,1) = gambar2R(i,j);
200 vgambar2G(k,1) = gambar2G(i,j);
201 vgambar2B(k,1) = gambar2B(i,j);
202 mat_gambar2 = [vgambar2R, vgambar2G, vgambar2B];
203
204
205 vgambar3R(k,1) = gambar3R(i,j);
206 vgambar3G(k,1) = gambar3G(i,j);
207 vgambar3B(k,1) = gambar3B(i,j);
208 mat_gambar3 = [vgambar3R, vgambar3G, vgambar3B];
209
210
211 vgambar4R(k,1) = gambar4R(i,j);
212 vgambar4G(k,1) = gambar4G(i,j);
213 vgambar4B(k,1) = gambar4B(i,j);
214 mat_gambar4 = [vgambar4R, vgambar4G, vgambar4B];
215
216
217 vgambar5R(k,1) = gambar5R(i,j);
218 vgambar5G(k,1) = gambar5G(i,j);
219 vgambar5B(k,1) = gambar5B(i,j);
220 mat_gambar5 = [vgambar5R, vgambar5G, vgambar5B];
221
222
223 vgambar6R(k,1) = gambar6R(i,j);
224 vgambar6G(k,1) = gambar6G(i,j);
225 vgambar6B(k,1) = gambar6B(i,j);
226 mat_gambar6 = [vgambar6R, vgambar6G, vgambar6B];
227
228
229 vgambar7R(k,1) = gambar7R(i,j);
230 vgambar7G(k,1) = gambar7G(i,j);
231 vgambar7B(k,1) = gambar7B(i,j);
232 mat_gambar7 = [vgambar7R, vgambar7G, vgambar7B];
233
234
235 vgambar8R(k,1) = gambar8R(i,j);
236 vgambar8G(k,1) = gambar8G(i,j);
237 vgambar8B(k,1) = gambar8B(i,j);
238 mat_gambar8 = [vgambar8R, vgambar8G, vgambar8B];
239
240
241 vgambar9R(k,1) = gambar9R(i,j);
242 vgambar9G(k,1) = gambar9G(i,j);
243 vgambar9B(k,1) = gambar9B(i,j);
244 mat_gambar9 = [vgambar9R, vgambar9G, vgambar9B];
245
246
247 vgambar10R(k,1) = gambar10R(i,j);
248 vgambar10G(k,1) = gambar10G(i,j);
249 vgambar10B(k,1) = gambar10B(i,j);
250 mat_gambar10 = [vgambar10R, vgambar10G, vgambar10B];
251
252
253 vgambar11R(k,1) = gambar11R(i,j);
254 vgambar11G(k,1) = gambar11G(i,j);
255 vgambar11B(k,1) = gambar11B(i,j);
256 mat_gambar11 = [vgambar11R, vgambar11G, vgambar11B];
257
258
259 vgambar12R(k,1) = gambar12R(i,j);
260 vgambar12G(k,1) = gambar12G(i,j);
261 vgambar12B(k,1) = gambar12B(i,j);
262 mat_gambar12 = [vgambar12R, vgambar12G, vgambar12B];
263
264
265 vgambar13R(k,1) = gambar13R(i,j);
266 vgambar13G(k,1) = gambar13G(i,j);
267 vgambar13B(k,1) = gambar13B(i,j);
268 mat_gambar13 = [vgambar13R, vgambar13G, vgambar13B];
269
270
271 vgambar14R(k,1) = gambar14R(i,j);
272 vgambar14G(k,1) = gambar14G(i,j);
273 vgambar14B(k,1) = gambar14B(i,j);
274 mat_gambar14 = [vgambar14R, vgambar14G, vgambar14B];
275
276
277 vgambar15R(k,1) = gambar15R(i,j);
278 vgambar15G(k,1) = gambar15G(i,j);
279 vgambar15B(k,1) = gambar15B(i,j);
280 mat_gambar15 = [vgambar15R, vgambar15G, vgambar15B];
281
282
283 vgambar16R(k,1) = gambar16R(i,j);
284 vgambar16G(k,1) = gambar16G(i,j);
285 vgambar16B(k,1) = gambar16B(i,j);
286 mat_gambar16 = [vgambar16R, vgambar16G, vgambar16B];
287
288
289 vgambar17R(k,1) = gambar17R(i,j);
290 vgambar17G(k,1) = gambar17G(i,j);
291 vgambar17B(k,1) = gambar17B(i,j);
292 mat_gambar17 = [vgambar17R, vgambar17G, vgambar17B];
293
294
295 vgambar18R(k,1) = gambar18R(i,j);
296 vgambar18G(k,1) = gambar18G(i,j);
297 vgambar18B(k,1) = gambar18B(i,j);
298 mat_gambar18 = [vgambar18R, vgambar18G, vgambar18B];
299
300
301 vgambar19R(k,1) = gambar19R(i,j);
302 vgambar19G(k,1) = gambar19G(i,j);
303 vgambar19B(k,1) = gambar19B(i,j);
304 mat_gambar19 = [vgambar19R, vgambar19G, vgambar19B];
305
306
307 vgambar20R(k,1) = gambar20R(i,j);
308 vgambar20G(k,1) = gambar20G(i,j);
309 vgambar20B(k,1) = gambar20B(i,j);
310 mat_gambar20 = [vgambar20R, vgambar20G, vgambar20B];
311
312
313 vgambar21R(k,1) = gambar21R(i,j);
314 vgambar21G(k,1) = gambar21G(i,j);
315 vgambar21B(k,1) = gambar21B(i,j);
316 mat_gambar21 = [vgambar21R, vgambar21G, vgambar21B];
317
318 vgambar22R(k,1) = gambar22R(i,j);
319 vgambar22G(k,1) = gambar22G(i,j);
320 vgambar22B(k,1) = gambar22B(i,j);
321 mat_gambar22 = [vgambar22R, vgambar22G, vgambar22B];
322
323 vgambar23R(k,1) = gambar23R(i,j);
324 vgambar23G(k,1) = gambar23G(i,j);
325 vgambar23B(k,1) = gambar23B(i,j);
326 mat_gambar23 = [vgambar23R, vgambar23G, vgambar23B];
327
328 vgambar24R(k,1) = gambar24R(i,j);
329 vgambar24G(k,1) = gambar24G(i,j);
330 vgambar24B(k,1) = gambar24B(i,j);
331 mat_gambar24 = [vgambar24R, vgambar24G, vgambar24B];
332
333 vgambar25R(k,1) = gambar25R(i,j);
334 vgambar25G(k,1) = gambar25G(i,j);
335 vgambar25B(k,1) = gambar25B(i,j);
336 mat_gambar25 = [vgambar25R, vgambar25G, vgambar25B];
337
338 vgambar26R(k,1) = gambar26R(i,j);
339 vgambar26G(k,1) = gambar26G(i,j);
340 vgambar26B(k,1) = gambar26B(i,j);
341 mat_gambar26 = [vgambar26R, vgambar26G, vgambar26B];
342
343 vgambar27R(k,1) = gambar27R(i,j);
344 vgambar27G(k,1) = gambar27G(i,j);
345 vgambar27B(k,1) = gambar27B(i,j);
346 mat_gambar27 = [vgambar27R, vgambar27G, vgambar27B];
347
348 vgambar28R(k,1) = gambar28R(i,j);
349 vgambar28G(k,1) = gambar28G(i,j);
350 vgambar28B(k,1) = gambar28B(i,j);
351 mat_gambar28 = [vgambar28R, vgambar28G, vgambar28B];
352
353 vgambar29R(k,1) = gambar29R(i,j);
354 vgambar29G(k,1) = gambar29G(i,j);
355 vgambar29B(k,1) = gambar29B(i,j);
356 mat_gambar29 = [vgambar29R, vgambar29G, vgambar29B];
357
358 vgambar30R(k,1) = gambar30R(i,j);
359 vgambar30G(k,1) = gambar30G(i,j);
360 vgambar30B(k,1) = gambar30B(i,j);
361 mat_gambar30 = [vgambar30R, vgambar30G, vgambar30B];
362
363 k = k + 1;
364 end
365end
366
367% Merubah RGB ke Greyscale
368GYS1 = 0.299 * mat_gambar1(:,1) + 0.587 * mat_gambar1(:,2) + 0.114 * mat_gambar1(:,3);
369GYS2 = 0.299 * mat_gambar2(:,1) + 0.587 * mat_gambar2(:,2) + 0.114 * mat_gambar2(:,3);
370GYS3 = 0.299 * mat_gambar3(:,1) + 0.587 * mat_gambar3(:,2) + 0.114 * mat_gambar3(:,3);
371GYS4 = 0.299 * mat_gambar4(:,1) + 0.587 * mat_gambar4(:,2) + 0.114 * mat_gambar4(:,3);
372GYS5 = 0.299 * mat_gambar5(:,1) + 0.587 * mat_gambar5(:,2) + 0.114 * mat_gambar5(:,3);
373GYS6 = 0.299 * mat_gambar6(:,1) + 0.587 * mat_gambar6(:,2) + 0.114 * mat_gambar6(:,3);
374GYS7 = 0.299 * mat_gambar7(:,1) + 0.587 * mat_gambar7(:,2) + 0.114 * mat_gambar7(:,3);
375GYS8 = 0.299 * mat_gambar8(:,1) + 0.587 * mat_gambar8(:,2) + 0.114 * mat_gambar8(:,3);
376GYS9 = 0.299 * mat_gambar9(:,1) + 0.587 * mat_gambar9(:,2) + 0.114 * mat_gambar9(:,3);
377GYS10 = 0.299 * mat_gambar10(:,1) + 0.587 * mat_gambar10(:,2) + 0.114 * mat_gambar10(:,3);
378GYS11 = 0.299 * mat_gambar11(:,1) + 0.587 * mat_gambar11(:,2) + 0.114 * mat_gambar11(:,3);
379GYS12 = 0.299 * mat_gambar12(:,1) + 0.587 * mat_gambar12(:,2) + 0.114 * mat_gambar12(:,3);
380GYS13 = 0.299 * mat_gambar13(:,1) + 0.587 * mat_gambar13(:,2) + 0.114 * mat_gambar13(:,3);
381GYS14 = 0.299 * mat_gambar14(:,1) + 0.587 * mat_gambar14(:,2) + 0.114 * mat_gambar14(:,3);
382GYS15 = 0.299 * mat_gambar15(:,1) + 0.587 * mat_gambar15(:,2) + 0.114 * mat_gambar15(:,3);
383GYS16 = 0.299 * mat_gambar16(:,1) + 0.587 * mat_gambar16(:,2) + 0.114 * mat_gambar16(:,3);
384GYS17 = 0.299 * mat_gambar17(:,1) + 0.587 * mat_gambar17(:,2) + 0.114 * mat_gambar17(:,3);
385GYS18 = 0.299 * mat_gambar18(:,1) + 0.587 * mat_gambar18(:,2) + 0.114 * mat_gambar18(:,3);
386GYS19 = 0.299 * mat_gambar19(:,1) + 0.587 * mat_gambar19(:,2) + 0.114 * mat_gambar19(:,3);
387GYS20 = 0.299 * mat_gambar20(:,1) + 0.587 * mat_gambar20(:,2) + 0.114 * mat_gambar20(:,3);
388GYS21 = 0.299 * mat_gambar21(:,1) + 0.587 * mat_gambar21(:,2) + 0.114 * mat_gambar21(:,3);
389GYS22 = 0.299 * mat_gambar22(:,1) + 0.587 * mat_gambar22(:,2) + 0.114 * mat_gambar22(:,3);
390GYS23 = 0.299 * mat_gambar23(:,1) + 0.587 * mat_gambar23(:,2) + 0.114 * mat_gambar23(:,3);
391GYS24 = 0.299 * mat_gambar24(:,1) + 0.587 * mat_gambar24(:,2) + 0.114 * mat_gambar24(:,3);
392GYS25 = 0.299 * mat_gambar25(:,1) + 0.587 * mat_gambar25(:,2) + 0.114 * mat_gambar25(:,3);
393GYS26 = 0.299 * mat_gambar26(:,1) + 0.587 * mat_gambar26(:,2) + 0.114 * mat_gambar26(:,3);
394GYS27 = 0.299 * mat_gambar27(:,1) + 0.587 * mat_gambar27(:,2) + 0.114 * mat_gambar27(:,3);
395GYS28 = 0.299 * mat_gambar28(:,1) + 0.587 * mat_gambar28(:,2) + 0.114 * mat_gambar28(:,3);
396GYS29 = 0.299 * mat_gambar29(:,1) + 0.587 * mat_gambar29(:,2) + 0.114 * mat_gambar29(:,3);
397GYS30 = 0.299 * mat_gambar30(:,1) + 0.587 * mat_gambar30(:,2) + 0.114 * mat_gambar30(:,3);
398
399GYS1 = GYS1';
400GYS2 = GYS2';
401GYS3 = GYS3';
402GYS4 = GYS4';
403GYS5 = GYS5';
404GYS6 = GYS6';
405GYS7 = GYS7';
406GYS8 = GYS8';
407GYS9 = GYS9';
408GYS10 = GYS10';
409GYS11 = GYS11';
410GYS12 = GYS12';
411GYS13 = GYS13';
412GYS14 = GYS14';
413GYS15 = GYS15';
414GYS16 = GYS16';
415GYS17 = GYS17';
416GYS18 = GYS18';
417GYS19 = GYS19';
418GYS20 = GYS20';
419GYS21 = GYS21';
420GYS22 = GYS22';
421GYS23 = GYS23';
422GYS24 = GYS24';
423GYS25 = GYS25';
424GYS26 = GYS26';
425GYS27 = GYS27';
426GYS28 = GYS28';
427GYS29 = GYS29';
428GYS30 = GYS30';
429
430DataGYS = [GYS1; GYS2; GYS3; GYS4; GYS5; GYS6; GYS7; GYS8; GYS9; GYS10; GYS11; GYS12; GYS13; GYS14; GYS15; GYS16; GYS17; GYS18; GYS19; GYS20; GYS21; GYS22; GYS23; GYS24; GYS25; GYS26; GYS27; GYS28; GYS29; GYS30;];
431
432[n,p] = size(DataGYS);
433Xrata = mean(DataGYS);
434
435for i = 1:n
436 Z(i, :) = DataGYS(i, :) - Xrata;
437end
438
439S = cov(DataGYS);
440
441for i = 1:n
442 for j = 1:p
443 Zr(i,j) = Z(i,j) / sqrt(S(j,j)); % Zr adalah data hasil normalisasi
444 end
445end
446
447Zcov = cov(Zr);
448
449[VA, DA] = eig(Zcov);
450
451m = 1
452for i = p:-1:1
453 DA_urut(m,m) = DA(i,i);
454 VA_urut(m,:) = VA(i,:);
455 m = m+1;
456end
457
458for i = 1:p
459 eValue(i,1) = DA_urut(i,i);
460end
461
462Total = sum(eValue)
463
464PK = 0;
465komponen = 0;%jumlah komponen utama yang dipertahankan
466
467for j = 1:p
468 proporsiValue(j,:) = eValue(j,1) / Total;%mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
469 if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
470 PK = PK + proporsiValue(j, :);
471 komponen = komponen + 1;
472 end
473end
474
475VA_Komponen = VA_urut(:,1:komponen)
476
477W = Z * VA_Komponen
478
479plot3(W(:,1), W(:,2), W(:,3), '*')
480
481grid on
2.m Surowy
1clear; clc;
2
3% Get Data
4gbr1 = imread('images/Banana/1.jpg');
5gbr2 = imread('images/Banana/2.jpg');
6gbr3 = imread('images/Banana/3.jpg');
7gbr4 = imread('images/Banana/4.jpg');
8gbr5 = imread('images/Banana/5.jpg');
9gbr6 = imread('images/Banana/6.jpg');
10gbr7 = imread('images/Banana/7.jpg');
11gbr8 = imread('images/Banana/8.jpg');
12gbr9 = imread('images/Banana/9.jpg');
13gbr10 = imread('images/Banana/10.jpg');
14gbr11 = imread('images/Kiwi/1.jpg');
15gbr12 = imread('images/Kiwi/2.jpg');
16gbr13 = imread('images/Kiwi/3.jpg');
17gbr14 = imread('images/Kiwi/4.jpg');
18gbr15 = imread('images/Kiwi/5.jpg');
19gbr16 = imread('images/Kiwi/6.jpg');
20gbr17 = imread('images/Kiwi/7.jpg');
21gbr18 = imread('images/Kiwi/8.jpg');
22gbr19 = imread('images/Kiwi/9.jpg');
23gbr20 = imread('images/Kiwi/10.jpg');
24gbr21 = imread('images/Orange/1.jpg');
25gbr22 = imread('images/Orange/2.jpg');
26gbr23 = imread('images/Orange/3.jpg');
27gbr24 = imread('images/Orange/4.jpg');
28gbr25 = imread('images/Orange/5.jpg');
29gbr26 = imread('images/Orange/6.jpg');
30gbr27 = imread('images/Orange/7.jpg');
31gbr28 = imread('images/Orange/8.jpg');
32gbr29 = imread('images/Orange/9.jpg');
33gbr30 = imread('images/Orange/10.jpg');
34gbr1=double(rgb2gray(gbr1));
35gbr2=double(rgb2gray(gbr2));
36gbr3=double(rgb2gray(gbr3));
37gbr4=double(rgb2gray(gbr4));
38gbr5=double(rgb2gray(gbr5));
39gbr6=double(rgb2gray(gbr6));
40gbr7=double(rgb2gray(gbr7));
41gbr8=double(rgb2gray(gbr8));
42gbr9=double(rgb2gray(gbr9));
43gbr10=double(rgb2gray(gbr10));
44gbr11=double(rgb2gray(gbr11));
45gbr12=double(rgb2gray(gbr12));
46gbr13=double(rgb2gray(gbr13));
47gbr14=double(rgb2gray(gbr14));
48gbr15=double(rgb2gray(gbr15));
49gbr16=double(rgb2gray(gbr16));
50gbr17=double(rgb2gray(gbr17));
51gbr18=double(rgb2gray(gbr18));
52gbr19=double(rgb2gray(gbr19));
53gbr20=double(rgb2gray(gbr20));
54gbr21=double(rgb2gray(gbr21));
55gbr22=double(rgb2gray(gbr22));
56gbr23=double(rgb2gray(gbr23));
57gbr24=double(rgb2gray(gbr24));
58gbr25=double(rgb2gray(gbr25));
59gbr26=double(rgb2gray(gbr26));
60gbr27=double(rgb2gray(gbr27));
61gbr28=double(rgb2gray(gbr28));
62gbr29=double(rgb2gray(gbr29));
63gbr30=double(rgb2gray(gbr30));
64
65gbr1=reshape(gbr1',1,[]);
66gbr2=reshape(gbr2',1,[]);
67gbr3=reshape(gbr3',1,[]);
68gbr4=reshape(gbr4',1,[]);
69gbr5=reshape(gbr5',1,[]);
70gbr6=reshape(gbr6',1,[]);
71gbr7=reshape(gbr7',1,[]);
72gbr8=reshape(gbr8',1,[]);
73gbr9=reshape(gbr9',1,[]);
74gbr10=reshape(gbr10',1,[]);
75gbr11=reshape(gbr11',1,[]);
76gbr12=reshape(gbr12',1,[]);
77gbr13=reshape(gbr13',1,[]);
78gbr14=reshape(gbr14',1,[]);
79gbr15=reshape(gbr15',1,[]);
80gbr16=reshape(gbr16',1,[]);
81gbr17=reshape(gbr17',1,[]);
82gbr18=reshape(gbr18',1,[]);
83gbr19=reshape(gbr19',1,[]);
84gbr20=reshape(gbr20',1,[]);
85gbr21=reshape(gbr21',1,[]);
86gbr22=reshape(gbr22',1,[]);
87gbr23=reshape(gbr23',1,[]);
88gbr24=reshape(gbr24',1,[]);
89gbr25=reshape(gbr25',1,[]);
90gbr26=reshape(gbr26',1,[]);
91gbr27=reshape(gbr27',1,[]);
92gbr28=reshape(gbr28',1,[]);
93gbr29=reshape(gbr29',1,[]);
94gbr30=reshape(gbr30',1,[]);
95
96X = [gbr1; gbr2; gbr3; gbr4; gbr5; gbr6; gbr7; gbr8; gbr9; gbr10; gbr11; gbr12; gbr13; gbr14; gbr15; gbr16; gbr17; gbr18; gbr19; gbr20; gbr21; gbr22; gbr23; gbr24; gbr25; gbr26; gbr27; gbr28; gbr29; gbr30];
97[n,p] = size(X);
98Xrata = mean(X, 1);
99
100%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
101Z = zeros(size(X));
102for i = 1:n
103 Z(i, :) = X(i, :)-Xrata;
104end
105
106%Menghitung covariance matrix
107S = cov(X); % varian
108
109%Standardization Data
110Zr = zeros(size(Z));
111for i = 1:n
112 for j = 1:p
113 Zr(i,j) = Z(i,j) / sqrt(S(j,j));
114 end
115end
116% Zr ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL)
117
118%2. Menghitung cov dari data yang sudah direduksi
119Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr
120
121%3. Menghitung eigen value dan eigen vector dari Zcov
122[VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value
123
124%4. menghitung komponen utama
125% Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar
126% yang diperlukan dari besar ke kecil
127%mengurutkan hasil eigen (DESC)
128m = 1;
129for i = p:-1:1
130 DA_urut(m,m) = DA(i,i);
131 VA_urut(m,:) = VA(:,i)';
132 m = m+1;
133end
134
135%menghitung komponen utama
136eValue = zeros(p, 1);
137for i = 1:p
138 eValue(i,1) = DA_urut(i,i);
139end
140
141Total = sum(eValue); % total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama.
142
143% Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang
144% dianggap perlu
145PK = 0;
146komponen = 0; % jumlah komponen utama yang dipertahankan
147
148proporsiValue = zeros(p, 1);
149for j = 1:p
150 proporsiValue(j) = eValue(j,1) / Total; % mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
151 if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
152 PK = PK + proporsiValue(j);
153 komponen = komponen + 1;
154 end
155end
156
157% Mengambil eigen vector dari komponen yang terpilih
158VA_Komponen = VA_urut(:,1:komponen);
159% W = variable komponen utama / principal component variable
160W = Zr * VA_Komponen; % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF
161
162plot3(W(:,1), W(:,2), W(:,3), '*'); % PK < 0.9
163%plot(W(:,1), W(:,2), '*'); % PK < 0.8
164grid on