MATLAB Answers

code to detection the cars this code not counting cars properly How do i make it count correct ?

76 views (last 30 days)
Jeje Ahmad
Jeje Ahmad on 18 Oct 2020
Commented: Jeje Ahmad on 23 Nov 2020 at 10:24
clc
clear all
MV = imread('cars1.jpg'); %To read image
MV1 = imread('backgnd.jpg');
A = double(rgb2gray(MV));%convert to gray
B= double(rgb2gray(MV1));%convert 2nd image to gray
[height, width] = size(A); %image size?
h1 = figure(1);
%Foreground Detection
thresh=11;
fr_diff = abs(A-B);
for j = 1:width
for k = 1:height
if (fr_diff(k,j)>thresh)
fg(k,j) = A(k,j);
else
fg(k,j) = 0;
end
end
end
subplot(2,2,1) , imagesc(MV), title (['Orignal Frame']);
subplot(2,2,2) , imshow(mat2gray(A)), title ('converted Frame');
subplot(2,2,3) , imshow(mat2gray(B)), title ('BACKGND Frame ');
36;
sd=imadjust(fg);% adjust the image intensity values to the color map
level=graythresh(sd);
m=imnoise(sd,'gaussian',0,0.025);% apply Gaussian noise
k=wiener2(m,[5,5]);%filtering using Weiner filter
bw=im2bw(k,level);
bw2=imfill(bw,'holes');
bw3 = bwareaopen(bw2,5000);
labeled = bwlabel(bw3,8);
cc=bwconncomp(bw3);
Densityoftraffic = cc.NumObjects/(size(bw3,1)*size(bw3,2));
blobMeasurements = regionprops(labeled,'all');
numberofcars = size(blobMeasurements, 1);
subplot(2,2,4) , imagesc(labeled), title (['Foreground']);
hold off;
disp(numberofcars);% display number of cars
disp(Densityoftraffic);%display number of vehicles

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 18 Oct 2020
It looks like the dimensions of image A, B are not same. So while subtracting you are getting the error.
You need to resize them into same dimensions using imresize.
A = double(rgb2gray(MV));%convert to gray
B= double(rgb2gray(MV1));%convert 2nd image to gray
[m1,n1,p1] = size(A) ;
[m2,n2,p2] = size(B) ;
m = max(m1,m2) ;
n = max(n1,n2) ;
A = imresize(A,[m n]) ;
B = imresize(B, [m n]) ;

  9 Comments

Show 6 older comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 Oct 2020
Here, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
% excel = actxserver('excel.application');
% filename = 'C:\Users\hayworth.ms\OneDrive - Procter and Gamble\Not Shared\Mark Hayworth Personal Files\Personal Documents\Financial Statements\Assets\ASSETS.xlsm';
% workbook = excel.Workbooks.Open(filename, [], true, [], 'images');
% excel.Visible = true;
MV = imread('cars1.png'); % To read image
MV1 = imread('backgnd.png');
carsImage = double(rgb2gray(MV));% convert to gray
[rows, columns, numColors] = size(carsImage)
backgroundImage = double(rgb2gray(MV1));%convert 2nd image to gray
[rowsb, columnsb, numColorsb] = size(backgroundImage)
% If the background image is not the same size as the test image, resize the test image.
if rows ~= rowsb || columns ~= columnsb
message = sprintf('Cars = %dx%d, background = %dx%d.\nI will resize the cars image.', rows, columns, rowsb, columnsb);
uiwait(helpdlg(message));
carsImage = imresize(carsImage, [rowsb, columnsb]);
[rows, columns, numColors] = size(carsImage)
end
% Display images.
subplot(2, 3, 1);
imshow(MV);
impixelinfo;
title('Cars1 image', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(MV1);
impixelinfo;
title('Background image', 'FontSize', fontSize);
h1 = figure(1);
%Foreground Detection
thresh=11;
fr_diff = imabsdiff(carsImage, backgroundImage);
subplot(2, 3, 3);
imshow(fr_diff, []);
impixelinfo;
title('Difference image', 'FontSize', fontSize);
% Take the histogram.
subplot(2, 3, 4);
histogram(fr_diff, 128);
% Put a line where the threshold is
xline(thresh, 'LineWidth', 2);
grid on;
title('Histogram of Difference Image', 'FontSize', fontSize);
% Threshold to create a binary image (mask)
mask = fr_diff > thresh;
subplot(2, 3, 5);
imshow(mask);
title('Thresholded image', 'FontSize', fontSize);
But rather than resizing, which doesn't allow for a perfect subtraction as you can see, you should really investigate why your frames are different sizes than your background image. And not only are they different sizes, but the cameras seem to be pointed in different directions so the field of view is not the same between the two cameras.

  28 Comments

Image Analyst
Image Analyst on 14 Nov 2020 at 15:47
It counts the blobs, regardless of what they represent. You have to do better segmentation to make it count only the cars and nothing else. The Computer Vision Toolbox has car tracking capability. I don't. So look there for a solution.
Jeje Ahmad
Jeje Ahmad on 23 Nov 2020 at 10:24
@Image Analyst
hi i am use the code to detect in video but the number of cars not give me how i can count the cars in video
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 100);
videoReader = VideoReader('video.mp4');
for i = 1:200
frame = readFrame(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
figure; imshow(frame); title('Video Frame');
figure; imshow(foreground); title('Foreground');
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
figure; imshow(filteredForeground); title('Clean Foreground');
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 1500);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(result); title('Detected Cars');
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
release(videoReader); % close the video file

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by