How to use matlab vision.ForegroundDetector with webcam instead of input video file.

hsrc = vision.VideoFileReader('viptraffic.avi','ImageColorSpace','Intensity','VideoOutputDataType','uint8');
hfg = vision.ForegroundDetector(...
       'NumTrainingFrames', 5, ... % 5 because of short video
       'InitialVariance', 30*30); % initial standard deviation of 30
hblob = vision.BlobAnalysis(...
       'CentroidOutputPort', false, 'AreaOutputPort', false, ...
       'BoundingBoxOutputPort', true, ...
       'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 250);
hsi = vision.ShapeInserter('BorderColor','White');

hsnk = vision.VideoPlayer();
while ~isDone(hsrc)
     frame  = step(hsrc);
     fgMask = step(hfg, frame);
     bbox   = step(hblob, fgMask);
     out    = step(hsi, frame, bbox); % draw bounding boxes around cars
     step(hsnk, out); % view results in the video player
end
release(hsnk);
release(hsrc);
有帮助吗?

解决方案

If you have access to the Image Acquisition Toolbox, you could replace the first line with:

hsrc = imaq.VideoDevice('winvideo', 1, 'MJPG_640x480', ...
    'ReturnedColorSpace','grayscale', 'ReturnedDataType','uint8');

You will need to adjust the parameters according to the supported formats by your webcam. Just consult the documentation of the imaq.VideoDevice function.

Also replace the loop test by just while true since the video feed is always not done :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top