フィルターのクリア

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 Center および File ExchangeImage 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