필터 지우기
필터 지우기

Cars detection in image

조회 수: 9 (최근 30일)
Joselyn  Jok
Joselyn Jok 2017년 5월 2일
댓글: Johanphilip Davis 2021년 10월 28일
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:

답변 (4개)

Image Analyst
Image Analyst 2017년 5월 2일
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
Nur Farah Aqilah Mohd Fazli
Nur Farah Aqilah Mohd Fazli 2019년 5월 11일
where can i put all the changing coding in your starting coding? i did not understand
Image Analyst
Image Analyst 2019년 5월 11일
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.

댓글을 달려면 로그인하십시오.


Rabious Seajon
Rabious Seajon 2018년 2월 11일
편집: Image Analyst 2018년 2월 11일
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
Image Analyst
Image Analyst 2018년 2월 11일
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 2019년 6월 11일
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

댓글을 달려면 로그인하십시오.


Jeje Ahmad
Jeje Ahmad 2020년 10월 17일
Hi , Can i take this code
please

Jeje Ahmad
Jeje Ahmad 2020년 10월 17일
@joselyn jok
@Image Analyst
  댓글 수: 9
Image Analyst
Image Analyst 2021년 10월 28일
Again, it doesn't work for video unless you just process a frame at a time. MATLAB has tracking that works with cars:
Johanphilip Davis
Johanphilip Davis 2021년 10월 28일
Thank you for sharing

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by