문제

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