To solve my above-mentioned problem, I used Matlab GUI. In this GUI, I create one axes (axes1 in which video will be shown) and three pushbuttons named start, stop and delete. Opening function of GUI was:
function stop_video_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
imaqreset
handles.cam = webcam(1);
guidata(hObject, handles);
using handles.cam = webcam(1); I start my camera.
In start callback function I used while loop with KeepRunning global variable. When KeepRunning = 1, the code inside while loop will run. Code for start function:
function start_Callback(hObject, eventdata, handles)
global KeepRunning;
KeepRunning=1;
while KeepRunning
cam = handles.cam;
data = snapshot(cam);
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
imshow(data, 'Parent', handles.axes1);
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', 'yellow');
end
hold off
drawnow;
end
guidata(hObject, handles);
By clicking on Stop pushbutton KeepRunning will be equal to 0 and video get paused. Code for stop button is below:
function stop_Callback(hObject, eventdata, handles)
global KeepRunning;
KeepRunning=0;
guidata(hObject, handles);
Delete pushbutton is used to delete cam and its code is below:
function delete_Callback(hObject, eventdata, handles)
cam = handles.cam;
delete (cam);
clear cam;
guidata(hObject, handles);
Image of my GUI is:
Thanks.