You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to detect free spots in a parking area?
10 views (last 30 days)
Show older comments
Hello All,
I am working on a project which is to detect vacant spots in a park. I have a row of 10 spots on a small model just like in the images attached here:
I want to write a program that tells the webcam to take an image and when there is motion, take another image and compare the two to tell how many spots are available.
Please provide a source code with any explanation that you know will aid me.
Thank you.
4 Comments
manpreet singh
on 17 May 2014
Hello Hazha,
I am working on the same project, can you please share your code with me. I am looking for an idea.
Kind Regards.
karthik devaraj
on 28 Mar 2018
Hello have you done this ? Kindly help me win your code to detect car parking please
Hazman Mat Husin
on 22 Nov 2018
Hi hazha, im from uthm and my final project is about this.. can i get the coding please?if you there please email me at hazmanmathusin@yahoo.com
Pratiksha Pradhan
on 7 Oct 2020
Hello, I am doing something similar for an undergraduate school project. Could you please share your code for reference?
Accepted Answer
Image Analyst
on 20 Apr 2014
See my attached demo that detects motion in a video. You'll just have to replace read() with getsnapshot(), or snapshot if you're using the new webcam add-on introduced with R2014a.
To see if a parking spot is taken, just compare the color in the known locations to black. Do it for every color channel.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get means in the rectangle of this parking place.
redMean = mean2(redChannel(row1:row2, col1:col2));
greenMean = mean2(greenChannel(row1:row2, col1:col2));
blueMean = mean2(blueChannel(row1:row2, col1:col2));
% Set a flag to say if it's taken or not.
taken = abs(redMean - redBlack) > tolerance & abs(greenMean- greenBlack) > tolerance & abs(blueMean - blueBlack) > tolerance;
Of course you have to know row1, row2, col1, and col2 for every location that you want to inspect, but that's no problem. Then repeat the above code for every parking spot. You might make taken be an array.
You should check Vision Bibliography. There are published papers on this. I assume this is an undergraduate project so you might be able to get away with just copying known methods. If it's a graduate project, then you're going to have to invent something totally new to the world or else improve on the methods that have already been published.
Huang, C.C.[Ching-Chun], Wang, S.J.[Sheng-Jyh],
A Hierarchical Bayesian Generation Framework for Vacant Parking Space Detection,
CirSysVideo(20), No. 12, December 2010, pp. 1770-1785.
Suhr, J.K.[Jae Kyu], Jung, H.G.[Ho Gi],
Sensor Fusion-Based Vacant Parking Slot Detection and Tracking,
ITS(15), No. 1, February 2014, pp. 21-36.
Suhr, J.K.[Jae Kyu], Jung, H.G.[Ho Gi], Bae, K.[Kwanghyuk], Kim, J.[Jaihie],
Automatic free parking space detection by using motion stereo-based 3D reconstruction,
MVA(21), No. 2, February 2010, pp. xx-yy.
and so on.
37 Comments
Hazha
on 20 Apr 2014
Thanks a lot @Image Analyst for your answer. You are correct! This is an undergraduate project.
I am sorry, but I do not see the attachment!
Also, there is no need to compare two images with each other, correct? We are comparing the color of the cars with the color of the pavement?
Plus, how will my loop look like?
Thanks again.
Image Analyst
on 20 Apr 2014
Sorry. Here's the file. No you don't need to compare images themselves, just the color values extracted from small rectangles that are the parking spaces.
You would define the starting rows and columns for each space (arrays called rows and columns), and a width and height, say 20 and 30 pixels respectively. Then do
rgbImage = getsnapshot();
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
for ps = 1 : 10 % Loop over 10 spaces
row1 = rows(ps);
row2 = row1 + 30; % or whatever the height is.
col1 = columns(ps);
col2 = col1 + 20; % or whatever the width is.
% Get means in the rectangle of this parking place.
redMean = mean2(redChannel(row1:row2, col1:col2));
greenMean = mean2(greenChannel(row1:row2, col1:col2));
blueMean = mean2(blueChannel(row1:row2, col1:col2));
% Set a flag to say if it's taken or not.
taken = abs(redMean - redBlack) > tolerance & abs(greenMean- greenBlack) & tolerance & abs(blueMean - blueBlack) > tolerance;
end
Hazha
on 20 Apr 2014
Edited: Image Analyst
on 20 Apr 2014
@Image Analyst
A couple of questions:
- I ran the above code and all I get is a lot of numbers and an error at the end(see attachment).
- I ran the code you attached, but is there a small program to tell the camera:if there is motion, take an image, tell me how many spots are available?
- In case there is a black colored car, how should we deal with that? since the color of my pavement is black also?
Please explain with code, if possible.
Thank you.
Image Analyst
on 20 Apr 2014
There is no attachment. What code did you run? Did you see how I subtracted one frame from the prior frames? Did you do the same thing in your code?
If it's a black car that will tell you how large the smallest tolerance needs to be. If you're getting a lot of false alarms (says a car is there when there's not), then you might have to either raise the tolerance or also check texture. The texture of the ground will be different. for example do an edge detection and measure that also. Smooth ground will not have many edges but a car will, even a black car.
Image Analyst
on 21 Apr 2014
You can use stdfilt() or entropyfilt() to do texture filtering.
Image Analyst
on 21 Apr 2014
No I didn't have time. And I need to leave again soon for an errand. I'll be gone a few hours. You could have saved some time by posting the error.
Image Analyst
on 21 Apr 2014
Hazha when people post code you need to think about what they post and see where it needs to be modified. Just because in my sample code I used the variable name of "rgbImage" does not mean that you also used that exact name. What did you call your image? Maybe you called it originalImage or something similar, or worst case some single letter like I or X or A or some bad choice like that. What did you use? Replace my "rgbImage" with whatever you used. That's what people do when people post sample code for them - they adapt it.
Image Analyst
on 21 Apr 2014
You have weird characters in there that aren't apostrophes. I fixed them for you:
vid = videoinput('winvideo', 1, 'YUY2_320x240');
dhia jamaa
on 18 May 2015
Hello to all. i am working about the same idea. my problem is, for an image with a certain number of parking spots, how to locate each spot and to know their coordinates. any idea?? thanks in advance
Zain Rajani
on 8 Dec 2016
what is redBlack, greenBlack and blueBlack in the code mean? and what is it use for? please tell me
Thank you.
Image Analyst
on 8 Dec 2016
It tells you how much of a difference there is between the color of the black asphalt (or concrete) and what is in the picture (like a car), so you can tell if the parking space is empty or if there is a vehicle there.
Walter Roberson
on 8 Dec 2016
Image Analyst: the problem was that your code has
taken = abs(redMean - redBlack) > tolerance & abs(greenMean- greenBlack) > tolerance & abs(blueMean - blueBlack) > tolerance;
without defining redBlack, greenBlack, blueBlack .
Zain Rajani
on 8 Dec 2016
Yes please define what is RedBlack, greenblack and blueblack in your code. Please
Image Analyst
on 8 Dec 2016
I don't know what they are. You have to measure what they are for your blank (no car) image. Look in the parking space and measure the RGB value of the parking space with no car in there. That's what you should pick/use for the values. Perhaps it's 50,39,45 -- I don't know. Measure it with imtool in MATLAB, or with Photoshop or whatever, then use those values in your code.
Zain Rajani
on 8 Dec 2016
So you mean to say that I need to measure the red mean, blue mean and green mean for image with no cars and substitute this in place of redBlack and so on?
Image Analyst
on 8 Dec 2016
Yes. And if the values vary from one parking spot to the next, or from one time of day to the next, then you might need a whole array of color values so you can use the right color in the right place at the right time. It can get more complicated. For example, do you think the color changes with weather? Sure it does. Not only might it be brighter or darker, like if an overhead lamp turns on, but the color of the light may change, like from noon to sunset to overcast to cloudy, etc. Perhaps you need to also make another measurement that says how uniform the value is. Like if it's an empty parking spot the value over the spot may have a low standard deviation but if a car is there the St Dev might be higher because of windows, bumpers, door handles, etc.
Walter Roberson
on 8 Dec 2016
And reflections from the adjacent cars makes a difference too.
Zain Rajani
on 9 Dec 2016
How to get the tolerance if I assume that weather conditions and light remains the same?
Image Analyst
on 9 Dec 2016
After examining the parking place ROI to find the mean and standard deviation of the colors, try some values and see if they work?
Zain Rajani
on 9 Dec 2016
Can you please tell me how to count the number of cars present in an image?
Image Analyst
on 9 Dec 2016
Inspect every parking space seeing if the image of it matches features for an empty one. If a particular parking spot is not empty, set some element of some array that represents that parking lot to "true". Then to count all the cars there, simply sum your parking lot array:
numberOfCarsParked = sum(parkingLotArray(:));
Cristian Cuenca Moreno
on 26 Feb 2022
Hello,
I'm doing a project just like yours and I can't get it to draw just one x or a circle in each parking space, but it draws several, do you know how I could fix this problem?
Image Analyst
on 26 Feb 2022
@Cristian Cuenca Moreno I think you must have modified the script. Attach the actual m-file you are using.
Cristian Cuenca Moreno
on 3 Mar 2022
¿Could the mask be calculated from the image of the empty parking lot?
Image Analyst
on 3 Mar 2022
Sure. I just took an image that had cars in it, but that doesn't matter, and painted over the spaces. The spaces were white and elsewhere was black. I then saved it as a gray scale mask image.
Cristian Cuenca Moreno
on 6 Mar 2022
Could you tell me some code or idea that allows me to make such a mask?
Image Analyst
on 6 Mar 2022
For example, if you don't have Photoshop you can use the open source GIMP or any other paint program.
Cristian Cuenca Moreno
on 6 Mar 2022
Sorry if I haven't explained it very well. I was referring to how to calculate it using matlab.
Walter Roberson
on 6 Mar 2022
"¿Could the mask be calculated from the image of the empty parking lot?"
NO.
At least not without using a large source of external information for Deep Learning to be able to figure out by calculation what the "meaning" of each pixel is. Is that narrow spot marked out with lines motorcycle parking? Is it there to prevent people from blocking a utility closet? Is it there because the water flow is designed to fall there? Is this spot double-width because it is a handicap spot allowing for vans with ramps and that is marked on the wall? Is this spot double-width because the markings have faded?
You cannot reliably calculate the mask without a lot of real-world knowledge. But as a human looking at a set of photographs, you can probably draw in the boundaries fairly well.
Image Analyst
on 6 Mar 2022
Just to build on what Walter said, I'm sure you'd spend way more time trying to develop an image processing program to identify spaces than if you just hand drew them. I mean, why spend days or weeks trying to develop a program to automatically identify spaces when you can hand draw them in a few minutes? If you have a parking lot image, just upload it and I can give you back the mask with just a few minutes work.
Cristian Cuenca Moreno
on 7 Mar 2022
Ok, thaks
My objective was to build a code trained to do it itself whit some lines but if its very tedious, its better to draw it
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)