0% found this document useful (0 votes)
7 views

RGB

Uploaded by

Deviaputriwl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

RGB

Uploaded by

Deviaputriwl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

24.

2 MEMBUAT GUI KALIBRASI KOMPUTER VISION PENDETEKSI OBJEK

Untuk membuat GUI kalibrasi komputer vision pendeteksi objek berwarna digunakan
algoritma pengolahan citra digial, algoritma ini akan menghasilkan kordinat objek
berdasarkan pusat warnanya. Kordinat objek ini kemudian digunakan untuk membuat
logika posisi objek sebagaimana digambarkan pada Gambar 24.1. Berikut adalah tahapan
pembuatan GUI untuk kalibrasi pendeteksian objek berdasarkan warna menggunakan fasilitas
GUIDE MATLAB:

1. Bukalah program MATLAB. Setelah itu, ketiklah ‘guide’ pada Command Windows dan
pilihlah Create a New GUI seperti berikut.

Gambar 24.4 Jendela GUIDE Quick Start untuk membuat file baru GUI MATLAB

2. Kemudian akan muncul tampilan yang siap diisi desain tampilan


Gambar 24.5 Tampilan jendela GUIDE untuk membuat GUI MATLAB

3. Buatlah tampilan robot vision pendeteksi objek seperti contoh berikut

Gambar 24.6 Desain GUI untuk robot vision pendeteksi objek

4. Lakukan penyesuaian pada property inspector dengan double click pada item yang ingin
di atur propertinya.
Gambar 24.7 Pengaturan string dan tag pada property Inspector

5. Lakukanlah pengaturan property inspector sebagai tabel berikut

Tabel 24.3 Pengaturan property inspector


Komponen String Tag
Text ROBOT VISION PENDETEKSI text 1
OBJEK BERWARNA
Popupmenu Pilih Nilai Frame pop
25
50
100
150
200
300
400
Axes axes1
Axes axes2
uipanel Pilih Warna uipanel1
uipanel Kordinat Objek uipanel2
uipanel Output Arah Gerak Uipanel3
Push Button Set set
Push Button MERAH redv
Push Button HIJAU greenv
Push Button BIRU bluev
Push Button START startv
Push Button RESET reset
Push Button EXIT exit
Edit bc1
Edit bc2
Edit arah
Text Sumbu-X text2
Text Sumbu-Y text3
Text Video Real Time vd
Text Deteksi Warna dw

6. Kemudian save file, misalkan diberi nama: GUIvision, maka akan terdapat dua file yang
tersimpan yaitu GUIvision.m dan GUIvision.fig. Setelah itu isikan program utama pada
function GUIvision_OpeningFcn, function pop_Callback, function set_Callback

, function startv_Callback, function reset_Callback, function exit_Callback,


function bc1_Callback, function bc2_Callback, function redv_Callback,

function greenv_Callback, function bluev_Callback sedangkan yang lainnya akan


terprogram otomatis. Program lengkap GUIvision.m dapat dibuat seperti contoh berikut:

Program GUI MATLAB Untuk Mendeteksi Objek Berwarna

function varargout = GUIvision(varargin)


% GUIVISION MATLAB code for GUIvision.fig
% GUIVISION, by itself, creates a new GUIVISION or raises the existing
% singleton*.
% H = GUIVISION returns the handle to a new GUIVISION or the handle to
% the existing singleton*.
% GUIVISION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUIVISION.M with the given input
arguments.
%
% GUIVISION('Property','Value',...) creates a new GUIVISION or raises
the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before GUIvision_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to GUIvision_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 GUIvision
% Last Modified by GUIDE v2.5 23-Dec-2013 15:25:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUIvision_OpeningFcn, ...
'gui_OutputFcn', @GUIvision_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 GUIvision is made visible.


function GUIvision_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 GUIvision (see VARARGIN)
% Choose default command line output for GUIvision
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set(handles.axes1,'Visible','off');
set(handles.axes2,'Visible','off');
set(handles.dw,'visible','off');
set(handles.uipanel1,'visible','off');
set(handles.startv,'enable','on');
set(handles.set,'enable','off');
set(handles.pop,'enable','on');
set(handles.vd,'visible','off');
% UIWAIT makes GUIvision wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = GUIvision_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 selection change in pop.


function pop_Callback(hObject, eventdata, handles)
% hObject handle to pop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch val
case 1
warndlg('Please select a frame value');
set(handles.set,'enable','off');
case 2
handles.frame=25;
set(handles.set,'enable','on');
case 3
handles.frame=50;
set(handles.set,'enable','on');
case 4
handles.frame=100;
set(handles.set,'enable','on');
case 5
handles.frame=150;
set(handles.set,'enable','on');
case 6
handles.frame=200;
set(handles.set,'enable','on');
case 7
handles.frame=300;
set(handles.set,'enable','on');
case 8
handles.frame=400;
set(handles.set,'enable','on');
end
guidata(hObject,handles);
% Hints: contents = cellstr(get(hObject,'String')) returns pop contents as
cell array
% contents{get(hObject,'Value')} returns selected item from pop

% --- Executes during object creation, after setting all properties.


function pop_CreateFcn(hObject, eventdata, handles)
% hObject handle to pop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 set.


function set_Callback(hObject, eventdata, handles)
% hObject handle to set (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.val=handles.frame;
guidata(hObject,handles);
set(handles.uipanel1,'visible','on');

% --- Executes on button press in startv.


function startv_Callback(hObject, eventdata, handles)
% hObject handle to startv (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;
axes(handles.axes2); cla;
set(handles.axes1,'Visible','off');
set(handles.axes2,'Visible','off');
set(handles.dw,'visible','off');
set(handles.uipanel1,'visible','off');
set(handles.startv,'enable','off');
set(handles.set,'enable','off');
set(handles.pop,'enable','off');
set(handles.vd,'visible','off');
set(handles.dw,'visible','on');
set(handles.vd,'visible','on');
h = waitbar(0,'Please wait...');
steps = 200;
for step = 1:steps
% computations take place here
waitbar(step / steps)
end
close(h)
switch handles.cv
case 1
imaqreset
info= imaqhwinfo;
vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
start(vid)
% Set loop from the value of slider
while(vid.FramesAcquired<=handles.val)
im = getsnapshot(vid);
R=im(:,:,1);
R=fliplr(R);
G=im(:,:,2);
G=fliplr(G);
B=im(:,:,3);
B=fliplr(B);
im=cat(3,R,G,B);
diff= imsubtract(im(:,:,1), rgb2gray(im));
diff2= medfilt2(diff, [3 3]);
diff3= im2bw(diff2,0.18);
diff4= bwareaopen(diff3,300);
bw = bwlabel(diff4, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
axes(handles.axes2);
imshow(bw)
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')

a=text(bc(1)+15,bc(2),strcat('X:',num2str(round(bc(1))),'Y:',num2str(round(
bc(2)))));
set(a,'FontName','Arial','FontWeight','bold','FontSize',12,'Color','red');
set(handles.bc1,'String',num2str(bc(1)));
set(handles.bc2,'String',num2str(bc(2)));
myform=guidata(gcbo);
if bc(1)>=0 && bc(1)<=106.7 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kanan curam');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kanan sedang');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','belok kanan sedikit');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','maju ke depan dekat');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','maju ke depan sedang');
elseif bc(1)>=106.7 && bc(1)<=213.4&&bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','maju ke depan jauh');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kiri curam');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kiri sedang');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=160 &&bc(2)<=240
set(myform.arah,'String','belok kiri sedikit');
else
set(myform.arah,'String','tak terdeteksi');
end
end
hold off
axes(handles.axes1);
imshow(im);
end
stop(vid);
flushdata(vid);
clear all

case 2
imaqreset
info= imaqhwinfo;
vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
start(vid)
% Set loop from the value of slider
while(vid.FramesAcquired<=handles.val)
im = getsnapshot(vid);
R=im(:,:,1);
R=fliplr(R);
G=im(:,:,2);
G=fliplr(G);
B=im(:,:,3);
B=fliplr(B);
im=cat(3,R,G,B);
diff= imsubtract(im(:,:,2), rgb2gray(im));
diff2= medfilt2(diff, [3 3]);
diff3= im2bw(diff2,0.18);
diff4= bwareaopen(diff3,300);
bw = bwlabel(diff4, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
axes(handles.axes2);
imshow(bw)
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2),strcat('X:',num2str(round(bc(1))),'Y:',num2str(round(
bc(2)))));
set(a,'FontName','Arial','FontWeight','bold','FontSize',12,'Color','red');
set(handles.bc1,'String',num2str(bc(1)));
set(handles.bc2,'String',num2str(bc(2)));
myform=guidata(gcbo);
if bc(1)>=0 && bc(1)<=106.7 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kanan curam');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kanan sedang');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','belok kanan sedikit');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','maju ke depan dekat');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=80 &&bc(2)<=160
set(myform.arah,'String','maju ke depan sedang');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=160 &&bc(2)<=240
set(myform.arah,'String','maju ke depan jauh');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kiri curam');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kiri sedang');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','belok kiri sedikit');
else
set(myform.arah,'String',' tak terdeteksi');
end
end
hold off
axes(handles.axes1);
imshow(im);
end
stop(vid);
flushdata(vid);
clear all

case 3
imaqreset
info= imaqhwinfo;
vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
start(vid)
% Set loop from the value of slider
while(vid.FramesAcquired<=handles.val)
im = getsnapshot(vid);
R=im(:,:,1);
R=fliplr(R);
G=im(:,:,2);
G=fliplr(G);
B=im(:,:,3);
B=fliplr(B);
im=cat(3,R,G,B);
diff= imsubtract(im(:,:,3), rgb2gray(im));
diff2= medfilt2(diff, [3 3]);
diff3= im2bw(diff2,0.18);
diff4= bwareaopen(diff3,300);
bw = bwlabel(diff4, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
axes(handles.axes2);
imshow(bw)
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2),strcat('X:',num2str(round(bc(1))),'Y:',num2str(round(
bc(2)))));
set(a,'FontName','Arial','FontWeight','bold','FontSize',12,'Color','red');
set(handles.bc1,'String',num2str(bc(1)));
set(handles.bc2,'String',num2str(bc(2)));
myform=guidata(gcbo);
if bc(1)>=0 && bc(1)<=106.7 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kanan curam');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kanan sedang');
elseif bc(1)>=0 && bc(1)<=106.7 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','belok kanan sedikit');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','maju ke depan dekat');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','maju ke depan sedang');
elseif bc(1)>=106.7 && bc(1)<=213.4 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','maju ke depan jauh');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=0 && bc(2)<=80
set(myform.arah,'String','belok kiri curam');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=80 && bc(2)<=160
set(myform.arah,'String','belok kiri sedang');
elseif bc(1)>=213.4 && bc(1)<=320 && bc(2)>=160 && bc(2)<=240
set(myform.arah,'String','belok kiri sedikit');
else
set(myform.arah,'String',' tak terdeteksi');
end
end
hold off
axes(handles.axes1);
imshow(im);
end
stop(vid);
flushdata(vid);
clear all
end

% --- Executes on button press in reset.


function reset_Callback(hObject, eventdata, handles)
% hObject handle to reset (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;
axes(handles.axes2); cla;
set(handles.axes1,'Visible','off');
set(handles.axes2,'Visible','off');
set(handles.dw,'visible','off');
set(handles.uipanel1,'visible','off');
set(handles.startv,'enable','off');
set(handles.set,'enable','on');
set(handles.pop,'enable','on');
set(handles.vd,'visible','off');

% --- Executes on button press in exit.


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

function arah_Callback(hObject, eventdata, handles)


% hObject handle to arah (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 arah as text
% str2double(get(hObject,'String')) returns contents of arah as a
double

% --- Executes during object creation, after setting all properties.


function arah_CreateFcn(hObject, eventdata, handles)
% hObject handle to arah (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 bc1_Callback(hObject, eventdata, handles)


% hObject handle to bc1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
bc1=str2double(get(hObject,'string'));
handles.bc1=bc1;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of bc1 as text
% str2double(get(hObject,'String')) returns contents of bc1 as a
double

% --- Executes during object creation, after setting all properties.


function bc1_CreateFcn(hObject, eventdata, handles)
% hObject handle to bc1 (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 bc2_Callback(hObject, eventdata, handles)


% hObject handle to bc2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
bc2=str2double(get(hObject,'string'));
handles.bc2=bc2;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of bc2 as text
% str2double(get(hObject,'String')) returns contents of bc2 as a
double

% --- Executes during object creation, after setting all properties.


function bc2_CreateFcn(hObject, eventdata, handles)
% hObject handle to bc2 (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 redv.


function redv_Callback(hObject, eventdata, handles)
% hObject handle to redv (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.cv=1;
guidata(hObject,handles);
set(handles.startv,'enable','on');

% --- Executes on button press in greenv.


function greenv_Callback(hObject, eventdata, handles)
% hObject handle to greenv (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.cv=2;
guidata(hObject,handles);
set(handles.startv,'enable','on');

% --- Executes on button press in bluev.


function bluev_Callback(hObject, eventdata, handles)
% hObject handle to bluev (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.cv=3;
guidata(hObject,handles);
set(handles.startv,'enable','on');

7. Setelah membuat program, kemudian tekan toolbar save and run GUIvision.m, maka
akan muncul tampilan GUI, kemudian pilih nilai frame video yang diinginkan, klik
tombol Set, tekan tombol pilihan warna yang akan dideteksi, lalu klik tombol START.
Maka sistem akan mulai mendeteksi kordinat posisi objek.
(a)

(b)
Gambar 24.8 Tampilan GUI robot vision pendeteksi objek berwarna merah

You might also like