フィルターのクリア

Centroid based object Tracking(vehicle tracking using image processing) ,Counting number of vehicles in video

7 ビュー (過去 30 日間)
I am trying to count all the vehicles in a video.(or track all the objects in video)
I am trying to track vehicle in a sample video.For that perposes I have differentiated the videos in frames .And I would like to track the objects in every frame.In everyframe I found the old objects and new objects .Now My task is to index every vehicle.Like the image below
In this link one get get the sample video and code. (video file) https://drive.google.com/drive/folders/1kfZJW3knR8oqhq23TD3VGQtbOu4bsGt5
Any kind of help will be highly appreciated.
clc
clear all
close all
%%
Filename='newtraf.mp4';
temp=zeros(2,10);
standarddim=imread('120.jpg');%there is no vehicle in this image
%create a VideoReader object
vid = VideoReader(Filename);
p=0;
figure
while vid.hasFrame %loop through the video file using an index
currentFrame = vid.readFrame(); %read an image
imshow(currentFrame);
%%
im3=(currentFrame-standarddim);
im3=im3(:,:,1);
im3=im3>34;
s=strel('disk',5);
im3=imdilate(im3,s);
title('Drawing Bounding Box over original image')
hold on;
%%
stats = regionprops(im3);
if(size(stats)>0)
mm=transpose(stats(1).Centroid);
% this mm is the array to store all the centroids of the objects in current frame
for i =2 : size(stats)
mm =[mm transpose(stats(i).Centroid)];
end
end
%% finding new objects and mark them with a id no
[newcar,oldcar]=Currentobj(mm,temp); % I made a function wchich return the new object centoid
newcar(3,:)=p:(size(newcar,2)+p-1);% I am trying to index
%%in this section it demands couple of lines code.
temp=mm;
%%
%%
for i=1:length(stats)
if(stats(i).Area>300)
rectangle('Position', stats(i).BoundingBox, 'LineWidth', 2, 'EdgeColor', 'g');
end
end
pause(1.0/vid.FrameRate);
end
%%
% function for conting new objects in a frame.
function [new_object,old_object]= Currentobj(neww_frame,previous_frame)
[rn,cn]=size(neww_frame);
[rt,ct]=size(previous_frame);
old_object=[0;0];
new_object=[0;0];
for i=1:cn
count=0;
for j=1:ct
if (distnce(neww_frame(:,i),previous_frame(:,j))<10)
old_object=[old_object neww_frame(:,i)];
count=count+1;
break;
end
end
if(count==0)
new_object=[new_object neww_frame(:,i)];
end
end
new_object(:,1)=[];
old_object(:,1)=[];
end
function d =distnce(p,q)
d = sqrt( (p(1)-q(1))^2 + (p(2)-q(2))^2 );
end
Thanks in advance.Please help me indexing the objects in frame.

回答 (1 件)

Satwik
Satwik 2024 年 5 月 24 日
編集済み: Satwik 2024 年 5 月 24 日
Hi
With certain modification to your code you can achieve your desired goal of displaying indexes for the car objects in the video. These modifications include rework of the 'CurrentObj' function, the method to extract and store centroids and the approach to display rectangles and car object index on the video clip. More details are given below:
  1. Initialize a variable ‘temp’ to store the centroids.
clc;
clear all;
close all;
Filename = 'tr2.mp4';
temp = zeros(10, 0); % Initialize temp with correct dimensions
2. For extracting centroid of the objects you may use a direct approach.
stats = regionprops(im3, 'Centroid', 'BoundingBox', 'Area');
centroids = cat(1, stats.Centroid); % Extract centroids directly
% Update the IDs based on new detections
[newcar, oldcar] = Currentobj(centroids', temp);
temp = centroids'; % Update temp for the next iteration
3. Modify the ‘CurrentObj’ function for counting new objects in a frame.
function [new_object, old_object] = Currentobj(new_frame, previous_frame)
old_object = [];
new_object = [];
newObjectCount = 0;
for i = 1:size(new_frame, 2)
found = false;
for j = 1:size(previous_frame, 2)
if distnce(new_frame(:,i), previous_frame(:,j)) < 10
found = true;
break;
end
end
if ~found
newObjectCount = newObjectCount + 1;
new_object = [new_object, [new_frame(:,i); newObjectCount]]; % Append new object with its ID
end
end
end
4. And then to draw rectangles and display the IDs for the cars, you may go with this approach.
for i = 1:length(stats)
if stats(i).Area > 300 && i <= size(newcar, 2)
rectangle('Position', stats(i).BoundingBox, 'LineWidth', 2, 'EdgeColor', 'g');
% Display the ID near the detected car
text(centroids(i, 1), centroids(i, 2), sprintf('%d', newcar(3, i)), 'Color', 'yellow', 'FontSize', 30);
end
end
You may try the above modifications in your code the achieve the desired goal.
Hope this help!

カテゴリ

Help Center および File ExchangeTracking and Motion Estimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by