Deteksi Kematangan Buah Sawit Menggunakan Self-Organizing Maps (SOM)


Self Organizing Maps (SOM) merupakan suatu metode Jaringan Saraf Tiruan yang diperkenalkan pertama kali oleh Teuvo Kohonen tahun 1981, sehingga sering disebut dengan Jaringan Kohonen. Dinamakan “Self Organizing” karena tidak memerlukan pengawasan/ tak terawasi (unsupervised learning) dan disebut “Maps” karena berusaha memetakan bobotnya agar sesuai dengan input data yang diberikan. Neuron-neuron pada jaringan ini menyusun dirinya sendiri berdasarkan nilai input tertentu dalam suatu kelompok, biasa disebut cluster. Selama proses penyusunan diri, cluster dengan vektor bobot paling cocok dengan pola bobot (jarak paling dekat) akan terpilih sebagai pemenang. Neuron pemenang beserta neuron-neuron tetangga akan memperbaiki bobotnya masing-masing.

Arsitektur Self Organizing Map (SOM)

Berikut ini merupakan contoh pemrograman MATLAB untuk mendeteksi kematangan buah sawit menggunakan Self Organizing Map (SOM). Langkah-langkahnya adalah sebagai berikut:

1. Mempersiapkan data latih

–>

Citra Buah Sawit Dalam Kelas “Matang”
Citra Buah Sawit Dalam Kelas “Mentah”

2. Melakukan tahapan pelatihan yang meliputi:

  • Membaca citra RGB
  • Mengkonversi citra RGB menjadi citra grayscale
  • Mengkonversi citra grayscale menjadi citra biner
  • Melakukan operasi morfologi untuk menyempurnakan hasil segmentasi
  • Melakukan ekstraksi ciri warna RGB dan HSV
  • Melakukan ekstraksi ciri tekstur GLCM
  • Melakukan seleksi ciri menggunakan information gain
  • Membangun arsitektur SOM
  • Melakukan training
  • Membaca nilai keluaran pelatihan
  • Menghitung nilai akurasi pelatihan

Source code yang digunakan untuk tahapan pelatihan adalah sebagai berikut:

% Adi Pamungkas, S.Si, M.Si
% Website: https://pemrogramanmatlab.com/
% Email  : pemrogramanmatlab@gmail.com

clc; clear; close all;

%%% Matang
% membaca file citra
nama_folder = 'Data Latih/Matang';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_matang
ciri_matang = zeros(jumlah_file,10);
target_matang = zeros(jumlah_file,1);

for n = 1:jumlah_file
    % membaca citra RGB
    Img = im2double(imread(fullfile(nama_folder,nama_file(n).name)));
    % konversi citra RGB menjadi grayscale
    Img_gray = rgb2gray(Img);
    % konversi citra grayscale menjadi biner
    bw = imbinarize(Img_gray);
    % operasi morfologi
    bw = imcomplement(bw);
    bw = imfill(bw,'holes');
    bw = bwareaopen(bw,100);
    % ekstraksi ciri warna RGB
    R = Img(:,:,1);
    G = Img(:,:,2);
    B = Img(:,:,3);
    R(~bw) = 0;
    G(~bw) = 0;
    B(~bw) = 0;
    Red = sum(sum(R))/sum(sum(bw));
    Green = sum(sum(G))/sum(sum(bw));
    Blue = sum(sum(B))/sum(sum(bw));
    % ekstraksi ciri warna HSV
    HSV = rgb2hsv(Img);
    H = HSV(:,:,1);
    S = HSV(:,:,2);
    V = HSV(:,:,3);
    H(~bw) = 0;
    S(~bw) = 0;
    V(~bw) = 0;
    Hue = sum(sum(H))/sum(sum(bw));
    Saturation = sum(sum(S))/sum(sum(bw));
    Value = sum(sum(V))/sum(sum(bw));
    % ekstraksi ciri tekstur GLCM
    Img_gray(~bw) = 0;
    GLCM = graycomatrix(Img_gray,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    Contrast = mean(stats.Contrast);
    Correlation = mean(stats.Correlation);
    Energy = mean(stats.Energy);
    Homogeneity = mean(stats.Homogeneity);
    % mengisi hasil ekstraksi ciri pada variabel ciri_matang
    ciri_matang(n,1) = Red;
    ciri_matang(n,2) = Green;
    ciri_matang(n,3) = Blue;
    ciri_matang(n,4) = Hue;
    ciri_matang(n,5) = Saturation;
    ciri_matang(n,6) = Value;
    ciri_matang(n,7) = Contrast;
    ciri_matang(n,8) = Correlation;
    ciri_matang(n,9) = Energy;
    ciri_matang(n,10) = Homogeneity;
    % mengisi nilai target pada variabel target_matang
    target_matang(n,1) = 1;
end

%%% Mentah
% membaca file citra
nama_folder = 'Data Latih/Mentah';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_mentah
ciri_mentah = zeros(jumlah_file,10);
target_mentah = zeros(jumlah_file,1);

for n = 1:jumlah_file
    % membaca citra RGB
    Img = im2double(imread(fullfile(nama_folder,nama_file(n).name)));
    % konversi citra RGB menjadi grayscale
    Img_gray = rgb2gray(Img);
    % konversi citra grayscale menjadi biner
    bw = imbinarize(Img_gray);
    % operasi morfologi
    bw = imcomplement(bw);
    bw = imfill(bw,'holes');
    bw = bwareaopen(bw,100);
    % ekstraksi ciri warna RGB
    R = Img(:,:,1);
    G = Img(:,:,2);
    B = Img(:,:,3);
    R(~bw) = 0;
    G(~bw) = 0;
    B(~bw) = 0;
    Red = sum(sum(R))/sum(sum(bw));
    Green = sum(sum(G))/sum(sum(bw));
    Blue = sum(sum(B))/sum(sum(bw));
    % ekstraksi ciri warna HSV
    HSV = rgb2hsv(Img);
    H = HSV(:,:,1);
    S = HSV(:,:,2);
    V = HSV(:,:,3);
    H(~bw) = 0;
    S(~bw) = 0;
    V(~bw) = 0;
    Hue = sum(sum(H))/sum(sum(bw));
    Saturation = sum(sum(S))/sum(sum(bw));
    Value = sum(sum(V))/sum(sum(bw));
    % ekstraksi ciri tekstur GLCM
    Img_gray(~bw) = 0;
    GLCM = graycomatrix(Img_gray,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    Contrast = mean(stats.Contrast);
    Correlation = mean(stats.Correlation);
    Energy = mean(stats.Energy);
    Homogeneity = mean(stats.Homogeneity);
    % mengisi hasil ekstraksi ciri pada variabel ciri_mentah
    ciri_mentah(n,1) = Red;
    ciri_mentah(n,2) = Green;
    ciri_mentah(n,3) = Blue;
    ciri_mentah(n,4) = Hue;
    ciri_mentah(n,5) = Saturation;
    ciri_mentah(n,6) = Value;
    ciri_mentah(n,7) = Contrast;
    ciri_mentah(n,8) = Correlation;
    ciri_mentah(n,9) = Energy;
    ciri_mentah(n,10) = Homogeneity;
    % mengisi nilai target pada variabel target_mentah
    target_mentah(n,1) = 2;
end

% menyusun ciri_latih dan target_latih
ciri_latih = [ciri_matang;ciri_mentah];
target_latih = [target_matang;target_mentah];

% menghitung nilai gain
[gains, attorder] = computegains(array2table(ciri_latih),target_latih);

% menyimpan urutan nilai gain
save('attorder.mat','attorder')

% menampilkan grafik nilai gain
figure;
bar(attorder, gains(attorder));
ylabel('Gain');
set(gca,'XTickLabel',{'Red','Green','Blue','Hue','Saturation','Value',...
    'Contrast','Correlation','Energy','Homogeneity'})

% seleksi ciri berdasarkan 5 nilai gain tertinggi
ciri_matang_seleksi = zeros(numel(target_matang),5);
ciri_mentah_seleksi = zeros(numel(target_mentah),5);
for k = 1:5
    ciri_matang_seleksi(:,k) = ciri_matang(:,attorder(k));
    ciri_mentah_seleksi(:,k) = ciri_mentah(:,attorder(k));
end

% membuat model SOM pada masing2 kelas
net1 = selforgmap([2 2],'topologyFcn','gridtop'); % hextop,randtop,gridtop
net2 = selforgmap([2 2],'topologyFcn','gridtop'); % hextop,randtop,gridtop

% melakukan pelatihan jaringan
net1 = train(net1,ciri_matang_seleksi');
net2 = train(net2,ciri_mentah_seleksi');

% membaca nilai bobot jaringan
wtnet1 = net1.iw{1,1};
wtnet2 = net2.iw{1,1};

% menyimpan nilai bobot jaringan
save('wtnet1.mat','wtnet1');
save('wtnet2.mat','wtnet2');

% menghitung jarak masing2 kelas dengan titik pusat masing2 model SOM
pfn1 = dist(wtnet1,[ciri_matang_seleksi;ciri_mentah_seleksi]');
pfn2 = dist(wtnet2,[ciri_matang_seleksi;ciri_mentah_seleksi]');

% menghitung jarak terpendek
Group = zeros(numel(target_latih),2);
for i = 1:numel(target_latih)
    Group(i,:) = [min(pfn1(:,i)),min(pfn2(:,i))];
end

% membaca nilai keluaran hasil pelatihan
[~,Groupmin] = min(Group,[],2);

% menghitung nilai akurasi pelatihan
akurasi = (sum(target_latih==Groupmin)/numel(target_latih))*100;

% menampilkan nilai akurasi pelatihan
disp(['Akurasi Pelatihan = ',num2str(akurasi),' %'])

Berikut merupakan grafik nilai gain masing-masing ciri

Nilai Gain Masing-Masing Ciri

Tampilan proses training SOM adalah sebagai berikut

Proses Training Self Organizing Maps (SOM)

Akurasi pelatihan yang diperoleh adalah

Akurasi Pelatihan

3. Mempersiapkan data uji

Citra Buah Sawit Dalam Kelas “Matang”
Citra Buah Sawit Dalam Kelas “Mentah”

4. Melakukan tahapan pengujian yang meliputi:

  • Membaca citra RGB
  • Mengkonversi citra RGB menjadi citra grayscale
  • Mengkonversi citra grayscale menjadi citra biner
  • Melakukan operasi morfologi untuk menyempurnakan hasil segmentasi
  • Melakukan ekstraksi ciri warna RGB dan HSV
  • Melakukan ekstraksi ciri tekstur GLCM
  • Melakukan seleksi ciri menggunakan information gain
  • Memanggil arsitektur SOM hasil pelatihan
  • Melakukan testing
  • Membaca nilai keluaran pengujian
  • Menghitung nilai akurasi pengujian

Source code yang digunakan untuk tahapan pengujian adalah sebagai berikut:

% Adi Pamungkas, S.Si, M.Si
% Website: https://pemrogramanmatlab.com/
% Email  : pemrogramanmatlab@gmail.com

clc; clear; close all;

%%% Matang
% membaca file citra
nama_folder = 'Data Uji/Matang';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_matang
ciri_matang = zeros(jumlah_file,10);
target_matang = zeros(jumlah_file,1);

for n = 1:jumlah_file
    % membaca citra RGB
    Img = im2double(imread(fullfile(nama_folder,nama_file(n).name)));
    % konversi citra RGB menjadi grayscale
    Img_gray = rgb2gray(Img);
    % konversi citra grayscale menjadi biner
    bw = imbinarize(Img_gray);
    % operasi morfologi
    bw = imcomplement(bw);
    bw = imfill(bw,'holes');
    bw = bwareaopen(bw,100);
    % ekstraksi ciri warna RGB
    R = Img(:,:,1);
    G = Img(:,:,2);
    B = Img(:,:,3);
    R(~bw) = 0;
    G(~bw) = 0;
    B(~bw) = 0;
    Red = sum(sum(R))/sum(sum(bw));
    Green = sum(sum(G))/sum(sum(bw));
    Blue = sum(sum(B))/sum(sum(bw));
    % ekstraksi ciri warna HSV
    HSV = rgb2hsv(Img);
    H = HSV(:,:,1);
    S = HSV(:,:,2);
    V = HSV(:,:,3);
    H(~bw) = 0;
    S(~bw) = 0;
    V(~bw) = 0;
    Hue = sum(sum(H))/sum(sum(bw));
    Saturation = sum(sum(S))/sum(sum(bw));
    Value = sum(sum(V))/sum(sum(bw));
    % ekstraksi ciri tekstur GLCM
    Img_gray(~bw) = 0;
    GLCM = graycomatrix(Img_gray,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    Contrast = mean(stats.Contrast);
    Correlation = mean(stats.Correlation);
    Energy = mean(stats.Energy);
    Homogeneity = mean(stats.Homogeneity);
    % mengisi hasil ekstraksi ciri pada variabel ciri_matang
    ciri_matang(n,1) = Red;
    ciri_matang(n,2) = Green;
    ciri_matang(n,3) = Blue;
    ciri_matang(n,4) = Hue;
    ciri_matang(n,5) = Saturation;
    ciri_matang(n,6) = Value;
    ciri_matang(n,7) = Contrast;
    ciri_matang(n,8) = Correlation;
    ciri_matang(n,9) = Energy;
    ciri_matang(n,10) = Homogeneity;
    % mengisi nilai target pada variabel target_matang
    target_matang(n,1) = 1;
end

%%% Mentah
% membaca file citra
nama_folder = 'Data Uji/Mentah';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_mentah
ciri_mentah = zeros(jumlah_file,10);
target_mentah = zeros(jumlah_file,1);

for n = 1:jumlah_file
    % membaca citra RGB
    Img = im2double(imread(fullfile(nama_folder,nama_file(n).name)));
    % konversi citra RGB menjadi grayscale
    Img_gray = rgb2gray(Img);
    % konversi citra grayscale menjadi biner
    bw = imbinarize(Img_gray);
    % operasi morfologi
    bw = imcomplement(bw);
    bw = imfill(bw,'holes');
    bw = bwareaopen(bw,100);
    % ekstraksi ciri warna RGB
    R = Img(:,:,1);
    G = Img(:,:,2);
    B = Img(:,:,3);
    R(~bw) = 0;
    G(~bw) = 0;
    B(~bw) = 0;
    Red = sum(sum(R))/sum(sum(bw));
    Green = sum(sum(G))/sum(sum(bw));
    Blue = sum(sum(B))/sum(sum(bw));
    % ekstraksi ciri warna HSV
    HSV = rgb2hsv(Img);
    H = HSV(:,:,1);
    S = HSV(:,:,2);
    V = HSV(:,:,3);
    H(~bw) = 0;
    S(~bw) = 0;
    V(~bw) = 0;
    Hue = sum(sum(H))/sum(sum(bw));
    Saturation = sum(sum(S))/sum(sum(bw));
    Value = sum(sum(V))/sum(sum(bw));
    % ekstraksi ciri tekstur GLCM
    Img_gray(~bw) = 0;
    GLCM = graycomatrix(Img_gray,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    Contrast = mean(stats.Contrast);
    Correlation = mean(stats.Correlation);
    Energy = mean(stats.Energy);
    Homogeneity = mean(stats.Homogeneity);
    % mengisi hasil ekstraksi ciri pada variabel ciri_mentah
    ciri_mentah(n,1) = Red;
    ciri_mentah(n,2) = Green;
    ciri_mentah(n,3) = Blue;
    ciri_mentah(n,4) = Hue;
    ciri_mentah(n,5) = Saturation;
    ciri_mentah(n,6) = Value;
    ciri_mentah(n,7) = Contrast;
    ciri_mentah(n,8) = Correlation;
    ciri_mentah(n,9) = Energy;
    ciri_mentah(n,10) = Homogeneity;
    % mengisi nilai target pada variabel target_mentah
    target_mentah(n,1) = 2;
end

% menyusun target_uji
target_uji = [target_matang;target_mentah];

% memanggil variabel attorder (urutan ciri dengan nilai gain tertinggi)
load('attorder.mat')

% seleksi ciri berdasarkan 5 nilai gain tertinggi
ciri_matang_seleksi = zeros(numel(target_matang),5);
ciri_mentah_seleksi = zeros(numel(target_mentah),5);
for k = 1:5
    ciri_matang_seleksi(:,k) = ciri_matang(:,attorder(k));
    ciri_mentah_seleksi(:,k) = ciri_mentah(:,attorder(k));
end

% memanggil nilai bobot jaringan hasil pelatihan
load('wtnet1.mat');
load('wtnet2.mat');

% menghitung jarak masing2 kelas dengan titik pusat masing2 model SOM
pfn1 = dist(wtnet1,[ciri_matang_seleksi;ciri_mentah_seleksi]');
pfn2 = dist(wtnet2,[ciri_matang_seleksi;ciri_mentah_seleksi]');

% menghitung jarak terpendek
Group = zeros(numel(target_uji),2);
for i = 1:numel(target_uji)
    Group(i,:) = [min(pfn1(:,i)),min(pfn2(:,i))];
end

% membaca nilai keluaran hasil pengujian
[~,Groupmin] = min(Group,[],2);

% menghitung nilai akurasi pengujian
akurasi = (sum(target_uji==Groupmin)/numel(target_uji))*100;

% menampilkan nilai akurasi pengujian
disp(['Akurasi Pengujian = ',num2str(akurasi),' %'])

Akurasi pengujian yang diperoleh adalah

Akurasi Pengujian

5. Membuat tampilan Graphical User Interface (GUI)

Source code yang digunakan untuk membuat tampilan GUI adalah sebagai berikut

% Adi Pamungkas, S.Si, M.Si
% Website: https://pemrogramanmatlab.com/
% Email  : adipamungkas@st.fisika.undip.ac.id

function varargout = main_program(varargin)
% MAIN_PROGRAM MATLAB code for main_program.fig
%      MAIN_PROGRAM, by itself, creates a new MAIN_PROGRAM or raises the existing
%      singleton*.
%
%      H = MAIN_PROGRAM returns the handle to a new MAIN_PROGRAM or the handle to
%      the existing singleton*.
%
%      MAIN_PROGRAM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAIN_PROGRAM.M with the given input arguments.
%
%      MAIN_PROGRAM('Property','Value',...) creates a new MAIN_PROGRAM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before main_program_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to main_program_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help main_program

% Last Modified by GUIDE v2.5 01-Jan-2019 14:51:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @main_program_OpeningFcn, ...
    'gui_OutputFcn',  @main_program_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before main_program is made visible.
function main_program_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to main_program (see VARARGIN)

% Choose default command line output for main_program
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');

% UIWAIT makes main_program wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = main_program_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% menampilkan menu open file
[nama_file, nama_path] = uigetfile('*.jpg');

% jika ada file yang dipilih maka akan mengeksekusi perintah di bawahnya
if ~isequal(nama_file,0)
    % membaca file citra
    Img = im2double(imread(fullfile(nama_path, nama_file)));
    % menampilkan citra pada axes 1
    axes(handles.axes1)
    imshow(Img)
    title('Citra Asli')
    % menampilkan nama file citra pada edit1
    set(handles.edit1,'String',nama_file)
    % menyimpan variabel Img pada lokasi handles
    handles.Img = Img;
    guidata(hObject, handles)
else
    % jika tidak ada file yang dipilih maka akan kembali
    return
end



% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% memanggil variabel Img yang ada di lokasi handles
Img = handles.Img;
% konversi citra RGB menjadi grayscale
Img_gray = rgb2gray(Img);
% konversi citra grayscale menjadi biner
bw = imbinarize(Img_gray);
% operasi morfologi
bw = imcomplement(bw);
bw = imfill(bw,'holes');
bw = bwareaopen(bw,100);
% menampilkan citra biner hasil segmentasi pada axes2
axes(handles.axes2)
imshow(bw)
title('Citra biner')
% ekstraksi komponen RGB
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
% mengubah nilai background menjadi nol
R(~bw) = 0;
G(~bw) = 0;
B(~bw) = 0;
RGB = cat(3,R,G,B);
% menampilkan citra RGB hasil segmentasi pada axes3
axes(handles.axes3)
imshow(RGB)
title('Hasil Segmentasi')
% menyimpan variabel Img_gray & bw pada lokasi handles
handles.Img_gray = Img_gray;
handles.bw = bw;
guidata(hObject, handles)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% memanggil variabel Img, Img_gray, & bw yang ada di lokasi handles
Img = handles.Img;
Img_gray = handles.Img_gray;
bw = handles.bw;
% ekstraksi ciri warna RGB
R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);
R(~bw) = 0;
G(~bw) = 0;
B(~bw) = 0;
Red = sum(sum(R))/sum(sum(bw));
Green = sum(sum(G))/sum(sum(bw));
Blue = sum(sum(B))/sum(sum(bw));
% ekstraksi ciri warna HSV
HSV = rgb2hsv(Img);
H = HSV(:,:,1);
S = HSV(:,:,2);
V = HSV(:,:,3);
H(~bw) = 0;
S(~bw) = 0;
V(~bw) = 0;
Hue = sum(sum(H))/sum(sum(bw));
Saturation = sum(sum(S))/sum(sum(bw));
Value = sum(sum(V))/sum(sum(bw));
% ekstraksi ciri tekstur GLCM
Img_gray(~bw) = 0;
GLCM = graycomatrix(Img_gray,'Offset',[0 1; -1 1; -1 0; -1 -1]);
stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
Contrast = mean(stats.Contrast);
Correlation = mean(stats.Correlation);
Energy = mean(stats.Energy);
Homogeneity = mean(stats.Homogeneity);
% mengisi hasil ekstraksi ciri pada variabel ciri_sayur
ciri_sayur = cell(10,2);
ciri_sayur{1,1} = 'Red';
ciri_sayur{2,1} = 'Green';
ciri_sayur{3,1} = 'Blue';
ciri_sayur{4,1} = 'Hue';
ciri_sayur{5,1} = 'Saturation';
ciri_sayur{6,1} = 'Value';
ciri_sayur{7,1} = 'Contrast';
ciri_sayur{8,1} = 'Correlation';
ciri_sayur{9,1} = 'Energy';
ciri_sayur{10,1} = 'Homogeneity';
ciri_sayur{1,2} = num2str(Red);
ciri_sayur{2,2} = num2str(Green);
ciri_sayur{3,2} = num2str(Blue);
ciri_sayur{4,2} = num2str(Hue);
ciri_sayur{5,2} = num2str(Saturation);
ciri_sayur{6,2} = num2str(Value);
ciri_sayur{7,2} = num2str(Contrast);
ciri_sayur{8,2} = num2str(Correlation);
ciri_sayur{9,2} = num2str(Energy);
ciri_sayur{10,2} = num2str(Homogeneity);
% menampilkan ciri_sayur pada tabel
set(handles.text2,'String','Hasil Ekstraksi Ciri')
set(handles.uitable1,'Data',ciri_sayur,'RowName',1:10)
ciri_uji = [Red,Green,Blue,Hue,Saturation,Value,...
    Contrast,Correlation,Energy,Homogeneity];
% menyimpan variabel ciri_uji pada lokasi handles
handles.ciri_uji = ciri_uji;
guidata(hObject, handles)


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% memangil variabel ciri_uji pada lokasi handles
ciri_uji = handles.ciri_uji;

% memanggil variabel attorder (urutan ciri dengan nilai gain tertinggi)
load('attorder.mat')

% seleksi ciri berdasarkan 5 nilai gain tertinggi
ciri_seleksi = zeros(1,5);
for k = 1:5
    ciri_seleksi(:,k) = ciri_uji(:,attorder(k));
end

% memanggil nilai bobot jaringan hasil pelatihan
load('wtnet1.mat');
load('wtnet2.mat');

% menghitung jarak masing2 kelas dengan titik pusat masing2 model SOM
pfn1 = dist(wtnet1,ciri_seleksi');
pfn2 = dist(wtnet2,ciri_seleksi');

% menghitung jarak terpendek
Group = [min(pfn1(:,1)),min(pfn2(:,1))];

% membaca nilai keluaran hasil pengujian
[~,Groupmin] = min(Group,[],2);

% mengubah nilai keluaran menjadi kelas keluaran
switch Groupmin
    case 1
        kelas = 'Matang';
    case 2
        kelas = 'Mentah';
    otherwise
        kelas = 'Tidak Dikenali';
end

% menampilkan kelas keluaran pada edit2
set(handles.edit2,'String',kelas)

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% mereset button2
axes(handles.axes1)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

axes(handles.axes2)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

axes(handles.axes3)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

set(handles.edit1,'String',[])
set(handles.edit2,'String',[])
set(handles.text2,'String',[])
set(handles.uitable1,'Data',[])


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Tampilan GUI untuk pengolahan citra buah sawit dalam kelas “matang”

Tampilan GUI kelas “matang”
Tampilan GUI kelas “mentah”

Posted on May 8, 2021, in Pengenalan Matlab and tagged , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: