videoDetector is a modified version of the Matlab example program 'videotrafficgmm'. This program uses Gaussian mixture models to detect the foreground of a video frame, then applies blob detection to detect automobiles. A specific region of interest on the video frame should be specified. Also a counting mechanism uses two seperate regions of interest (corresponding to two lanes on the road) to count the number of cars in each lane.
- The program expects a parameter for the name of the video you would like to process. The video is expected to be in the same directory as the program, or otherwise you should specify the path.
- A window will appear with a video frame loaded, expecting a region of interest selection. Upon completion, double-click or right click and press create mask to continue the process.
- Two .mat files named 'lane1.mat' and 'lane2.mat' are expected to be in the working directory. Otherwise, this code can be commented out and code to select and save two lane regions can be enabled.
- There is a short training phase, then detection will start.
- A text file named 'detectedCars.txt' will be opened or created to write the lanes and corresponding frames in which cars are detected.
A modified version of this program can be created to support code generation. Here are the steps necessary:
- Replace
mask = roipoly(frame)
withmaskfile = coder.load('maskfile.mat');
and thenmask = maskfile.mask;
because roipoly is not supported by code generation. This means a mask must be saved ahead of time as 'maskfile.mat' in the working directory so that it can be loaded in later. - Disable all code related to displaying the frames and videos (e.g.
imshow
,videoPlayer.position
,insertShape
,insertText
,step(videoPlayer, result)
) - Hard-code the name of the video file rather than using a variable in the function parameter.
- Use
coder.varsize('frame');
after declaringframe
in order to explicitly define variable data - replace
frame .* mask
withframe(1:w, 1:h) .* mask
where w and h are the width and height of the frame respectively, in order to explicitly make the operation of the same matrix dimensions - Remove
k = bwconvhull(filteredForeground, 'objects')
because that method is not supported for code generation. Replacek
in call tobbox = step(blobAnalysis, k)
to bebbox = step(blobAnalysis, filteredForeground)
Note: this means that blobs will not be reshaped in the code generation program, potentially affecting accuracy.