Remove landmarks (non moving object) from video

5 vues (au cours des 30 derniers jours)
Nguyen Nguyen
Nguyen Nguyen le 28 Fév 2023
Hi all,
I have this code of putting a green box on objects that are detected in a video. The idea is to recognize white car in my video. The code is below:
clear all
close all
clc
%% Access Video
trafficVid= VideoReader('traffic-27262.gif');
gs = read(trafficVid,15);
implay('traffic-27261.mov'); %Explore video
%% Develop Algorithm
darkCarValue = 70;
darkCar = rgb2gray(read(trafficVid,15));
noDarkCar = imextendedmax(darkCar, darkCarValue);
imshow(darkCar)
figure, imshow(noDarkCar)
%%
sedisk = strel('line',20,25);
%sesquare = strel('square', 2);
noiseFreeObject = imopen(noDarkCar, sedisk);
imshow(noiseFreeObject)
title('Object After Removing Noise');
%% Apply Algorithm
nframes = trafficVid.NumberOfFrames;
I = read(trafficVid, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
singleFrame = read(trafficVid, k);
% Convert to grayscale to do morphological processing.
I = rgb2gray(singleFrame);
noDarkCars = imextendedmax(I, darkCarValue);
% Remove lane markings and other non-disk shaped structures.
noiseFreeObject = imopen(noDarkCars, sedisk);
% Remove small structures by removing all pixels less than 150 px.
noiseFreeObject = bwareaopen(noiseFreeObject, 150);
bw = bwlabel(noiseFreeObject);
stats = regionprops(noiseFreeObject, 'BoundingBox','Centroid');
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',2);
end
figure(1),imshow(noiseFreeObject,[])
end
I couldn't upload the video because my video was in mp4 so I converted it to gif but I'm not sure if it works. Anyway, the problem I have is my code does not only detect the car but also the other stuff. I tried to get rid of it by using strel but some of the background objects are the same as the car so I can only get rid of it partially this way.
My question is is there a better way to get rid of other objects in the video that are not cars? I'm thinking removing non-moving objects but I don't know how to do that.
Thank you in advance!

Réponses (1)

Rishav
Rishav le 7 Mar 2023
One approach to remove non-moving objects in a video is to use background subtraction. This method assumes that the background of the video is static and that moving objects can be detected by comparing each frame with the background.
Here's a modified version of your code that uses background subtraction to detect only moving objects:
clear all
close all
clc
%% Access Video
trafficVid= VideoReader('traffic-27262.gif');
%% Background Subtraction
background = read(trafficVid,1);
for k = 2:30 % Use first 30 frames to get background
background = imadd(background, read(trafficVid,k));
end
background = background/30;
figure, imshow(background)
%% Develop Algorithm
darkCarValue = 70;
sedisk = strel('line',20,25);
nframes = trafficVid.NumberOfFrames;
I = read(trafficVid, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
singleFrame = read(trafficVid, k);
% Convert to grayscale to do morphological processing.
I = rgb2gray(singleFrame);
% Subtract background to detect moving objects
foreground = imabsdiff(I,background);
threshold = graythresh(foreground);
movingObjects = imbinarize(foreground,threshold);
noDarkCars = imextendedmax(I, darkCarValue);
% Remove lane markings and other non-disk shaped structures.
noiseFreeObject = imopen(noDarkCars, sedisk);
% Remove small structures by removing all pixels less than 150 px.
noiseFreeObject = bwareaopen(noiseFreeObject, 150);
% Only keep moving objects
noiseFreeObject = noiseFreeObject & movingObjects;
bw = bwlabel(noiseFreeObject);
stats = regionprops(noiseFreeObject, 'BoundingBox','Centroid');
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',2);
end
figure(1),imshow(noiseFreeObject,[])
end
The code now includes a background subtraction step that calculates the average of the first 30 frames of the video to obtain the background. This background is then subtracted from each frame of the video to obtain a foreground mask that highlights the moving objects.
The ‘movingObjects’ mask is then combined with the ‘noiseFreeObject’ mask to only keep the objects that are moving. This should help eliminate false detections from stationary objects in the scene.
You may need to adjust the threshold value and other parameters to get the best results for your particular video.
For more reference, you can refer:
  1. Background subtraction algorithm for real time video? - Help Center - MATLAB & Simulink (mathworks.com)
  2. Abandoned Object Detection

Community Treasure Hunt

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

Start Hunting!

Translated by