Identifikasi Jenis Buah Tomat Berdasarkan Analisis Bentuk Dan Tekstur


Analisis bentuk dan tekstur dapat digunakan untuk merancang sebuah sistem identifikasi objek. Berikut ini merupakan contoh pemrograman MATLAB untuk mengidentifikasi jenis buah tomat (hijau dan merah) berdasarkan analisis bentuk dan tekstur. Analisis bentuk dilakukan menggunakan parameter metric dan eccentricity, sedangkan analisis tekstur dilakukan menggunakan metode Gray Level Co-occurence Matrix (GLCM) dengan parameter contrast, correlation, energy, dan homogeneity.

Langkah-langkah pemrograman-nya adalah sebagai berikut:
1. Mempersiapkan citra buah tomat

Citra yang digunakan terdiri dari 4 citra buah tomat berwarna hijau dan 4 citra buah tomat berwarna merah

2. Melakukan segmentasi dan ekstraksi ciri berdasarkan analisis bentuk dan tekstur
Source code yang digunakan adalah sebagai berikut

clc; clear; close all; warning off all;

image_folder = 'D:\Identifikasi Jenis Buah Tomat Berdasarkan Analisis Bentuk Dan Tekstur\tomat';
filenames = dir(fullfile(image_folder, '*.jpg'));
total_images = numel(filenames);
ciri_database = zeros(total_images,6);

for n = 1:total_images
    full_name= fullfile(image_folder, filenames(n).name);
    Img = imread(full_name);
    
    % Color-Based Segmentation Using K-Means Clustering
    cform = makecform('srgb2lab');
    lab = applycform(Img,cform);
    
    ab = double(lab(:,:,2:3));
    nrows = size(ab,1);
    ncols = size(ab,2);
    ab = reshape(ab,nrows*ncols,2);
    
    nColors = 2;
    [cluster_idx, ~] = kmeans(ab,nColors,'distance','sqEuclidean', ...
        'Replicates',3);
    
    pixel_labels = reshape(cluster_idx,nrows,ncols);
    
    segmented_images = cell(1,3);
    rgb_label = repmat(pixel_labels,[1 1 3]);
    
    for k = 1:nColors
        color = Img;
        color(rgb_label ~= k) = 0;
        segmented_images{k} = color;
    end
    
    area_cluster1 = sum(find(pixel_labels==1));
    area_cluster2 = sum(find(pixel_labels==2));
    
    [~,cluster_min] = min([area_cluster1,area_cluster2]);
    
    Img_bw = (pixel_labels==cluster_min);
    Img_bw = imfill(Img_bw,'holes');
    Img_bw = bwareaopen(Img_bw,50);
    
    stats = regionprops(Img_bw,'Area','Perimeter','Eccentricity');
    area = stats.Area;
    perimeter = stats.Perimeter;
    metric = 4*pi*area/(perimeter^2);
    eccentricity = stats.Eccentricity;
    
    Img_gray = rgb2gray(Img);
    Img_gray(~Img_bw) = 0;
    
    pixel_dist = 1;
    GLCM = graycomatrix(Img_gray,'Offset',[0 pixel_dist; -pixel_dist pixel_dist; -pixel_dist 0; -pixel_dist -pixel_dist]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    Contrast = mean(stats.Contrast);
    Correlation = mean(stats.Correlation);
    Energy = mean(stats.Energy);
    Homogeneity = mean(stats.Homogeneity);
    
    ciri_database(n,:) = [metric,eccentricity,Contrast,Correlation,Energy,Homogeneity];
end

save ciri_database ciri_database

Data base ciri bentuk dan tekstur dari citra buah tomat yang diekstrak, ditunjukkan pada tabel di bawah ini

Pada tabel di atas, kolom ke-1 dan 2 adalah nilai metric dan eccentricity yang merupakan parameter dari analisis bentuk, sedangkan kolom ke-3, 4, 5, dan 6 adalah nilai contrast, correlation, energy, dan homogeneity yang merupakan parameter dari analisis tekstur. Data ciri tersebut kemudian disimpan untuk dapat di-load dalam tahapan identifikasi.

3. Tahapan identifikasi dilakukan menggunakan GUI dengan tampilan sebagai berikut:
a. Tampilan awal

b. Buka citra buah tomat

c. Transformasi ruang warna citra yang semula berada pada ruang warna RGB menjadi ruang warna L*a*b

d. Segmentasi citra menggunakan metode k-means clustering

e. Ekstraksi ciri bentuk terhadap citra biner berdasarkan parameter metric dan eccentricity

f. Ekstraksi ciri tekstur terhadap citra grayscale berdasarkan parameter contrast, correlation, energy, dan homogeneity

g. Identifikasi jenis buah tomat melalui perhitungan jarak euclidean

Pada tampilan GUI di atas, tampak bahwa citra buah tomat merah diidentifikasi juga sebagai tomat merah. Hal ini menunjukkan bahwa proses identifikasi sesuai dengan target yang diharapkan.

Tampilan GUI proses identifikasi untuk citra buah tomat yang lain adalah sebagai berikut

Tampilan source code GUI sistem identifikasi jenis buah tomat adalah sebagai berikut:

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

% Last Modified by GUIDE v2.5 29-Dec-2017 22:49:27

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

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

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

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


% --- Outputs from this function are returned to the command line.
function varargout = identifikasi_tomat_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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
load ciri_database
ciri_total = handles.ciri_total;

ciri = zeros(1,6);
for i = 1:6
    ciri(i) = str2double(ciri_total{i,2});
end

[num,~] = size(ciri_database);

dist = zeros(1,num);
for n = 1:num
    data_base = ciri_database(n,:);
    jarak = sum((data_base-ciri).^2).^0.5;
    dist(n) = jarak;
end

[~,id] = min(dist);

if isempty(id)
    set(handles.edit1,'String','Unknown')
else
    switch id
        case 1
            tingkat = 'Tomat Hijau';
        case 2
            tingkat = 'Tomat Hijau';
        case 3
            tingkat = 'Tomat Hijau';
        case 4
            tingkat = 'Tomat Hijau';
        case 5
            tingkat = 'Tomat Merah';
        case 6
            tingkat = 'Tomat Merah';
        case 7
            tingkat = 'Tomat Merah';
        case 8
            tingkat = 'Tomat Merah';
    end
    set(handles.edit1,'String',tingkat)
end


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


% --- 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)
Img_bw = handles.Img_bw;

axes(handles.axes4)
imshow(Img_bw)
title('Citra biner');

stats = regionprops(Img_bw,'Area','Perimeter','Eccentricity');
area = stats.Area;
perimeter = stats.Perimeter;
metric = 4*pi*area/(perimeter^2);
eccentricity = stats.Eccentricity;

ciri_bentuk = cell(2,2);
ciri_bentuk{1,1} = 'Metric';
ciri_bentuk{2,1} = 'Eccentricity';
ciri_bentuk{1,2} = num2str(metric);
ciri_bentuk{2,2} = num2str(eccentricity);

row_cell = cell(2,1);
for i = 1:2
    row_cell{i} = num2str(i);
end

handles.ciri_bentuk = ciri_bentuk;
guidata(hObject, handles)

set(handles.text2,'String','Ekstraksi Ciri')
set(handles.uitable1,'Data',ciri_bentuk,'RowName',row_cell)

% --- 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)
Img = handles.Img;
Img_bw = handles.Img_bw;
ciri_bentuk = handles.ciri_bentuk;

Img_gray = rgb2gray(Img);
Img_gray(~Img_bw) = 0;

axes(handles.axes5)
imshow(Img_gray)
title('Citra Grayscale')

pixel_dist = 1;
GLCM = graycomatrix(Img_gray,'Offset',[0 pixel_dist; -pixel_dist pixel_dist; -pixel_dist 0; -pixel_dist -pixel_dist]);
stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
Contrast = mean(stats.Contrast);
Correlation = mean(stats.Correlation);
Energy = mean(stats.Energy);
Homogeneity = mean(stats.Homogeneity);

ciri_total = cell(6,2);
ciri_total{1,1} = ciri_bentuk{1,1};
ciri_total{1,2} = ciri_bentuk{1,2};
ciri_total{2,1} = ciri_bentuk{2,1};
ciri_total{2,2} = ciri_bentuk{2,2};
ciri_total{3,1} = 'Contrast';
ciri_total{4,1} = 'Correlation';
ciri_total{5,1} = 'Energy';
ciri_total{6,1} = 'Homogeneity';
ciri_total{3,2} = num2str(Contrast);
ciri_total{4,2} = num2str(Correlation);
ciri_total{5,2} = num2str(Energy);
ciri_total{6,2} = num2str(Homogeneity);

row_cell = cell(6,1);
for i = 1:6
    row_cell{i} = num2str(i);
end

handles.ciri_total = ciri_total;
guidata(hObject, handles)

set(handles.text2,'String','Ekstraksi Ciri')
set(handles.uitable1,'Data',ciri_total,'RowName',row_cell)


% --- 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)
Img = handles.Img;

% Color-Based Segmentation Using K-Means Clustering
cform = makecform('srgb2lab');
lab = applycform(Img,cform);
axes(handles.axes2)
imshow(lab)
title('Citra L*a*b');

handles.lab = lab;
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)
Img = handles.Img;
lab = handles.lab;

ab = double(lab(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);

nColors = 2;
[cluster_idx, ~] = kmeans(ab,nColors,'distance','sqEuclidean', ...
    'Replicates',3);

pixel_labels = reshape(cluster_idx,nrows,ncols);

segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);

for k = 1:nColors
    color = Img;
    color(rgb_label ~= k) = 0;
    segmented_images{k} = color;
end

area_cluster1 = sum(find(pixel_labels==1));
area_cluster2 = sum(find(pixel_labels==2));

[~,cluster_min] = min([area_cluster1,area_cluster2]);

Img_bw = (pixel_labels==cluster_min);
Img_bw = imfill(Img_bw,'holes');
Img_bw = bwareaopen(Img_bw,50);

cabai = Img;
R = cabai(:,:,1);
G = cabai(:,:,2);
B = cabai(:,:,3);
R(~Img_bw) = 0;
G(~Img_bw) = 0;
B(~Img_bw) = 0;
cabai_rgb = cat(3,R,G,B);
axes(handles.axes3)
imshow(cabai_rgb)
title('Citra Hasil Segmentasi');

handles.Img_bw = Img_bw;
guidata(hObject, handles)


% --- 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)
[filename,pathname] = uigetfile('*.jpg');

if ~isequal(filename,0)
    Img = imread(fullfile(pathname,filename));
    axes(handles.axes1)
    imshow(Img)
    title('Citra RGB')
else
    return
end

handles.Img = Img;
guidata(hObject, handles)

% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (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',[])

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

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

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

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

Source code sistem identifikasi jenis buah tomat pada pemrograman matlab di atas dapat diunduh pada halaman berikut ini: Source Code

Posted on December 30, 2017, in Pengenalan Pola, Pengolahan Citra and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. 10 Comments.

  1. Assalamualaikum , ka saya coba buat database ekstraksi tanpa menggunakan fungsi contrast dkk yang ada di matlab, saya pakai rumus manual yang di ketik untuk nilai contrast dkk di matlabny tapi kenapa ga bisa kebaca ka ? jadi nilai ekstraksi saya 0 semua di dalam database ny. Apa database ny hanya baca rumus yg mengggunakan fungsi-fungsi yang sudah disediakan di matlab ? terimakasih ka

  2. selamat sore kak, mau bertanya
    dalam glcm sebelum ekstraksi ciri kan ada tahapannya, ini dari jurnal yg saya baca:
    1. Membuat area kerja matriks.
    2. Menentukan hubungan spasial antara piksel referensi dengan piksel tetangga, berapa nilai sudut θ dan jarak d.
    3.dan seterusnya

    untuk poin 1 itu buat area kerja matriksnya bagaimana ya kak, saya kan ada masukan citra uda skala abu2 ukuran 200×200 jadi yg nongol 200×200 di workspace matlab (banyak bgt) sedangkan yg saya liat liat (jurnal) ukuran matriksnya uda 3×3/4×4/5×5 itu diambil secara acak kah kak? atau ada cara khusus? saya mau hitung manual kak untuk ekstraksi ciri dgn rumusnya masing2, takutnya ditanya asal mulanya X(

    maap kalo pertanyaannya kurang jelas, mohon pencerahannya

  3. saya coba run muncul pesan error

    Undefined function or variable ‘identifikasi_tomat’.

    Error in @(hObject,eventdata)identifikasi_tomat(‘edit1_CreateFcn’,hObject,eventdata,guidata(hObject))

    dan saaya pas mau klik tombol2nya

    Undefined function or variable ‘identifikasi_tomat’.

    Error in @(hObject,eventdata)identifikasi_tomat(‘pushbutton1_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback

    ada solusinya mas?

    • matlab yg sya gunakan matlab 2015

    • ketika membuka citra dan memilih citra muncul pesan error lgi

      Attempt to reference field of non-structure array.

      Error in identifikasi_tomat>pushbutton1_Callback (line 316)
      axes(handles.axes1)

      Error in gui_mainfcn (line 95)
      feval(varargin{:});

      Error in identifikasi_tomat (line 46)
      gui_mainfcn(gui_State, varargin{:});

      Error in @(hObject,eventdata)identifikasi_tomat(‘pushbutton1_Callback’,hObject,eventdata,guidata(hObject))

      Error while evaluating UIControl Callback

    • Bisa dicoba didownload dan dirun kembali

    • masih error mas.ketika baru mau buka citra

      Attempt to reference field of non-structure array.

      Error in identifikasi_tomat>pushbutton1_Callback (line 316)
      axes(handles.axes1)

      Error in gui_mainfcn (line 95)
      feval(varargin{:});

      Error in identifikasi_tomat (line 46)
      gui_mainfcn(gui_State, varargin{:});

      Error in @(hObject,eventdata)identifikasi_tomat(‘pushbutton1_Callback’,hObject,eventdata,guidata(hObject))

      Error while evaluating UIControl Callback

    • sudah saya coba tidak ada error
      bisa dicoba didownload ulang kembali

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 )

w

Connecting to %s

%d bloggers like this: