Klasifikasi Daun Dengan Metode ANFIS (Adaptive Neuro-Fuzzy Inference System)

Adaptive Neuro-Fuzzy Inference System (ANFIS) adalah penggabungan mekanisme fuzzy inference system yang 

digambarkan dalam arsitektur jaringan syaraf. Sistem inferensi fuzzy yang digunakan adalah sistem inferensi fuzzy model Tagaki-Sugeno-Kang (TSK) 

orde satu dengan pertimbangan kesederhanaan dan kemudahan komputasi.

Selengkapnya dibahas disini 

Metode anfis dapat diaplikasikan pada pemrograman MATLAB untuk mengklasifikasi citra pada daun ke dalam 4 kelompok yaitu (A, B, C, dan D) menggunakan algoritma ANFIS. Pada contoh ini digunakan 40 citra daun yang terdiri dari 10 citra pada masing-masing kelas. Citra gambar tersebut dibagi menjadi dua bagian yaitu sebanyak 28 citra untuk data pelatihan dan 12 citra untuk data pengujian. Contoh citra daun yang digunakan ditunjukkan pada gambar di bawah ini

Langkah-langkah pemrograman-nya adalah sebagai berikut:

1. Mempersiapkan citra latih untuk proses pelatihan


2. Melakukan pelatihan algoritma ANFIS untuk mengklasifikasikan citra daun.
% Abdul Haris Kuspranoto ST. MT.
% www.sinauprogramming.com
clc; clear; close all; warning off all;

image_folder = 'training data';
filenames = dir(fullfile(image_folder, '*.tif'));
total_images = numel(filenames);

feature = zeros(total_images,6);

for n = 1:total_images
    full_name = fullfile(image_folder, filenames(n).name);
    I = imread(full_name);
    Img = im2bw(I,graythresh(I));
    Img = imcomplement(Img);
    Img = imfill(Img,'holes');
    Img = bwareaopen(Img,1000);
    
    % Ekstraksi Ciri Morfologi
    stats = regionprops(Img,'All');
    area = stats.Area;
    perimeter = stats.Perimeter;
    metric = 4*pi*area/(perimeter^2);
    eccentricity = stats.Eccentricity;
    
    % Ekstraksi Ciri Tekstur
    GLCM = graycomatrix(I,'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);
    
    feature(n,1) = metric;
    feature(n,2) = eccentricity;
    feature(n,3) = contrast;
    feature(n,4) = correlation;
    feature(n,5) = energy;
    feature(n,6) = homogeneity;
end

target = zeros(total_images,1);
target(1:7,:) = 1;
target(8:14,:) = 2;
target(15:21,:) = 3;
target(22:28,:) = 4;

trnData = [feature,target];

numMFs = 2;
mfType = 'gbellmf';
error_goal = 1e-6;
epoch = 120;
trnOpt(1) = epoch;
trnOpt(2) = error_goal;

fismat = genfis1(trnData,numMFs,mfType);
[trnfismat,rmse] = anfis(trnData, fismat, trnOpt);
[x,mf] = plotmf(trnfismat,'input',1);
subplot(2,3,1), plot(x,mf)
xlabel('input 1 (gbellmf)')
[x,mf] = plotmf(trnfismat,'input',2);
subplot(2,3,2), plot(x,mf)
xlabel('input 2 (gbellmf)')
[x,mf] = plotmf(trnfismat,'input',3);
subplot(2,3,3), plot(x,mf)
xlabel('input 3 (gbellmf)')
[x,mf] = plotmf(trnfismat,'input',4);
subplot(2,3,4), plot(x,mf)
xlabel('input 4 (gbellmf)')
[x,mf] = plotmf(trnfismat,'input',5);
subplot(2,3,5), plot(x,mf)
xlabel('input 5 (gbellmf)')
[x,mf] = plotmf(trnfismat,'input',6);
subplot(2,3,6), plot(x,mf)
xlabel('input 6 (gbellmf)')
writefis(trnfismat,'trnfismat')

output = round(evalfis(trnData(:,1:6), trnfismat));

error = numel(find(output~=target));
akurasi_pelatihan = (numel(output)-error)/(numel(output))*100


3. Mempersiapkan citra uji untuk proses pengujian


4. Melakukan pengujian algoritma ANFIS menggunakan fuzzy inference system 
yang dihasilkan dari proses pelatihan. Koding program untuk proses 
pengujian adalah:

% Abdul Haris Kuspranoto ST. MT.
% www.sinauprogramming.com

clc; clear; close all; warning off all;

trnfismat = readfis('trnfismat');
image_folder = 'testing data';
filenames = dir(fullfile(image_folder, '*.tif'));
total_images = numel(filenames);

feature = zeros(total_images,6);

for n = 1:total_images
    full_name = fullfile(image_folder, filenames(n).name);
    I = imread(full_name);
    Img = im2bw(I,graythresh(I));
    Img = imcomplement(Img);
    Img = imfill(Img,'holes');
    Img = bwareaopen(Img,1000);
    
    % Ekstraksi Ciri Morfologi
    stats = regionprops(Img,'All');
    area = stats.Area;
    perimeter = stats.Perimeter;
    metric = 4*pi*area/(perimeter^2);
    eccentricity = stats.Eccentricity;
    
    % Ekstraksi Ciri Tekstur
    GLCM = graycomatrix(I,'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);
    
    feature(n,1) = metric;
    feature(n,2) = eccentricity;
    feature(n,3) = contrast;
    feature(n,4) = correlation;
    feature(n,5) = energy;
    feature(n,6) = homogeneity;
end

target = zeros(total_images,1);
target(1:3,:) = 1;
target(4:6,:) = 2;
target(7:9,:) = 3;
target(10:12,:) = 4;

testData = feature;
output = round(evalfis(testData, trnfismat));

error = numel(find(output~=target));
akurasi_pengujian = (numel(output)-error)/(numel(output))*100

5. Membuat tampilan Graphical User Interface (GUI)
% Abdul Haris Kuspranoto
% Website: https://sinauprogramming.com/


function varargout = Classification_Clover_Anfis(varargin)
% CLASSIFICATION_CLOVER_ANFIS MATLAB code for Classification_Clover_Anfis.fig
%      CLASSIFICATION_CLOVER_ANFIS, by itself, creates a new CLASSIFICATION_CLOVER_ANFIS or raises the existing
%      singleton*.
%
%      H = CLASSIFICATION_CLOVER_ANFIS returns the handle to a new CLASSIFICATION_CLOVER_ANFIS or the handle to
%      the existing singleton*.
%
%      CLASSIFICATION_CLOVER_ANFIS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CLASSIFICATION_CLOVER_ANFIS.M with the given input arguments.
%
%      CLASSIFICATION_CLOVER_ANFIS('Property','Value',...) creates a new CLASSIFICATION_CLOVER_ANFIS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Classification_Clover_Anfis_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Classification_Clover_Anfis_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 Classification_Clover_Anfis

% Last Modified by GUIDE v2.5 19-Oct-2020 06:08:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Classification_Clover_Anfis_OpeningFcn, ...
    'gui_OutputFcn',  @Classification_Clover_Anfis_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 Classification_Clover_Anfis is made visible.
function Classification_Clover_Anfis_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 Classification_Clover_Anfis (see VARARGIN)

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

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

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


% --- Outputs from this function are returned to the command line.
function varargout = Classification_Clover_Anfis_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)
[nama_file,nama_path] = uigetfile({'*.*'});

if ~isequal(nama_file,0)
    I = imread(fullfile(nama_path,nama_file));
    axes(handles.axes1)
    imshow(I)
    title('Original Image');
    handles.I = I;
    guidata(hObject,handles)
else
    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)
I = handles.I;
Img = im2bw(I,graythresh(I));
Img = imcomplement(Img);
Img = imfill(Img,'holes');
Img = bwareaopen(Img,1000);

% Ekstraksi Ciri Morfologi
stats = regionprops(Img,'All');
area = stats.Area;
perimeter = stats.Perimeter;
metric = 4*pi*area/(perimeter^2);
eccentricity = stats.Eccentricity;

% Ekstraksi Ciri Tekstur
GLCM = graycomatrix(I,'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);

testData = [metric,eccentricity,contrast,correlation,energy,homogeneity];
trnfismat = readfis('trnfismat.fis');
output = round(evalfis(testData, trnfismat));

axes(handles.axes2)
imshow(Img)
title('Segmentation Result Of Image');

if output == 1
    kelas = 'Clover Class A';
elseif output == 2
    kelas = 'Clover Class B';
elseif output == 3
    kelas = 'Clover Class C';
elseif output == 4
    kelas = 'Clover Class D';
end

set(handles.edit1,'String',kelas)

% --- 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)
axes(handles.axes1)
cla reset;
set(gca,'XTick',[]);
set(gca,'YTick',[]);

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

set(handles.edit1,'String','');

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

Data ada di Source Code

Video Selengkapnya 



0 Comments