Ask Your Question

Identifying black cars with OpenCV in Python

asked 2016-05-23 19:50:02 -0500

marcosbontempo gravatar image


I'm using OpenCV in Python to identify cars in a parking lot. It works fine when the color of the car is different than black.

Here is my code. It basically applies a canny and counts the white points:

cv2.imshow("image", image)

# Gray scale and blur
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 5, 10, 10)
blur = cv2.GaussianBlur(gray,(5,5),0)        
cv2.imshow("blur", blur)

# Apply canny
edged = cv2.Canny(blur, 30, 200)
cv2.imshow("Edged", edged)

# Result
points = cv2.countNonZero(edged)

Here is the result for a white car:

white car

But the code doesn't work well with the shadows of the others cars:


And even worst with black cars:

black car

How do I fix this problem? Do I need to change my method? Maybe the canny isn't the best.

I'm using this image of the parking:


Any tip will be very helpful,


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-05-24 03:16:11 -0500

I'm assuming from your question, that you already know where are the parking spot. In that case, you could try to detect car with the OpenCV (see that question) . Then you could see if the detected car is in a parking spot or not (arriving, leaving, etc.).

Another solution, also knowing the parking spot, is to compare pixel by pixel the difference between a known empty spot and the current one. This will suffer from the illumination issue due to the sun, but could be counter-balanced with a Mixture of Gaussian for background learning, or multiple samples of the parking spot at different time of the day (it seems you have the hour on the top of the image).

edit flag offensive delete link more


Thanks for your answer!

Yes, I have each vacancy's ROI. As you suggested, I'm trying the haar cascade solution, according to this link:

But, in the haar cascade training, I realized that it's converging too soon:

===== TRAINING 1-stage =====

| N | HR | FA |

| 1| 1| 1|

| 2| 1| 0.3333|

Do you know what is wrong? I assured that the width x heigh ratio is the same of the images. I'm using this command:

opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.95 -maxFalseAlarmRate 0.5 -numPos 1000 -numNeg 600 -w 50 -h 30 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048

Am I missing any parameter?

marcosbontempo gravatar imagemarcosbontempo ( 2016-05-25 07:02:19 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-05-23 19:45:31 -0500

Seen: 826 times

Last updated: May 24 '16