1.m
· 14 KiB · Mathematica
Ham
clear; clc;
% Load image (Type : UINT8)
gambar1 = imread('Banana/1.jpg');
gambar2 = imread('Banana/2.jpg');
gambar3 = imread('Banana/3.jpg');
gambar4 = imread('Banana/4.jpg');
gambar5 = imread('Banana/5.jpg');
gambar6 = imread('Banana/6.jpg');
gambar7 = imread('Banana/7.jpg');
gambar8 = imread('Banana/8.jpg');
gambar9 = imread('Banana/9.jpg');
gambar10 = imread('Banana/10.jpg');
gambar11 = imread('Kiwi/1.jpg');
gambar12 = imread('Kiwi/2.jpg');
gambar13 = imread('Kiwi/3.jpg');
gambar14 = imread('Kiwi/4.jpg');
gambar15 = imread('Kiwi/5.jpg');
gambar16 = imread('Kiwi/6.jpg');
gambar17 = imread('Kiwi/7.jpg');
gambar18 = imread('Kiwi/8.jpg');
gambar19 = imread('Kiwi/9.jpg');
gambar20 = imread('Kiwi/10.jpg');
gambar21 = imread('Orange/1.jpg');
gambar22 = imread('Orange/2.jpg');
gambar23 = imread('Orange/3.jpg');
gambar24 = imread('Orange/4.jpg');
gambar25 = imread('Orange/5.jpg');
gambar26 = imread('Orange/6.jpg');
gambar27 = imread('Orange/7.jpg');
gambar28 = imread('Orange/8.jpg');
gambar29 = imread('Orange/9.jpg');
gambar30 = imread('Orange/10.jpg');
% Konversi UINT8 menjadi double agar dapat di operasikan komputasinya
gambar1 = double(gambar1);
gambar2 = double(gambar2);
gambar3 = double(gambar3);
gambar4 = double(gambar4);
gambar5 = double(gambar5);
gambar6 = double(gambar6);
gambar7 = double(gambar7);
gambar8 = double(gambar8);
gambar9 = double(gambar9);
gambar10 = double(gambar10);
gambar11 = double(gambar11);
gambar12 = double(gambar12);
gambar13 = double(gambar13);
gambar14 = double(gambar14);
gambar15 = double(gambar15);
gambar16 = double(gambar16);
gambar17 = double(gambar17);
gambar18 = double(gambar18);
gambar19 = double(gambar19);
gambar20 = double(gambar20);
gambar21 = double(gambar21);
gambar22 = double(gambar22);
gambar23 = double(gambar23);
gambar24 = double(gambar24);
gambar25 = double(gambar25);
gambar26 = double(gambar26);
gambar27 = double(gambar27);
gambar28 = double(gambar28);
gambar29 = double(gambar29);
gambar30 = double(gambar30);
% 1,2,3 itu layer, RGB itu warnanya
gambar1R = gambar1(:,:,1); % layer 1
gambar1G = gambar1(:,:,2); % layer 2
gambar1B = gambar1(:,:,3); % layer 3
gambar2R = gambar2(:,:,1);
gambar2G = gambar2(:,:,2);
gambar2B = gambar2(:,:,3);
gambar3R = gambar3(:,:,1);
gambar3G = gambar3(:,:,2);
gambar3B = gambar3(:,:,3);
gambar4R = gambar4(:,:,1);
gambar4G = gambar4(:,:,2);
gambar4B = gambar4(:,:,3);
gambar5R = gambar5(:,:,1);
gambar5G = gambar5(:,:,2);
gambar5B = gambar5(:,:,3);
gambar6R = gambar6(:,:,1);
gambar6G = gambar6(:,:,2);
gambar6B = gambar6(:,:,3);
gambar7R = gambar7(:,:,1);
gambar7G = gambar7(:,:,2);
gambar7B = gambar7(:,:,3);
gambar8R = gambar8(:,:,1);
gambar8G = gambar8(:,:,2);
gambar8B = gambar8(:,:,3);
gambar9R = gambar9(:,:,1);
gambar9G = gambar9(:,:,2);
gambar9B = gambar9(:,:,3);
gambar10R = gambar10(:,:,1);
gambar10G = gambar10(:,:,2);
gambar10B = gambar10(:,:,3);
gambar11R = gambar11(:,:,1);
gambar11G = gambar11(:,:,2);
gambar11B = gambar11(:,:,3);
gambar12R = gambar12(:,:,1);
gambar12G = gambar12(:,:,2);
gambar12B = gambar12(:,:,3);
gambar13R = gambar13(:,:,1);
gambar13G = gambar13(:,:,2);
gambar13B = gambar13(:,:,3);
gambar14R = gambar14(:,:,1);
gambar14G = gambar14(:,:,2);
gambar14B = gambar14(:,:,3);
gambar15R = gambar15(:,:,1);
gambar15G = gambar15(:,:,2);
gambar15B = gambar15(:,:,3);
gambar16R = gambar16(:,:,1);
gambar16G = gambar16(:,:,2);
gambar16B = gambar16(:,:,3);
gambar17R = gambar17(:,:,1);
gambar17G = gambar17(:,:,2);
gambar17B = gambar17(:,:,3);
gambar18R = gambar18(:,:,1);
gambar18G = gambar18(:,:,2);
gambar18B = gambar18(:,:,3);
gambar19R = gambar19(:,:,1);
gambar19G = gambar19(:,:,2);
gambar19B = gambar19(:,:,3);
gambar20R = gambar20(:,:,1);
gambar20G = gambar20(:,:,2);
gambar20B = gambar20(:,:,3);
gambar21R = gambar21(:,:,1);
gambar21G = gambar21(:,:,2);
gambar21B = gambar21(:,:,3);
gambar22R = gambar22(:,:,1);
gambar22G = gambar22(:,:,2);
gambar22B = gambar22(:,:,3);
gambar23R = gambar23(:,:,1);
gambar23G = gambar23(:,:,2);
gambar23B = gambar23(:,:,3);
gambar24R = gambar24(:,:,1);
gambar24G = gambar24(:,:,2);
gambar24B = gambar24(:,:,3);
gambar25R = gambar25(:,:,1);
gambar25G = gambar25(:,:,2);
gambar25B = gambar25(:,:,3);
gambar26R = gambar26(:,:,1);
gambar26G = gambar26(:,:,2);
gambar26B = gambar26(:,:,3);
gambar27R = gambar27(:,:,1);
gambar27G = gambar27(:,:,2);
gambar27B = gambar27(:,:,3);
gambar28R = gambar28(:,:,1);
gambar28G = gambar28(:,:,2);
gambar28B = gambar28(:,:,3);
gambar29R = gambar29(:,:,1);
gambar29G = gambar29(:,:,2);
gambar29B = gambar29(:,:,3);
gambar30R = gambar30(:,:,1);
gambar30G = gambar30(:,:,2);
gambar30B = gambar30(:,:,3);
[n,p] = size(gambar1R);
k = 1;
for i=1:n
for j=1:p
vgambar1R(k,1) = gambar1R(i,j);
vgambar1G(k,1) = gambar1G(i,j);
vgambar1B(k,1) = gambar1B(i,j);
mat_gambar1 = [vgambar1R, vgambar1G, vgambar1B];
vgambar2R(k,1) = gambar2R(i,j);
vgambar2G(k,1) = gambar2G(i,j);
vgambar2B(k,1) = gambar2B(i,j);
mat_gambar2 = [vgambar2R, vgambar2G, vgambar2B];
vgambar3R(k,1) = gambar3R(i,j);
vgambar3G(k,1) = gambar3G(i,j);
vgambar3B(k,1) = gambar3B(i,j);
mat_gambar3 = [vgambar3R, vgambar3G, vgambar3B];
vgambar4R(k,1) = gambar4R(i,j);
vgambar4G(k,1) = gambar4G(i,j);
vgambar4B(k,1) = gambar4B(i,j);
mat_gambar4 = [vgambar4R, vgambar4G, vgambar4B];
vgambar5R(k,1) = gambar5R(i,j);
vgambar5G(k,1) = gambar5G(i,j);
vgambar5B(k,1) = gambar5B(i,j);
mat_gambar5 = [vgambar5R, vgambar5G, vgambar5B];
vgambar6R(k,1) = gambar6R(i,j);
vgambar6G(k,1) = gambar6G(i,j);
vgambar6B(k,1) = gambar6B(i,j);
mat_gambar6 = [vgambar6R, vgambar6G, vgambar6B];
vgambar7R(k,1) = gambar7R(i,j);
vgambar7G(k,1) = gambar7G(i,j);
vgambar7B(k,1) = gambar7B(i,j);
mat_gambar7 = [vgambar7R, vgambar7G, vgambar7B];
vgambar8R(k,1) = gambar8R(i,j);
vgambar8G(k,1) = gambar8G(i,j);
vgambar8B(k,1) = gambar8B(i,j);
mat_gambar8 = [vgambar8R, vgambar8G, vgambar8B];
vgambar9R(k,1) = gambar9R(i,j);
vgambar9G(k,1) = gambar9G(i,j);
vgambar9B(k,1) = gambar9B(i,j);
mat_gambar9 = [vgambar9R, vgambar9G, vgambar9B];
vgambar10R(k,1) = gambar10R(i,j);
vgambar10G(k,1) = gambar10G(i,j);
vgambar10B(k,1) = gambar10B(i,j);
mat_gambar10 = [vgambar10R, vgambar10G, vgambar10B];
vgambar11R(k,1) = gambar11R(i,j);
vgambar11G(k,1) = gambar11G(i,j);
vgambar11B(k,1) = gambar11B(i,j);
mat_gambar11 = [vgambar11R, vgambar11G, vgambar11B];
vgambar12R(k,1) = gambar12R(i,j);
vgambar12G(k,1) = gambar12G(i,j);
vgambar12B(k,1) = gambar12B(i,j);
mat_gambar12 = [vgambar12R, vgambar12G, vgambar12B];
vgambar13R(k,1) = gambar13R(i,j);
vgambar13G(k,1) = gambar13G(i,j);
vgambar13B(k,1) = gambar13B(i,j);
mat_gambar13 = [vgambar13R, vgambar13G, vgambar13B];
vgambar14R(k,1) = gambar14R(i,j);
vgambar14G(k,1) = gambar14G(i,j);
vgambar14B(k,1) = gambar14B(i,j);
mat_gambar14 = [vgambar14R, vgambar14G, vgambar14B];
vgambar15R(k,1) = gambar15R(i,j);
vgambar15G(k,1) = gambar15G(i,j);
vgambar15B(k,1) = gambar15B(i,j);
mat_gambar15 = [vgambar15R, vgambar15G, vgambar15B];
vgambar16R(k,1) = gambar16R(i,j);
vgambar16G(k,1) = gambar16G(i,j);
vgambar16B(k,1) = gambar16B(i,j);
mat_gambar16 = [vgambar16R, vgambar16G, vgambar16B];
vgambar17R(k,1) = gambar17R(i,j);
vgambar17G(k,1) = gambar17G(i,j);
vgambar17B(k,1) = gambar17B(i,j);
mat_gambar17 = [vgambar17R, vgambar17G, vgambar17B];
vgambar18R(k,1) = gambar18R(i,j);
vgambar18G(k,1) = gambar18G(i,j);
vgambar18B(k,1) = gambar18B(i,j);
mat_gambar18 = [vgambar18R, vgambar18G, vgambar18B];
vgambar19R(k,1) = gambar19R(i,j);
vgambar19G(k,1) = gambar19G(i,j);
vgambar19B(k,1) = gambar19B(i,j);
mat_gambar19 = [vgambar19R, vgambar19G, vgambar19B];
vgambar20R(k,1) = gambar20R(i,j);
vgambar20G(k,1) = gambar20G(i,j);
vgambar20B(k,1) = gambar20B(i,j);
mat_gambar20 = [vgambar20R, vgambar20G, vgambar20B];
vgambar21R(k,1) = gambar21R(i,j);
vgambar21G(k,1) = gambar21G(i,j);
vgambar21B(k,1) = gambar21B(i,j);
mat_gambar21 = [vgambar21R, vgambar21G, vgambar21B];
vgambar22R(k,1) = gambar22R(i,j);
vgambar22G(k,1) = gambar22G(i,j);
vgambar22B(k,1) = gambar22B(i,j);
mat_gambar22 = [vgambar22R, vgambar22G, vgambar22B];
vgambar23R(k,1) = gambar23R(i,j);
vgambar23G(k,1) = gambar23G(i,j);
vgambar23B(k,1) = gambar23B(i,j);
mat_gambar23 = [vgambar23R, vgambar23G, vgambar23B];
vgambar24R(k,1) = gambar24R(i,j);
vgambar24G(k,1) = gambar24G(i,j);
vgambar24B(k,1) = gambar24B(i,j);
mat_gambar24 = [vgambar24R, vgambar24G, vgambar24B];
vgambar25R(k,1) = gambar25R(i,j);
vgambar25G(k,1) = gambar25G(i,j);
vgambar25B(k,1) = gambar25B(i,j);
mat_gambar25 = [vgambar25R, vgambar25G, vgambar25B];
vgambar26R(k,1) = gambar26R(i,j);
vgambar26G(k,1) = gambar26G(i,j);
vgambar26B(k,1) = gambar26B(i,j);
mat_gambar26 = [vgambar26R, vgambar26G, vgambar26B];
vgambar27R(k,1) = gambar27R(i,j);
vgambar27G(k,1) = gambar27G(i,j);
vgambar27B(k,1) = gambar27B(i,j);
mat_gambar27 = [vgambar27R, vgambar27G, vgambar27B];
vgambar28R(k,1) = gambar28R(i,j);
vgambar28G(k,1) = gambar28G(i,j);
vgambar28B(k,1) = gambar28B(i,j);
mat_gambar28 = [vgambar28R, vgambar28G, vgambar28B];
vgambar29R(k,1) = gambar29R(i,j);
vgambar29G(k,1) = gambar29G(i,j);
vgambar29B(k,1) = gambar29B(i,j);
mat_gambar29 = [vgambar29R, vgambar29G, vgambar29B];
vgambar30R(k,1) = gambar30R(i,j);
vgambar30G(k,1) = gambar30G(i,j);
vgambar30B(k,1) = gambar30B(i,j);
mat_gambar30 = [vgambar30R, vgambar30G, vgambar30B];
k = k + 1;
end
end
% Merubah RGB ke Greyscale
GYS1 = 0.299 * mat_gambar1(:,1) + 0.587 * mat_gambar1(:,2) + 0.114 * mat_gambar1(:,3);
GYS2 = 0.299 * mat_gambar2(:,1) + 0.587 * mat_gambar2(:,2) + 0.114 * mat_gambar2(:,3);
GYS3 = 0.299 * mat_gambar3(:,1) + 0.587 * mat_gambar3(:,2) + 0.114 * mat_gambar3(:,3);
GYS4 = 0.299 * mat_gambar4(:,1) + 0.587 * mat_gambar4(:,2) + 0.114 * mat_gambar4(:,3);
GYS5 = 0.299 * mat_gambar5(:,1) + 0.587 * mat_gambar5(:,2) + 0.114 * mat_gambar5(:,3);
GYS6 = 0.299 * mat_gambar6(:,1) + 0.587 * mat_gambar6(:,2) + 0.114 * mat_gambar6(:,3);
GYS7 = 0.299 * mat_gambar7(:,1) + 0.587 * mat_gambar7(:,2) + 0.114 * mat_gambar7(:,3);
GYS8 = 0.299 * mat_gambar8(:,1) + 0.587 * mat_gambar8(:,2) + 0.114 * mat_gambar8(:,3);
GYS9 = 0.299 * mat_gambar9(:,1) + 0.587 * mat_gambar9(:,2) + 0.114 * mat_gambar9(:,3);
GYS10 = 0.299 * mat_gambar10(:,1) + 0.587 * mat_gambar10(:,2) + 0.114 * mat_gambar10(:,3);
GYS11 = 0.299 * mat_gambar11(:,1) + 0.587 * mat_gambar11(:,2) + 0.114 * mat_gambar11(:,3);
GYS12 = 0.299 * mat_gambar12(:,1) + 0.587 * mat_gambar12(:,2) + 0.114 * mat_gambar12(:,3);
GYS13 = 0.299 * mat_gambar13(:,1) + 0.587 * mat_gambar13(:,2) + 0.114 * mat_gambar13(:,3);
GYS14 = 0.299 * mat_gambar14(:,1) + 0.587 * mat_gambar14(:,2) + 0.114 * mat_gambar14(:,3);
GYS15 = 0.299 * mat_gambar15(:,1) + 0.587 * mat_gambar15(:,2) + 0.114 * mat_gambar15(:,3);
GYS16 = 0.299 * mat_gambar16(:,1) + 0.587 * mat_gambar16(:,2) + 0.114 * mat_gambar16(:,3);
GYS17 = 0.299 * mat_gambar17(:,1) + 0.587 * mat_gambar17(:,2) + 0.114 * mat_gambar17(:,3);
GYS18 = 0.299 * mat_gambar18(:,1) + 0.587 * mat_gambar18(:,2) + 0.114 * mat_gambar18(:,3);
GYS19 = 0.299 * mat_gambar19(:,1) + 0.587 * mat_gambar19(:,2) + 0.114 * mat_gambar19(:,3);
GYS20 = 0.299 * mat_gambar20(:,1) + 0.587 * mat_gambar20(:,2) + 0.114 * mat_gambar20(:,3);
GYS21 = 0.299 * mat_gambar21(:,1) + 0.587 * mat_gambar21(:,2) + 0.114 * mat_gambar21(:,3);
GYS22 = 0.299 * mat_gambar22(:,1) + 0.587 * mat_gambar22(:,2) + 0.114 * mat_gambar22(:,3);
GYS23 = 0.299 * mat_gambar23(:,1) + 0.587 * mat_gambar23(:,2) + 0.114 * mat_gambar23(:,3);
GYS24 = 0.299 * mat_gambar24(:,1) + 0.587 * mat_gambar24(:,2) + 0.114 * mat_gambar24(:,3);
GYS25 = 0.299 * mat_gambar25(:,1) + 0.587 * mat_gambar25(:,2) + 0.114 * mat_gambar25(:,3);
GYS26 = 0.299 * mat_gambar26(:,1) + 0.587 * mat_gambar26(:,2) + 0.114 * mat_gambar26(:,3);
GYS27 = 0.299 * mat_gambar27(:,1) + 0.587 * mat_gambar27(:,2) + 0.114 * mat_gambar27(:,3);
GYS28 = 0.299 * mat_gambar28(:,1) + 0.587 * mat_gambar28(:,2) + 0.114 * mat_gambar28(:,3);
GYS29 = 0.299 * mat_gambar29(:,1) + 0.587 * mat_gambar29(:,2) + 0.114 * mat_gambar29(:,3);
GYS30 = 0.299 * mat_gambar30(:,1) + 0.587 * mat_gambar30(:,2) + 0.114 * mat_gambar30(:,3);
GYS1 = GYS1';
GYS2 = GYS2';
GYS3 = GYS3';
GYS4 = GYS4';
GYS5 = GYS5';
GYS6 = GYS6';
GYS7 = GYS7';
GYS8 = GYS8';
GYS9 = GYS9';
GYS10 = GYS10';
GYS11 = GYS11';
GYS12 = GYS12';
GYS13 = GYS13';
GYS14 = GYS14';
GYS15 = GYS15';
GYS16 = GYS16';
GYS17 = GYS17';
GYS18 = GYS18';
GYS19 = GYS19';
GYS20 = GYS20';
GYS21 = GYS21';
GYS22 = GYS22';
GYS23 = GYS23';
GYS24 = GYS24';
GYS25 = GYS25';
GYS26 = GYS26';
GYS27 = GYS27';
GYS28 = GYS28';
GYS29 = GYS29';
GYS30 = GYS30';
DataGYS = [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;];
[n,p] = size(DataGYS);
Xrata = mean(DataGYS);
for i = 1:n
Z(i, :) = DataGYS(i, :) - Xrata;
end
S = cov(DataGYS);
for i = 1:n
for j = 1:p
Zr(i,j) = Z(i,j) / sqrt(S(j,j)); % Zr adalah data hasil normalisasi
end
end
Zcov = cov(Zr);
[VA, DA] = eig(Zcov);
m = 1
for i = p:-1:1
DA_urut(m,m) = DA(i,i);
VA_urut(m,:) = VA(i,:);
m = m+1;
end
for i = 1:p
eValue(i,1) = DA_urut(i,i);
end
Total = sum(eValue)
PK = 0;
komponen = 0;%jumlah komponen utama yang dipertahankan
for j = 1:p
proporsiValue(j,:) = eValue(j,1) / Total;%mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
PK = PK + proporsiValue(j, :);
komponen = komponen + 1;
end
end
VA_Komponen = VA_urut(:,1:komponen)
W = Z * VA_Komponen
plot3(W(:,1), W(:,2), W(:,3), '*')
grid on
1 | clear; clc; |
2 | |
3 | % Load image (Type : UINT8) |
4 | gambar1 = imread('Banana/1.jpg'); |
5 | gambar2 = imread('Banana/2.jpg'); |
6 | gambar3 = imread('Banana/3.jpg'); |
7 | gambar4 = imread('Banana/4.jpg'); |
8 | gambar5 = imread('Banana/5.jpg'); |
9 | gambar6 = imread('Banana/6.jpg'); |
10 | gambar7 = imread('Banana/7.jpg'); |
11 | gambar8 = imread('Banana/8.jpg'); |
12 | gambar9 = imread('Banana/9.jpg'); |
13 | gambar10 = imread('Banana/10.jpg'); |
14 | gambar11 = imread('Kiwi/1.jpg'); |
15 | gambar12 = imread('Kiwi/2.jpg'); |
16 | gambar13 = imread('Kiwi/3.jpg'); |
17 | gambar14 = imread('Kiwi/4.jpg'); |
18 | gambar15 = imread('Kiwi/5.jpg'); |
19 | gambar16 = imread('Kiwi/6.jpg'); |
20 | gambar17 = imread('Kiwi/7.jpg'); |
21 | gambar18 = imread('Kiwi/8.jpg'); |
22 | gambar19 = imread('Kiwi/9.jpg'); |
23 | gambar20 = imread('Kiwi/10.jpg'); |
24 | gambar21 = imread('Orange/1.jpg'); |
25 | gambar22 = imread('Orange/2.jpg'); |
26 | gambar23 = imread('Orange/3.jpg'); |
27 | gambar24 = imread('Orange/4.jpg'); |
28 | gambar25 = imread('Orange/5.jpg'); |
29 | gambar26 = imread('Orange/6.jpg'); |
30 | gambar27 = imread('Orange/7.jpg'); |
31 | gambar28 = imread('Orange/8.jpg'); |
32 | gambar29 = imread('Orange/9.jpg'); |
33 | gambar30 = imread('Orange/10.jpg'); |
34 | |
35 | % Konversi UINT8 menjadi double agar dapat di operasikan komputasinya |
36 | gambar1 = double(gambar1); |
37 | gambar2 = double(gambar2); |
38 | gambar3 = double(gambar3); |
39 | gambar4 = double(gambar4); |
40 | gambar5 = double(gambar5); |
41 | gambar6 = double(gambar6); |
42 | gambar7 = double(gambar7); |
43 | gambar8 = double(gambar8); |
44 | gambar9 = double(gambar9); |
45 | gambar10 = double(gambar10); |
46 | gambar11 = double(gambar11); |
47 | gambar12 = double(gambar12); |
48 | gambar13 = double(gambar13); |
49 | gambar14 = double(gambar14); |
50 | gambar15 = double(gambar15); |
51 | gambar16 = double(gambar16); |
52 | gambar17 = double(gambar17); |
53 | gambar18 = double(gambar18); |
54 | gambar19 = double(gambar19); |
55 | gambar20 = double(gambar20); |
56 | gambar21 = double(gambar21); |
57 | gambar22 = double(gambar22); |
58 | gambar23 = double(gambar23); |
59 | gambar24 = double(gambar24); |
60 | gambar25 = double(gambar25); |
61 | gambar26 = double(gambar26); |
62 | gambar27 = double(gambar27); |
63 | gambar28 = double(gambar28); |
64 | gambar29 = double(gambar29); |
65 | gambar30 = double(gambar30); |
66 | |
67 | % 1,2,3 itu layer, RGB itu warnanya |
68 | gambar1R = gambar1(:,:,1); % layer 1 |
69 | gambar1G = gambar1(:,:,2); % layer 2 |
70 | gambar1B = gambar1(:,:,3); % layer 3 |
71 | |
72 | gambar2R = gambar2(:,:,1); |
73 | gambar2G = gambar2(:,:,2); |
74 | gambar2B = gambar2(:,:,3); |
75 | |
76 | gambar3R = gambar3(:,:,1); |
77 | gambar3G = gambar3(:,:,2); |
78 | gambar3B = gambar3(:,:,3); |
79 | |
80 | gambar4R = gambar4(:,:,1); |
81 | gambar4G = gambar4(:,:,2); |
82 | gambar4B = gambar4(:,:,3); |
83 | |
84 | gambar5R = gambar5(:,:,1); |
85 | gambar5G = gambar5(:,:,2); |
86 | gambar5B = gambar5(:,:,3); |
87 | |
88 | gambar6R = gambar6(:,:,1); |
89 | gambar6G = gambar6(:,:,2); |
90 | gambar6B = gambar6(:,:,3); |
91 | |
92 | gambar7R = gambar7(:,:,1); |
93 | gambar7G = gambar7(:,:,2); |
94 | gambar7B = gambar7(:,:,3); |
95 | |
96 | gambar8R = gambar8(:,:,1); |
97 | gambar8G = gambar8(:,:,2); |
98 | gambar8B = gambar8(:,:,3); |
99 | |
100 | gambar9R = gambar9(:,:,1); |
101 | gambar9G = gambar9(:,:,2); |
102 | gambar9B = gambar9(:,:,3); |
103 | |
104 | gambar10R = gambar10(:,:,1); |
105 | gambar10G = gambar10(:,:,2); |
106 | gambar10B = gambar10(:,:,3); |
107 | |
108 | gambar11R = gambar11(:,:,1); |
109 | gambar11G = gambar11(:,:,2); |
110 | gambar11B = gambar11(:,:,3); |
111 | |
112 | gambar12R = gambar12(:,:,1); |
113 | gambar12G = gambar12(:,:,2); |
114 | gambar12B = gambar12(:,:,3); |
115 | |
116 | gambar13R = gambar13(:,:,1); |
117 | gambar13G = gambar13(:,:,2); |
118 | gambar13B = gambar13(:,:,3); |
119 | |
120 | gambar14R = gambar14(:,:,1); |
121 | gambar14G = gambar14(:,:,2); |
122 | gambar14B = gambar14(:,:,3); |
123 | |
124 | gambar15R = gambar15(:,:,1); |
125 | gambar15G = gambar15(:,:,2); |
126 | gambar15B = gambar15(:,:,3); |
127 | |
128 | gambar16R = gambar16(:,:,1); |
129 | gambar16G = gambar16(:,:,2); |
130 | gambar16B = gambar16(:,:,3); |
131 | |
132 | gambar17R = gambar17(:,:,1); |
133 | gambar17G = gambar17(:,:,2); |
134 | gambar17B = gambar17(:,:,3); |
135 | |
136 | gambar18R = gambar18(:,:,1); |
137 | gambar18G = gambar18(:,:,2); |
138 | gambar18B = gambar18(:,:,3); |
139 | |
140 | gambar19R = gambar19(:,:,1); |
141 | gambar19G = gambar19(:,:,2); |
142 | gambar19B = gambar19(:,:,3); |
143 | |
144 | gambar20R = gambar20(:,:,1); |
145 | gambar20G = gambar20(:,:,2); |
146 | gambar20B = gambar20(:,:,3); |
147 | |
148 | gambar21R = gambar21(:,:,1); |
149 | gambar21G = gambar21(:,:,2); |
150 | gambar21B = gambar21(:,:,3); |
151 | |
152 | gambar22R = gambar22(:,:,1); |
153 | gambar22G = gambar22(:,:,2); |
154 | gambar22B = gambar22(:,:,3); |
155 | |
156 | gambar23R = gambar23(:,:,1); |
157 | gambar23G = gambar23(:,:,2); |
158 | gambar23B = gambar23(:,:,3); |
159 | |
160 | gambar24R = gambar24(:,:,1); |
161 | gambar24G = gambar24(:,:,2); |
162 | gambar24B = gambar24(:,:,3); |
163 | |
164 | gambar25R = gambar25(:,:,1); |
165 | gambar25G = gambar25(:,:,2); |
166 | gambar25B = gambar25(:,:,3); |
167 | |
168 | gambar26R = gambar26(:,:,1); |
169 | gambar26G = gambar26(:,:,2); |
170 | gambar26B = gambar26(:,:,3); |
171 | |
172 | gambar27R = gambar27(:,:,1); |
173 | gambar27G = gambar27(:,:,2); |
174 | gambar27B = gambar27(:,:,3); |
175 | |
176 | gambar28R = gambar28(:,:,1); |
177 | gambar28G = gambar28(:,:,2); |
178 | gambar28B = gambar28(:,:,3); |
179 | |
180 | gambar29R = gambar29(:,:,1); |
181 | gambar29G = gambar29(:,:,2); |
182 | gambar29B = gambar29(:,:,3); |
183 | |
184 | gambar30R = gambar30(:,:,1); |
185 | gambar30G = gambar30(:,:,2); |
186 | gambar30B = gambar30(:,:,3); |
187 | |
188 | [n,p] = size(gambar1R); |
189 | |
190 | k = 1; |
191 | for 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 |
365 | end |
366 | |
367 | % Merubah RGB ke Greyscale |
368 | GYS1 = 0.299 * mat_gambar1(:,1) + 0.587 * mat_gambar1(:,2) + 0.114 * mat_gambar1(:,3); |
369 | GYS2 = 0.299 * mat_gambar2(:,1) + 0.587 * mat_gambar2(:,2) + 0.114 * mat_gambar2(:,3); |
370 | GYS3 = 0.299 * mat_gambar3(:,1) + 0.587 * mat_gambar3(:,2) + 0.114 * mat_gambar3(:,3); |
371 | GYS4 = 0.299 * mat_gambar4(:,1) + 0.587 * mat_gambar4(:,2) + 0.114 * mat_gambar4(:,3); |
372 | GYS5 = 0.299 * mat_gambar5(:,1) + 0.587 * mat_gambar5(:,2) + 0.114 * mat_gambar5(:,3); |
373 | GYS6 = 0.299 * mat_gambar6(:,1) + 0.587 * mat_gambar6(:,2) + 0.114 * mat_gambar6(:,3); |
374 | GYS7 = 0.299 * mat_gambar7(:,1) + 0.587 * mat_gambar7(:,2) + 0.114 * mat_gambar7(:,3); |
375 | GYS8 = 0.299 * mat_gambar8(:,1) + 0.587 * mat_gambar8(:,2) + 0.114 * mat_gambar8(:,3); |
376 | GYS9 = 0.299 * mat_gambar9(:,1) + 0.587 * mat_gambar9(:,2) + 0.114 * mat_gambar9(:,3); |
377 | GYS10 = 0.299 * mat_gambar10(:,1) + 0.587 * mat_gambar10(:,2) + 0.114 * mat_gambar10(:,3); |
378 | GYS11 = 0.299 * mat_gambar11(:,1) + 0.587 * mat_gambar11(:,2) + 0.114 * mat_gambar11(:,3); |
379 | GYS12 = 0.299 * mat_gambar12(:,1) + 0.587 * mat_gambar12(:,2) + 0.114 * mat_gambar12(:,3); |
380 | GYS13 = 0.299 * mat_gambar13(:,1) + 0.587 * mat_gambar13(:,2) + 0.114 * mat_gambar13(:,3); |
381 | GYS14 = 0.299 * mat_gambar14(:,1) + 0.587 * mat_gambar14(:,2) + 0.114 * mat_gambar14(:,3); |
382 | GYS15 = 0.299 * mat_gambar15(:,1) + 0.587 * mat_gambar15(:,2) + 0.114 * mat_gambar15(:,3); |
383 | GYS16 = 0.299 * mat_gambar16(:,1) + 0.587 * mat_gambar16(:,2) + 0.114 * mat_gambar16(:,3); |
384 | GYS17 = 0.299 * mat_gambar17(:,1) + 0.587 * mat_gambar17(:,2) + 0.114 * mat_gambar17(:,3); |
385 | GYS18 = 0.299 * mat_gambar18(:,1) + 0.587 * mat_gambar18(:,2) + 0.114 * mat_gambar18(:,3); |
386 | GYS19 = 0.299 * mat_gambar19(:,1) + 0.587 * mat_gambar19(:,2) + 0.114 * mat_gambar19(:,3); |
387 | GYS20 = 0.299 * mat_gambar20(:,1) + 0.587 * mat_gambar20(:,2) + 0.114 * mat_gambar20(:,3); |
388 | GYS21 = 0.299 * mat_gambar21(:,1) + 0.587 * mat_gambar21(:,2) + 0.114 * mat_gambar21(:,3); |
389 | GYS22 = 0.299 * mat_gambar22(:,1) + 0.587 * mat_gambar22(:,2) + 0.114 * mat_gambar22(:,3); |
390 | GYS23 = 0.299 * mat_gambar23(:,1) + 0.587 * mat_gambar23(:,2) + 0.114 * mat_gambar23(:,3); |
391 | GYS24 = 0.299 * mat_gambar24(:,1) + 0.587 * mat_gambar24(:,2) + 0.114 * mat_gambar24(:,3); |
392 | GYS25 = 0.299 * mat_gambar25(:,1) + 0.587 * mat_gambar25(:,2) + 0.114 * mat_gambar25(:,3); |
393 | GYS26 = 0.299 * mat_gambar26(:,1) + 0.587 * mat_gambar26(:,2) + 0.114 * mat_gambar26(:,3); |
394 | GYS27 = 0.299 * mat_gambar27(:,1) + 0.587 * mat_gambar27(:,2) + 0.114 * mat_gambar27(:,3); |
395 | GYS28 = 0.299 * mat_gambar28(:,1) + 0.587 * mat_gambar28(:,2) + 0.114 * mat_gambar28(:,3); |
396 | GYS29 = 0.299 * mat_gambar29(:,1) + 0.587 * mat_gambar29(:,2) + 0.114 * mat_gambar29(:,3); |
397 | GYS30 = 0.299 * mat_gambar30(:,1) + 0.587 * mat_gambar30(:,2) + 0.114 * mat_gambar30(:,3); |
398 | |
399 | GYS1 = GYS1'; |
400 | GYS2 = GYS2'; |
401 | GYS3 = GYS3'; |
402 | GYS4 = GYS4'; |
403 | GYS5 = GYS5'; |
404 | GYS6 = GYS6'; |
405 | GYS7 = GYS7'; |
406 | GYS8 = GYS8'; |
407 | GYS9 = GYS9'; |
408 | GYS10 = GYS10'; |
409 | GYS11 = GYS11'; |
410 | GYS12 = GYS12'; |
411 | GYS13 = GYS13'; |
412 | GYS14 = GYS14'; |
413 | GYS15 = GYS15'; |
414 | GYS16 = GYS16'; |
415 | GYS17 = GYS17'; |
416 | GYS18 = GYS18'; |
417 | GYS19 = GYS19'; |
418 | GYS20 = GYS20'; |
419 | GYS21 = GYS21'; |
420 | GYS22 = GYS22'; |
421 | GYS23 = GYS23'; |
422 | GYS24 = GYS24'; |
423 | GYS25 = GYS25'; |
424 | GYS26 = GYS26'; |
425 | GYS27 = GYS27'; |
426 | GYS28 = GYS28'; |
427 | GYS29 = GYS29'; |
428 | GYS30 = GYS30'; |
429 | |
430 | DataGYS = [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); |
433 | Xrata = mean(DataGYS); |
434 | |
435 | for i = 1:n |
436 | Z(i, :) = DataGYS(i, :) - Xrata; |
437 | end |
438 | |
439 | S = cov(DataGYS); |
440 | |
441 | for 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 |
445 | end |
446 | |
447 | Zcov = cov(Zr); |
448 | |
449 | [VA, DA] = eig(Zcov); |
450 | |
451 | m = 1 |
452 | for i = p:-1:1 |
453 | DA_urut(m,m) = DA(i,i); |
454 | VA_urut(m,:) = VA(i,:); |
455 | m = m+1; |
456 | end |
457 | |
458 | for i = 1:p |
459 | eValue(i,1) = DA_urut(i,i); |
460 | end |
461 | |
462 | Total = sum(eValue) |
463 | |
464 | PK = 0; |
465 | komponen = 0;%jumlah komponen utama yang dipertahankan |
466 | |
467 | for 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 |
473 | end |
474 | |
475 | VA_Komponen = VA_urut(:,1:komponen) |
476 | |
477 | W = Z * VA_Komponen |
478 | |
479 | plot3(W(:,1), W(:,2), W(:,3), '*') |
480 | |
481 | grid on |
2.m
· 4.9 KiB · Mathematica
Ham
clear; clc;
% Get Data
gbr1 = imread('images/Banana/1.jpg');
gbr2 = imread('images/Banana/2.jpg');
gbr3 = imread('images/Banana/3.jpg');
gbr4 = imread('images/Banana/4.jpg');
gbr5 = imread('images/Banana/5.jpg');
gbr6 = imread('images/Banana/6.jpg');
gbr7 = imread('images/Banana/7.jpg');
gbr8 = imread('images/Banana/8.jpg');
gbr9 = imread('images/Banana/9.jpg');
gbr10 = imread('images/Banana/10.jpg');
gbr11 = imread('images/Kiwi/1.jpg');
gbr12 = imread('images/Kiwi/2.jpg');
gbr13 = imread('images/Kiwi/3.jpg');
gbr14 = imread('images/Kiwi/4.jpg');
gbr15 = imread('images/Kiwi/5.jpg');
gbr16 = imread('images/Kiwi/6.jpg');
gbr17 = imread('images/Kiwi/7.jpg');
gbr18 = imread('images/Kiwi/8.jpg');
gbr19 = imread('images/Kiwi/9.jpg');
gbr20 = imread('images/Kiwi/10.jpg');
gbr21 = imread('images/Orange/1.jpg');
gbr22 = imread('images/Orange/2.jpg');
gbr23 = imread('images/Orange/3.jpg');
gbr24 = imread('images/Orange/4.jpg');
gbr25 = imread('images/Orange/5.jpg');
gbr26 = imread('images/Orange/6.jpg');
gbr27 = imread('images/Orange/7.jpg');
gbr28 = imread('images/Orange/8.jpg');
gbr29 = imread('images/Orange/9.jpg');
gbr30 = imread('images/Orange/10.jpg');
gbr1=double(rgb2gray(gbr1));
gbr2=double(rgb2gray(gbr2));
gbr3=double(rgb2gray(gbr3));
gbr4=double(rgb2gray(gbr4));
gbr5=double(rgb2gray(gbr5));
gbr6=double(rgb2gray(gbr6));
gbr7=double(rgb2gray(gbr7));
gbr8=double(rgb2gray(gbr8));
gbr9=double(rgb2gray(gbr9));
gbr10=double(rgb2gray(gbr10));
gbr11=double(rgb2gray(gbr11));
gbr12=double(rgb2gray(gbr12));
gbr13=double(rgb2gray(gbr13));
gbr14=double(rgb2gray(gbr14));
gbr15=double(rgb2gray(gbr15));
gbr16=double(rgb2gray(gbr16));
gbr17=double(rgb2gray(gbr17));
gbr18=double(rgb2gray(gbr18));
gbr19=double(rgb2gray(gbr19));
gbr20=double(rgb2gray(gbr20));
gbr21=double(rgb2gray(gbr21));
gbr22=double(rgb2gray(gbr22));
gbr23=double(rgb2gray(gbr23));
gbr24=double(rgb2gray(gbr24));
gbr25=double(rgb2gray(gbr25));
gbr26=double(rgb2gray(gbr26));
gbr27=double(rgb2gray(gbr27));
gbr28=double(rgb2gray(gbr28));
gbr29=double(rgb2gray(gbr29));
gbr30=double(rgb2gray(gbr30));
gbr1=reshape(gbr1',1,[]);
gbr2=reshape(gbr2',1,[]);
gbr3=reshape(gbr3',1,[]);
gbr4=reshape(gbr4',1,[]);
gbr5=reshape(gbr5',1,[]);
gbr6=reshape(gbr6',1,[]);
gbr7=reshape(gbr7',1,[]);
gbr8=reshape(gbr8',1,[]);
gbr9=reshape(gbr9',1,[]);
gbr10=reshape(gbr10',1,[]);
gbr11=reshape(gbr11',1,[]);
gbr12=reshape(gbr12',1,[]);
gbr13=reshape(gbr13',1,[]);
gbr14=reshape(gbr14',1,[]);
gbr15=reshape(gbr15',1,[]);
gbr16=reshape(gbr16',1,[]);
gbr17=reshape(gbr17',1,[]);
gbr18=reshape(gbr18',1,[]);
gbr19=reshape(gbr19',1,[]);
gbr20=reshape(gbr20',1,[]);
gbr21=reshape(gbr21',1,[]);
gbr22=reshape(gbr22',1,[]);
gbr23=reshape(gbr23',1,[]);
gbr24=reshape(gbr24',1,[]);
gbr25=reshape(gbr25',1,[]);
gbr26=reshape(gbr26',1,[]);
gbr27=reshape(gbr27',1,[]);
gbr28=reshape(gbr28',1,[]);
gbr29=reshape(gbr29',1,[]);
gbr30=reshape(gbr30',1,[]);
X = [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];
[n,p] = size(X);
Xrata = mean(X, 1);
%1. mengurangi setiap variable dengan rata2nya (normalisasi data)
Z = zeros(size(X));
for i = 1:n
Z(i, :) = X(i, :)-Xrata;
end
%Menghitung covariance matrix
S = cov(X); % varian
%Standardization Data
Zr = zeros(size(Z));
for i = 1:n
for j = 1:p
Zr(i,j) = Z(i,j) / sqrt(S(j,j));
end
end
% Zr ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL)
%2. Menghitung cov dari data yang sudah direduksi
Zcov = cov(Zr); % matrix covarian dari variable hasil normalisasi Zr
%3. Menghitung eigen value dan eigen vector dari Zcov
[VA, DA] = eig(Zcov); % VA = Eigen Vector ; DA = Eigen Value
%4. menghitung komponen utama
% Hasil library matlab eig() --> hasil = diagonal utamanya urut dari kecil ke besar
% yang diperlukan dari besar ke kecil
%mengurutkan hasil eigen (DESC)
m = 1;
for i = p:-1:1
DA_urut(m,m) = DA(i,i);
VA_urut(m,:) = VA(:,i)';
m = m+1;
end
%menghitung komponen utama
eValue = zeros(p, 1);
for i = 1:p
eValue(i,1) = DA_urut(i,i);
end
Total = sum(eValue); % total varians dalam data asli yang dapat dijelaskan oleh seluruh komponen utama.
% Proporsi kumulatif = mengambil data seberapa persen secara kumulatif yang
% dianggap perlu
PK = 0;
komponen = 0; % jumlah komponen utama yang dipertahankan
proporsiValue = zeros(p, 1);
for j = 1:p
proporsiValue(j) = eValue(j,1) / Total; % mengukur seberapa besar kontribusi eigenvalue ke-j terhadap varians total data.
if(PK < 0.9) % Nilai PK tergantung peneliti --> 0.8
PK = PK + proporsiValue(j);
komponen = komponen + 1;
end
end
% Mengambil eigen vector dari komponen yang terpilih
VA_Komponen = VA_urut(:,1:komponen);
% W = variable komponen utama / principal component variable
W = Zr * VA_Komponen; % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF
plot3(W(:,1), W(:,2), W(:,3), '*'); % PK < 0.9
%plot(W(:,1), W(:,2), '*'); % PK < 0.8
grid on
1 | clear; clc; |
2 | |
3 | % Get Data |
4 | gbr1 = imread('images/Banana/1.jpg'); |
5 | gbr2 = imread('images/Banana/2.jpg'); |
6 | gbr3 = imread('images/Banana/3.jpg'); |
7 | gbr4 = imread('images/Banana/4.jpg'); |
8 | gbr5 = imread('images/Banana/5.jpg'); |
9 | gbr6 = imread('images/Banana/6.jpg'); |
10 | gbr7 = imread('images/Banana/7.jpg'); |
11 | gbr8 = imread('images/Banana/8.jpg'); |
12 | gbr9 = imread('images/Banana/9.jpg'); |
13 | gbr10 = imread('images/Banana/10.jpg'); |
14 | gbr11 = imread('images/Kiwi/1.jpg'); |
15 | gbr12 = imread('images/Kiwi/2.jpg'); |
16 | gbr13 = imread('images/Kiwi/3.jpg'); |
17 | gbr14 = imread('images/Kiwi/4.jpg'); |
18 | gbr15 = imread('images/Kiwi/5.jpg'); |
19 | gbr16 = imread('images/Kiwi/6.jpg'); |
20 | gbr17 = imread('images/Kiwi/7.jpg'); |
21 | gbr18 = imread('images/Kiwi/8.jpg'); |
22 | gbr19 = imread('images/Kiwi/9.jpg'); |
23 | gbr20 = imread('images/Kiwi/10.jpg'); |
24 | gbr21 = imread('images/Orange/1.jpg'); |
25 | gbr22 = imread('images/Orange/2.jpg'); |
26 | gbr23 = imread('images/Orange/3.jpg'); |
27 | gbr24 = imread('images/Orange/4.jpg'); |
28 | gbr25 = imread('images/Orange/5.jpg'); |
29 | gbr26 = imread('images/Orange/6.jpg'); |
30 | gbr27 = imread('images/Orange/7.jpg'); |
31 | gbr28 = imread('images/Orange/8.jpg'); |
32 | gbr29 = imread('images/Orange/9.jpg'); |
33 | gbr30 = imread('images/Orange/10.jpg'); |
34 | gbr1=double(rgb2gray(gbr1)); |
35 | gbr2=double(rgb2gray(gbr2)); |
36 | gbr3=double(rgb2gray(gbr3)); |
37 | gbr4=double(rgb2gray(gbr4)); |
38 | gbr5=double(rgb2gray(gbr5)); |
39 | gbr6=double(rgb2gray(gbr6)); |
40 | gbr7=double(rgb2gray(gbr7)); |
41 | gbr8=double(rgb2gray(gbr8)); |
42 | gbr9=double(rgb2gray(gbr9)); |
43 | gbr10=double(rgb2gray(gbr10)); |
44 | gbr11=double(rgb2gray(gbr11)); |
45 | gbr12=double(rgb2gray(gbr12)); |
46 | gbr13=double(rgb2gray(gbr13)); |
47 | gbr14=double(rgb2gray(gbr14)); |
48 | gbr15=double(rgb2gray(gbr15)); |
49 | gbr16=double(rgb2gray(gbr16)); |
50 | gbr17=double(rgb2gray(gbr17)); |
51 | gbr18=double(rgb2gray(gbr18)); |
52 | gbr19=double(rgb2gray(gbr19)); |
53 | gbr20=double(rgb2gray(gbr20)); |
54 | gbr21=double(rgb2gray(gbr21)); |
55 | gbr22=double(rgb2gray(gbr22)); |
56 | gbr23=double(rgb2gray(gbr23)); |
57 | gbr24=double(rgb2gray(gbr24)); |
58 | gbr25=double(rgb2gray(gbr25)); |
59 | gbr26=double(rgb2gray(gbr26)); |
60 | gbr27=double(rgb2gray(gbr27)); |
61 | gbr28=double(rgb2gray(gbr28)); |
62 | gbr29=double(rgb2gray(gbr29)); |
63 | gbr30=double(rgb2gray(gbr30)); |
64 | |
65 | gbr1=reshape(gbr1',1,[]); |
66 | gbr2=reshape(gbr2',1,[]); |
67 | gbr3=reshape(gbr3',1,[]); |
68 | gbr4=reshape(gbr4',1,[]); |
69 | gbr5=reshape(gbr5',1,[]); |
70 | gbr6=reshape(gbr6',1,[]); |
71 | gbr7=reshape(gbr7',1,[]); |
72 | gbr8=reshape(gbr8',1,[]); |
73 | gbr9=reshape(gbr9',1,[]); |
74 | gbr10=reshape(gbr10',1,[]); |
75 | gbr11=reshape(gbr11',1,[]); |
76 | gbr12=reshape(gbr12',1,[]); |
77 | gbr13=reshape(gbr13',1,[]); |
78 | gbr14=reshape(gbr14',1,[]); |
79 | gbr15=reshape(gbr15',1,[]); |
80 | gbr16=reshape(gbr16',1,[]); |
81 | gbr17=reshape(gbr17',1,[]); |
82 | gbr18=reshape(gbr18',1,[]); |
83 | gbr19=reshape(gbr19',1,[]); |
84 | gbr20=reshape(gbr20',1,[]); |
85 | gbr21=reshape(gbr21',1,[]); |
86 | gbr22=reshape(gbr22',1,[]); |
87 | gbr23=reshape(gbr23',1,[]); |
88 | gbr24=reshape(gbr24',1,[]); |
89 | gbr25=reshape(gbr25',1,[]); |
90 | gbr26=reshape(gbr26',1,[]); |
91 | gbr27=reshape(gbr27',1,[]); |
92 | gbr28=reshape(gbr28',1,[]); |
93 | gbr29=reshape(gbr29',1,[]); |
94 | gbr30=reshape(gbr30',1,[]); |
95 | |
96 | X = [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); |
98 | Xrata = mean(X, 1); |
99 | |
100 | %1. mengurangi setiap variable dengan rata2nya (normalisasi data) |
101 | Z = zeros(size(X)); |
102 | for i = 1:n |
103 | Z(i, :) = X(i, :)-Xrata; |
104 | end |
105 | |
106 | %Menghitung covariance matrix |
107 | S = cov(X); % varian |
108 | |
109 | %Standardization Data |
110 | Zr = zeros(size(Z)); |
111 | for i = 1:n |
112 | for j = 1:p |
113 | Zr(i,j) = Z(i,j) / sqrt(S(j,j)); |
114 | end |
115 | end |
116 | % Zr ADALAH HASIL DARI NORMALISASI DATA X (DATA AWAL) |
117 | |
118 | %2. Menghitung cov dari data yang sudah direduksi |
119 | Zcov = 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) |
128 | m = 1; |
129 | for i = p:-1:1 |
130 | DA_urut(m,m) = DA(i,i); |
131 | VA_urut(m,:) = VA(:,i)'; |
132 | m = m+1; |
133 | end |
134 | |
135 | %menghitung komponen utama |
136 | eValue = zeros(p, 1); |
137 | for i = 1:p |
138 | eValue(i,1) = DA_urut(i,i); |
139 | end |
140 | |
141 | Total = 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 |
145 | PK = 0; |
146 | komponen = 0; % jumlah komponen utama yang dipertahankan |
147 | |
148 | proporsiValue = zeros(p, 1); |
149 | for 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 |
155 | end |
156 | |
157 | % Mengambil eigen vector dari komponen yang terpilih |
158 | VA_Komponen = VA_urut(:,1:komponen); |
159 | % W = variable komponen utama / principal component variable |
160 | W = Zr * VA_Komponen; % W ADALAH FINAL DATA YANG DISEBUTKAN DI PDF |
161 | |
162 | plot3(W(:,1), W(:,2), W(:,3), '*'); % PK < 0.9 |
163 | %plot(W(:,1), W(:,2), '*'); % PK < 0.8 |
164 | grid on |