Search code examples
matlabimage-processingwebcamvideo-processing

MATLAB 2014b: Unable to close webcam and Matlab got stuck while processing Video


I want to track red color using real-time video processing in Matlab 2014b. To open and see the preview I used following lines of code:

cam = webcam (1);
preview(cam);

When I want to close the preview and cam I used following lines of code

closePreview(cam);
clear cam;

But when I run my red tracking object script and then used closePreview(cam); clear cam, to close the preview and clear cam, it doesn't work and Matlab keep remain busy. Even after canceling the preview window with mouse, Matlab keep busy and I'm not even able to close Matlab.

The code for red tracking script is:

cam = webcam (1);
preview(cam);
take_photo = 1;
while(take_photo == 1)
    % Get the snapshot of the current frame
    data = snapshot(cam);
    diff_im = imsubtract(data(:,:,1), rgb2gray(data));
    %Use a median filter to filter out noise
    diff_im = medfilt2(diff_im, [3 3]);
    % Convert the resulting grayscale image into a binary image.
    diff_im = im2bw(diff_im,0.18);
    % Remove all those pixels less than 300px
    diff_im = bwareaopen(diff_im,300);
    % Label all the connected components in the image.
    bw = bwlabel(diff_im, 8);  
    stats = regionprops(bw, 'BoundingBox', 'Centroid');
    % Display the image
    imshow(diff_im)
    hold on
      %This is a loop to bound the red objects in a rectangular box.
      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
  end

I thought the problem is with while loop, So to test what I did is, I wrote following lines of code in command window

take_photo = 0;
closePreview(cam);
  clear cam;

But this was of no use.

So, I want to close my preview and clear cam when I need it after tracking the red object. How to do it?

Thanks.


Solution

  • 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:

    enter image description here

    Thanks.