Cars detection in image

51 views (last 30 days)
Joselyn  Jok
Joselyn Jok on 2 May 2017
Commented: Johanphilip Davis on 28 Oct 2021
I'm working on cars detection project and able to detect few cars. However, I want to at least detect 80-90% of the cars. This is my codes that I've been working on.
%codes
clc;
close all;
clear all;
%image acquisition
f=imread('Cars.jpg');
f=imresize(f,[800 NaN]); % image loading unit
figure (1)
imshow(f)
g=rgb2gray(f);
g=medfilt2(g,[5 5]);
figure (2)
imshow (g)
% morphological image processing
conc=strel('disk',5);
gi=imdilate(g,conc);
conc1=strel('disk',5);
ge=imerode(gi,conc1); % morphological image processing
gdiff=imsubtract(gi,ge);
gdiff1=mat2gray(gdiff);
figure (4)
imshow (gdiff1)
gdiff2=conv2(gdiff1,[1 1;1 1]);
figure (5)
imshow (gdiff2)
gdiff3=imadjust(gdiff2,[0.4 0.9],[0 1],1);
figure (6)
imshow (gdiff3)
B=logical(gdiff3);
[a1 b1]=size(B);
figure(7)
imshow(B)
er=imerode(B,strel('line',60,8));
figure(8)
imshow(er)
out1=imsubtract(B,er);
F=imfill(out1,'holes'); %filling the object
H=bwmorph(F,'thin',0.5);
H=imerode(H,strel('line',8,55));
figure(9)
imshow(H)
%Normalization% & Object Recognition
I=bwareaopen(H,floor((a1/18)*(b1/18)));
I(1:floor(.9*a1),1:2)=1;
I(a1:-1:(a1-20),b1:1:(b1-2))=1;
figure(10)
imshow(I)
%Cars detection in image
figure (11)
imshowpair (f,I)
%Create bounding box on detected cars
Iprops=regionprops(I,'BoundingBox','Image');
hold on
text(8,785,strcat('\color{green}Cars Detected:',num2str(length(Iprops))))
hold on
for n=2:size(Iprops,1)
rectangle('Position',Iprops(n).BoundingBox,'EdgeColor','g','LineWidth',2);
end
hold off
And I get this output:
Where did I do wrong? Can anyone correct the codes? My due date will be up soon. I appreciate if anyone could help asap. Thank you.
This is my original image:

Answers (4)

Image Analyst
Image Analyst on 2 May 2017
Well. . . . all kinds of stuff. Did you notice how you were only getting light colored cars and no dark cars? That's because you did a morphological closing (dilation then erosion) which expands the white bars. And other things.
What I'd do is to see if you can get a totally empty parking lot image and then subtract them. If you can't then I'd find the asphalt by converting to hsv color space and looking for pixels that have low saturation and low value - basically find out the color of the asphalt. Then I'd get the color difference between all pixels in the image and the gray asphalt color. You can do this in RGB or LAB color space. Things with a high color difference are either cars or grass. But you can do size filtering because you know that no car will ever be as big as a lawn so use bwareafilt() to remove grass. Grass has high saturation and a hue in the green region and a size larger than the known size of a parking space. So with the grass and asphalt gone, now all you have is cars. But there is still the problem of dark cars because they are close to the color of dark asphalt. There again you'll have to use size information - cars are going to be no larger than the area of a parking spot so you can get rid of large dark asphalt areas. You could also look at the Solidity (returned by regionprops). With cars it will be close to 1 and for weird-shaped asphalt blobs it won't be close to 1.
  15 Comments
Nur Farah Aqilah Mohd Fazli
where can i put all the changing coding in your starting coding? i did not understand
Image Analyst
Image Analyst on 11 May 2019
You make two functions createMaskAsphalt(), and createMaskGrass() with different thresholds. You can put them at the end of your main m-file, or have them be separate m-files.
You can determine the thresholds and create the functions by using the "Color Thresholder" app on the Apps tab of the tool ribbon.

Sign in to comment.


Rabious Seajon
Rabious Seajon on 11 Feb 2018
Edited: Image Analyst on 11 Feb 2018
I=bwareaopen(H,floor((a1/18)*(b1/18)));
I(1:floor(.9*a1),1:2)=1;
I(a1:-1:(a1-20),b1:1:(b1-2))=1;
figure(10)
imshow(I)
how it works wold u please tell me sir?
  2 Comments
Image Analyst
Image Analyst on 11 Feb 2018
It first removes blobs smaller than floor((a1/18)*(b1/18)) pixels in area. Then it sets two rectangular blocks to 1 (white, true). Finally it brings up a figure with label 10 and displays the modified binary image.
sneha madda
sneha madda on 11 Jun 2019
Thanks a lot, it's working fine but I printed number of vehicles by adding these lines of code after for loop.
result = sprintf('Number of cars: %d.',n-1);
disp(result);% display number of cars
hold off

Sign in to comment.


Jeje Ahmad
Jeje Ahmad on 17 Oct 2020
Hi , Can i take this code
please

Jeje Ahmad
Jeje Ahmad on 17 Oct 2020
@joselyn jok
@Image Analyst
  9 Comments
Johanphilip Davis
Johanphilip Davis on 28 Oct 2021
Thank you for sharing

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!