Thresholding Citra


Citra digital merupakan representasi dari fungsi intensitas cahaya dalam bidang dua dimensi. Berdasarkan jenis warnanya, citra dapat dikelompokkan menjadi tiga jenis yaitu citra RGB, citra grayscale, dan citra biner.

Citra RGB

Citra Grayscale

Citra Biner

lena lena_gray lena_bw

Citra RGB merupakan citra yang tersusun oleh tiga kanal warna yaitu kanal merah, kanal hijau, dan kanal biru. Pada citra RGB 24-bit, masing-masing kanal warna memiliki nilai intensitas piksel dengan kedalaman bit sebesar 8-bit yang artinya memiliki variasi warna sebanyak 2^8 = 256 derajat warna (0 s.d 255). Setiap piksel pada citra RGB memiliki nilai intensitas yang merupakan kombinasi dari nilai R, G, dan B. Variasi warna pada setiap piksel pada citra RGB adalah sebanyak 256 x 256 x 256 = 16.777.216.

Sedangkan citra grayscale merupakan citra yang hanya memiliki satu kanal warna. Pada citra grayscale 8-bit, setiap piksel memiliki nilai intensitas warna yang memiliki variasi sebanyak 2^8 = 256 derajat warna (0 s.d 255). Nilai intensitas pada citra ini merupakan representasi dari derajat keabuan di mana nilai 0 menyatakan warna hitam sempurna dan nilai 255 menyatakan warna putih sempurna. Nilai intensitas antara 0 s.d 255 merupakan warna abu-abu.

Sama seperti citra grayscale, citra biner juga merupakan citra yang hanya memiliki satu kanal warna. Citra biner memiliki kedalaman bit sebesar 1-bit. Nilai intensitas warna pada setiap piksel citra biner dibagi menjadi 2^1 = 2 warna yaitu warna hitam yang dinyatakan oleh nilai 0 dan warna putih yang dinyatakan oleh nilai 1.

Materi lebih lanjut mengenai definisi dan jenis citra digital dapat dilihat pada laman berikut ini: Pengolahan Citra Digital

Berikut ini merupakan tutorial cara membuat citra berwarna (RGB) menjadi hitam putih (biner) menggunakan Graphical User Interface  (GUI) MATLAB

Thresholding: Citra RGB -> Citra Grayscale -> Citra Biner

Langkah-langkahnya yaitu:

1. Membuka aplikasi software Matlab
1

2. Membuka GUIDE Matlab dengan cara mengetik “guide” pada command window dan tekan enter, sehingga muncul tampilan seperti pada gambar di bawah ini
2

atau klik menu New >> Graphical User Interface seperti yang ditunjukkan pada gambar berikut
3

3. Klik “Ok” pada GUIDE Quick Start >> Create New GUI >> Blank GUI (default)
4

sehingga akan muncul tampilan GUIDE Matlab seperti pada gambar berikut
5

4. Untuk menampilkan nama palet komponen, klik File >> Preferences
6

kemudian beri tanda centang (√) pada menu Show names in component palette lalu klik “Ok”
7

sehingga akan muncul tampilan seperti pada gambar di bawah ini
8

5. Buatlah rancangan GUI MATLAB yang terdiri dari 2 axes, 3 pushbutton, 1 slider, dan 1 edit text seperti tampak pada gambar di bawah ini

1

6. Editlah property masing-masing komponen dengan cara meng-double klik setiap komponen lalu mengganti propertynya sesuai dengan tabel berikut

No Nama Komponen Property Nilai
1 Pushbutton String Open Image
Tag pushbutton1
2 Pushbutton String Grayscale
Tag pushbutton2
3 Pushbutton String Save
Tag pushbutton3
4 Slider min 0
max 255
tag slider1
5 Edit Text String <kosongkan>
6 Axes XTick <kosongkan>
YTick <kosongkan>
ZTick <kosongkan>
7 Axes XTick <kosongkan>
YTick <kosongkan>
ZTick <kosongkan>

sehingga tampilan GUI tampak pada gambar berikut:

2

6. Listing Program untuk pusbutton1 (tombol buka citra) adalah

% --- 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)
[name_file1,name_path1] = uigetfile( ...
    {'*.bmp;*.jpg;*.tif','Files of type (*.bmp,*.jpg,*.tif)';
    '*.bmp','File Bitmap (*.bmp)';...
    '*.jpg','File jpeg (*.jpg)';
    '*.tif','File Tif (*.tif)';
    '*.*','All Files (*.*)'},...
    'Open Image');

if ~isequal(name_file1,0)
    handles.data1 = imread(fullfile(name_path1,name_file1));
    guidata(hObject,handles);
    axes(handles.axes1);
    imshow(handles.data1);
else
    return;
end

Listing Program untuk pusbutton2 (konversi citra RGB menjadi grayscale)

% --- 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)
image1 = handles.data1;
gray = rgb2gray(image1);
axes(handles.axes2);
imshow(gray);
handles.data2 = gray;
guidata(hObject,handles);

Listing Program untuk slider1 (konversi citra grayscale menjadi biner)

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
gray = handles.data2;
value = get(handles.slider1,'value');
thresh = imcomplement(im2bw(gray,value/255));
axes(handles.axes2);
imshow(thresh);
handles.data3 = thresh;
guidata(hObject,handles);
set(handles.edit1,'String',value)

Listing Program untuk pushbutton3 (menyimpan citra biner hasil konversi)

% --- 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)
thresh = handles.data3;
[name_file_save,path_save] = uiputfile( ...
    {'*.bmp','File Bitmap (*.bmp)';...
    '*.jpg','File jpeg (*.jpg)';
    '*.tif','File Tif (*.tif)';
    '*.*','All Files (*.*)'},...
    'Save Image');
if ~isequal(name_file_save,0)
    imwrite(thresh,fullfile(path_save,name_file_save));
else
    return
end

Sedangkan listing program lengkapnya adalah sbb:

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

% Last Modified by GUIDE v2.5 24-Jul-2015 02:44:32

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

% Choose default command line output for Konversi_Biner
handles.output = hObject;
movegui(hObject,'center')

% Update handles structure
guidata(hObject,handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Konversi_Biner_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)
[name_file1,name_path1] = uigetfile( ...
    {'*.bmp;*.jpg;*.tif','Files of type (*.bmp,*.jpg,*.tif)';
    '*.bmp','File Bitmap (*.bmp)';...
    '*.jpg','File jpeg (*.jpg)';
    '*.tif','File Tif (*.tif)';
    '*.*','All Files (*.*)'},...
    'Open Image');

if ~isequal(name_file1,0)
    handles.data1 = imread(fullfile(name_path1,name_file1));
    guidata(hObject,handles);
    axes(handles.axes1);
    imshow(handles.data1);
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)
image1 = handles.data1;
gray = rgb2gray(image1);
axes(handles.axes2);
imshow(gray);
handles.data2 = gray;
guidata(hObject,handles);

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
gray = handles.data2;
value = get(handles.slider1,'value');
thresh = imcomplement(im2bw(gray,value/255));
axes(handles.axes2);
imshow(thresh);
handles.data3 = thresh;
guidata(hObject,handles);
set(handles.edit1,'String',value)

% --- 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)
thresh = handles.data3;
[name_file_save,path_save] = uiputfile( ...
    {'*.bmp','File Bitmap (*.bmp)';...
    '*.jpg','File jpeg (*.jpg)';
    '*.tif','File Tif (*.tif)';
    '*.*','All Files (*.*)'},...
    'Save Image');
if ~isequal(name_file_save,0)
    imwrite(thresh,fullfile(path_save,name_file_save));
else
    return
end

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
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

7. Ketika di Run maka tampilan GUI akan tampak seperti pada gambar di bawah ini

3

8. Klik Open Image, pilih gambar yang ingin diproses

4

9. Klik Grayscale

5

10. Geser nilai Slider

6

11. Citra biner yang terbentuk dapat disimpan dengan cara meng-klik tombol Save Image

Source code dan citra untuk konversi citra RGB menjadi biner dapat diunduh pada laman berikut ini: Download source code

Save

Save

Save

Posted on September 30, 2016, in Pengolahan Citra and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. 7 Comments.

  1. Makasih mas sangat bermanfaat🙂

  2. Programnya keren & bermanfaat banget mas, khususnya unt pemula, makasih yaaa…

  3. bagaimana menampilkan nilai RGB gambar asli ya, memunculkan nilainya di edit.

  1. Pingback: Tugas Thresholding Citra – heyho68

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: