Detect Cars in Video using regionprops
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm trying to write a code that detect cars from a video. Right now I'm trying to get the regionprops to work, it doesn't look like it's working at all since it's just giving random boxes and not connecting to the resulted video. Any help is greatly appreciated! Thank you.
clear all
close all
clc
%% Access Video
trafficVid= VideoReader('traffic.mj2');
gs = read(trafficVid,71);
implay('traffic.mj2'); %Explore video
%% Develop Algorithm
darkCarValue = 50;
darkCar = rgb2gray(gs);
noDarkCar = imextendedmax(darkCar, darkCarValue);
sedisk = strel('disk',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.
noiseFreeObject = bwareaopen(noiseFreeObject, 150);
taggedCars(:,:,:,k) = singleFrame;
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
end
%% Results
frameRate = trafficVid.FrameRate;
implay(taggedCars,frameRate);
6 个评论
prasanth s
2023-2-1
following code includes black car detection using a theshold value 200
clear all
close all
clc
%% Access Video
trafficVid= VideoReader('traffic.mj2');
gs = read(trafficVid,71);
%% Develop Algorithm
darkCarValue = 50;
darkCar = rgb2gray(gs);
noDarkCar = imextendedmax(darkCar, darkCarValue);
sedisk = strel('disk',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));
figure;
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.
noiseFreeObject = bwareaopen(noiseFreeObject, 150);
DarkCars = ~imextendedmax(I, 200);
DarkCars = imopen(DarkCars, sedisk);
DarkCars = imdilate(DarkCars, strel('disk',2));
DarkCars = bwareaopen(DarkCars, 150);
noiseFreeObject=noiseFreeObject|DarkCars;
taggedCars(:,:,:,k) = singleFrame;
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
%% Results
% frameRate = trafficVid.FrameRate;
% implay(taggedCars,frameRate);
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Octave 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!