Unable to close webcam and Matlab got stuck while processing Video.
Show older comments
Hello,
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 think 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.
Accepted Answer
More Answers (0)
Categories
Find more on Image Preview and Device Configuration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!