MATLAB 2014b:无法关闭网络摄像头,Matlab 在处理视频时卡住了

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

我想在 Matlab 2014b 中使用实时视频处理来跟踪红色。要打开并查看预览,我使用了以下代码行:

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

当我想关闭预览和摄像头时,我使用了以下代码行

closePreview(cam);
clear cam;

但是当我 运行 我的红色跟踪对象脚本然后使用 closePreview(cam); clear cam 关闭预览和清除 cam 时,它不起作用并且 Matlab 保持忙碌。即使在用鼠标取消预览 window 之后,Matlab 仍然很忙,我什至无法关闭 Matlab。

红色跟踪脚本代码为:

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

我认为问题出在 while loop,所以为了测试我所做的是,我在命令 window

中编写了以下代码行
take_photo = 0;
closePreview(cam);
  clear cam;

但这没有用。

所以,我想在跟踪红色对象后关闭预览并在需要时清除凸轮。怎么做?

谢谢。

为了解决我的 above-mentioned 问题,我使用了 Matlab GUI。在此 GUI 中,我创建了一个轴(将在其中显示视频的 axes1)和三个名为开始、停止和删除的按钮。 GUI 的打开功能是:

function stop_video_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
imaqreset
handles.cam = webcam(1);
guidata(hObject, handles);

使用 handles.cam = webcam(1); 我启动相机。

在启动回调函数中,我使用了带有 KeepRunning 全局变量的 while 循环。当 KeepRunning = 1 时,while 循环内的代码将 运行。 启动函数代码:

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);

点击停止按钮 KeepRunning 将等于 0 并且视频暂停。停止按钮的代码如下:

function stop_Callback(hObject, eventdata, handles)
global KeepRunning;
KeepRunning=0;
guidata(hObject, handles);

删除按钮用于删除cam,其代码如下:

function delete_Callback(hObject, eventdata, handles)
cam = handles.cam;
delete (cam);
clear cam;
guidata(hObject, handles);

我的 GUI 图像是:

谢谢。