Detect Cars in Video using regionprops

5 ビュー (過去 30 日間)
Nguyen Nguyen
Nguyen Nguyen 2023 年 2 月 1 日
コメント済み: Nguyen Nguyen 2023 年 2 月 1 日
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
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);
Nguyen Nguyen
Nguyen Nguyen 2023 年 2 月 1 日
This doesn't work :(

サインインしてコメントする。

回答 (0 件)

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by