I am learning some new materials through this matlab example . In addition to the number of detected cars in each frame, I am intersted to add a counter that keep tracks of the total number cars that passed since frame1.
For example lets say:
in frame 1, 2 cars are detected, and the counters are, "current frame=2", "from begining=2"
in frame 2, 1 more car enters and one of the cars from frame 1 exits, and the counters are, "current frame=2", "from begining=4"
how would you modify the code? copied the loop from the example here.
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
while hasFrame(videoReader)
frame = readFrame(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
bbox = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
step(videoPlayer, result); % display the results
end

 Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Nov. 2021

0 Stimmen

You need to index numCars so add a loop counter
loopCounter = 1;
while hasFrame(videoReader)
numCars(loopCounter) = size(bbox, 1);
result = insertText(result, [10 10], numCars(loopCounter), 'BoxOpacity', 1, 'FontSize', 14);
loopCounter = loopCounter + 1;
end

6 Kommentare

Tala
Tala am 12 Nov. 2021
Thanks for responding @Image Analyst. I have learned alot from your previous posts.
Adding a loop counter came to my mind as well. While it might work for many cases, I think is not a robust way.
Imagine this scenario :
Frame 1, 3 red cars are detected
Frame 2, one red car exits and 2 green cars enter.
The number of cars in frame1 is 3, and in frame 2 is 4. The loop counter that you mentioned would print 3(red)+4(2 red and 2 green)=7 as the total number of cars.
However, the total number of cars are 3(red)+2(green)=5.
Am I missing something?
appreciate you :)
Image Analyst
Image Analyst am 12 Nov. 2021
Sorry but each frame is independent. For frame 2 you have 2 red cars and 2 green cars, so 4 cars total and 4 bounding boxes. So numCars(2) = 4, not 7. I'm not sure why you think numCars(2) is equal to numCars(1) + size(bbox, 1); but it is not. That's not what I said to do.
Tala
Tala am 12 Nov. 2021
Bearbeitet: Tala am 12 Nov. 2021
I hear you. I thought you are suggesting the total number of cars is sum(numCars). how can I get 5 which is the total number of cars with numCars(loopCounter)?
Image Analyst
Image Analyst am 12 Nov. 2021
That's hard. You'd have to determine when a car is only partially in the field of view and exclude that. You can do that because every car completely inside the field of view at one time entered or left it, so it will be sure to be counted. Or in other words, you'll only count cars totally in the field of view. If a car just barely entered and then backed out of the field of view, you won't count that one because you can see only part of it. So now you're only analyzing cars that are completely inside the field of view. So now you need to characterize each car (bounding box) for other features like height and width of the box, average color inside the box, position of the box (lane driving in), etc. So now you have to look at all the cars and compare to the cars in the prios frame. If you determine that a car is new to the frame, you count it as a new/additional car. If the car was present in the prior frame, you don't count it.
I don't have a full turnkey tracking app. If you want one, there are some out there. We use a tracking app by Noldus for animals. It could probably be applied to cars.
Tala
Tala am 12 Nov. 2021
thanks @Image Analyst. Doing some reserach, this gentelman demosntrates how to do such a thing in open cv. apparently there are functions to use in open cv. if you have time, please take a look at the 7 seconds from 28:21 through 28:28. The counter probably picks up some background noise and adds on, but the idea is works. It keep adding up the number of detected objects instead of starting from zero in each frame. Are you familiar with such an example in matlab or its time for me to start using open cv! haha
Image Analyst
Image Analyst am 12 Nov. 2021
I've not really had the need to do tracking myself (yet). If that works for you, you can run Python and OpenCV from within MATLAB, or just translate his Python code into MATLAB.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

yanqi liu
yanqi liu am 12 Nov. 2021

0 Stimmen

sir,may be use a line to detect the car pass,and add to total,such as

3 Kommentare

Tala
Tala am 12 Nov. 2021
sure thing, how would you modify the matlab for that?
yanqi liu
yanqi liu am 12 Nov. 2021
sir,i think when the car detect on the line,then add it to total number
of course,can use some target tracking method to compute
Image Analyst
Image Analyst am 12 Nov. 2021
It might lie across the line for several frames so you'd have to be aware of that and only add it when it first appeared, so you don't double count it. Then to handle any kind of video noise you might check to make sure that one it appeared it was there for at least 2 or 3 frames.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by