-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIforvideo.m
executable file
·297 lines (279 loc) · 10.1 KB
/
GUIforvideo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
function varargout = GUIforvideo(varargin)
% GUIFORVIDEO M-file for GUIforvideo.fig
% GUIFORVIDEO, by itself, creates a new GUIFORVIDEO or raises the existing
% singleton*.
%
% H = GUIFORVIDEO returns the handle to a new GUIFORVIDEO or the handle to
% the existing singleton*.
%
% GUIFORVIDEO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUIFORVIDEO.M with the given input arguments.
%
% GUIFORVIDEO('Property','Value',...) creates a new GUIFORVIDEO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUIforvideo_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% reset. All inputs are passed to GUIforvideo_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 GUIforvideo
% Last Modified by GUIDE v2.5 16-Jul-2011 01:44:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUIforvideo_OpeningFcn, ...
'gui_OutputFcn', @GUIforvideo_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 GUIforvideo is made visible.
function GUIforvideo_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 GUIforvideo (see VARARGIN)
set(handles.preview,'Enable','on');
set(handles.detect,'Enable','off');
set(handles.stop,'Enable','off');
set(handles.reset,'Enable','off');
global vid
global hImage;
vid=videoinput('winvideo',1,'RGB24_160x120');
set(vid,'ReturnedColorSpace','rgb');
set(vid,'TriggerFrameDelay',5);
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval=3;%==> .5
vidRes = get(vid, 'VideoResolution');
%mendapatkan nilai warna dari video
nBands = get(vid, 'NumberOfBands');
%untuk menampilkan farme webcam pada layar1
hImage = image( zeros(vidRes(2), vidRes(1), nBands),'parent',handles.cam);
axis off
% Choose default command line output for GUIforvideo
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUIforvideo wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUIforvideo_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 preview.
function preview_Callback(hObject, eventdata, handles)
% hObject handle to preview (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
global hImage;
preview(vid, hImage);
set(handles.preview,'Enable','off');
set(handles.detect,'Enable','on');
set(handles.stop,'Enable','off');
set(handles.reset,'Enable','off');
% --- 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)
set(handles.stop,'Value',0);
cla(handles.cam,'reset');
axis off
global vid
global hImage;
vid=videoinput('winvideo',1,'RGB24_160x120');
set(vid,'ReturnedColorSpace','rgb');
set(vid,'TriggerFrameDelay',5);
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval=3;%==> .5
vidRes = get(vid, 'VideoResolution');
%mendapatkan nilai warna dari video
nBands = get(vid, 'NumberOfBands');
%untuk menampilkan farme webcam pada layar1
hImage = image( zeros(vidRes(2), vidRes(1), nBands),'parent',handles.cam);
axis off
set(handles.preview,'Enable','on');
set(handles.detect,'Enable','off');
set(handles.stop,'Enable','off');
set(handles.reset,'Enable','off');
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
closepreview(vid);
set(handles.preview,'Enable','off');
set(handles.detect,'Enable','off');
set(handles.stop,'Enable','off');
set(handles.reset,'Enable','on');
% Hint: get(hObject,'Value') returns toggle state of stop
% --- Executes on button press in detect.
function detect_Callback(hObject, eventdata, handles)
% hObject handle to detect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.preview,'Enable','off');
set(handles.detect,'Enable','off');
set(handles.stop,'Enable','on');
set(handles.reset,'Enable','off');
global vid;
global mouse;
import java.awt.Robot;
import java.awt.event.*;
mouse=Robot;
start(vid);
%proses image
while(1)%vid.FramesAcquired<=70
f=getsnapshot(vid);
% subplot(221);imshow(f);title('a');
I=f;
Iblur=fspecial('gaussian',8,35);
I=imfilter(I,Iblur,'symmetric','conv');
%deteksi kulit
YCbCr=rgb2ycbcr(I);
% subplot(222);imshow(YCbCr);title('b');
for i=1:size(YCbCr,1)
for j=1:size(YCbCr,2)
cb=YCbCr(i,j,2);
cr=YCbCr(i,j,3);
% if(~(cr >= 120 && cr <= 173 && cb >= 77 && cb <= 127))
% if(~(cr >= 125 && cr <=153 && cb >= 100 && cb <= 120))
if(~(cr >= 133 && cr <=155 && cb >= 107 && cb <= 130))
%bukan warna kulit
YCbCr(i,j)=0;
else
YCbCr(i,j)=255;
end
end
end
Irgb=ycbcr2rgb(YCbCr);
kulit=im2bw(Irgb);
kulitt=im2bw(Irgb);
kulit=bwareaopen(kulit,100);
%------------------------
[B L]=bwboundaries(kulit,'noholes');
bw=bwlabel(kulit,8);
rp=regionprops(bw,'centroid');
tgh=cat(1,rp.Centroid);
imshow(f);
hold on
for k=1:length(B)
boundary=B{k};
pixX=boundary(:,2);
pixY=boundary(:,1);
plot(boundary(:,2),boundary(:,1),'w','LineWidth',1);
plot(tgh(:,1),tgh(:,2),'b*');
xc=double(tgh(:,1));
Ux=zeros();Uy=zeros();Vx=zeros();Vy=zeros();
pU=zeros();pV=zeros();U=zeros(2);V=zeros(2);m=zeros();
sudut=zeros();a=[];b=[];z=[];
s=length(pixX)-14;
for i=1:length(pixX)
if (i-14)<1
Ux(i)=pixX(i+s)-pixX(i);
Uy(i)=pixY(i+s)-pixY(i);
Vx(i)=pixX(i+14)-pixX(i);
Vy(i)=pixY(i+14)-pixY(i);
elseif (i+14)>length(pixY)
Vx(i)=pixX(i-s)-pixX(i);
Vy(i)=pixY(i-s)-pixY(i);
Ux(i)=pixX(i-14)-pixX(i);
Uy(i)=pixY(i-14)-pixY(i);
else
Ux(i)=pixX(i-14)-pixX(i);
Uy(i)=pixY(i-14)-pixY(i);
Vx(i)=pixX(i+14)-pixX(i);
Vy(i)=pixY(i+14)-pixY(i);
end
pU(i)=sqrt(Ux(i)*Ux(i)+Uy(i)*Uy(i));
pV(i)=sqrt(Vx(i)*Vx(i)+Vy(i)*Vy(i));
U(:,i)=[Ux(i) Uy(i)];
V(:,i)=[Vx(i) Vy(i)];
m(i)=(dot(U(:,i),V(:,i)))/(pU(i)*pV(i));
sudut(i)=acos(m(i));
a(:,:,i)=[Ux(i) Uy(i) 0];
b(:,:,i)=[Vx(i) Vy(i) 0];
z(:,:,i)=cross(b(:,:,i),a(:,:,i));
end
zet=z(:,3,:);
zp=find(zet>0);
sdt=sudut(zp);%-- sudut z positif ada yang besar ada yng kecil
sdtK=find(sdt<0.872664626);%1.308996939
ind=zp(sdtK);
tipX=pixX(ind);
tipY=pixY(ind);
plot(tipX,tipY,'rO','LineWidth',1);
n=zeros(); %-----------------------------------> mulai jumlah jari berdasar pix X
for i=1:(length(tipX)-1)
n(i)=tipX(i+1)-tipX(i);
m(i)=tipY(i+1)-tipY(i);
end
q=find (n>3);
r=find(m>3);
if length(n)==1 && length(xc)==1 &&isempty(q)
k=0; %#ok<FXSET>
else if isempty(q) && length(xc)==1
k=1; %#ok<FXSET>
else if ~isempty(q) && length(xc)==1
k=length(q)+1; %#ok<FXSET>
else if ~isempty(r) && length(xc)==1
k=length(r)+1; %#ok<FXSET>
end
end
end
end
% k %-----------------------------------------------> end jumlah
% jari
if length(tipX)>1 %ppixX
p1pixX=double(tipX(1));%ppixX
p1pixY=double(tipY(1));%ppixY
else
continue
end
ptrX=int32((p1pixX/160)*1280);
ptrY=int32((p1pixY/120)*800);
if k==1 && length(xc)==1
mouse.mouseMove((1280-ptrX),ptrY);
else if k==2 && length(xc)==1
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.delay(350);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
else if k==3 &&length(xc)==1
mouse.mousePress(InputEvent.BUTTON3_MASK);
mouse.delay(350);
mouse.mouseRelease(InputEvent.BUTTON3_MASK);
else if k>3 && length(xc)==1
continue
end
end
end
end
end
hold off
flushdata(vid);
val = get(handles.stop,'Value');
if val == 1
break;
end
end
stop(vid);
delete(vid);
clear vid;