Car detection using OpenCV

I am working on a small personal project where i have to know wether an image shown is a car or not. I am using the basic OpenCV python based template matching.

Positive Image / Template Image

Car Top View :-

Top view

The simple template matching by using one of the positive image on the other is giving the required result.

Negative Image

But when we are using negative images like

negative image

the template finder is finding this as a positive match.

Methods tried and failed :-

  1. Increase the threshold for template matching :- Increasing the threshold is causing many of the actual positive images to stop matching

  2. Different types of template matching techniques that are there in OpenCV :- Not giving any better result

  3. Using multiple templates to reinforce positive and negative matches:- Doesn't work well for all the cases

Is there a better way to remove the template matching false positives that we are getting. Are there any feature matching or edge detection based techniques that can be used instead of template matching to improve my algorithm.

  • 1
    $\begingroup$ What does negative in negative image refer to? Also I don't understand why you are not happy with a positive match: isn't there a car in the "negative image"? $\endgroup$
    – anderstood
    Sep 20, 2017 at 19:11
  • $\begingroup$ @anderstood sorry for being not clear on this one... the car should only be detected when the full car is present . In the negative case the cars are being detected even if only a small part of the car is under the sliding window. $\endgroup$
    Sep 21, 2017 at 8:42

2 Answers 2


I assume that the image have same scales in terms of sizes. With that out of the way, I would suggest try using the following:

  1. Break up your image into parts. (the template image as well as the test image. may be 3 or 4 according to your choice.)
  2. Then perform template matching on each part. Consider common threshold for every template part and then count it as positive only when all parts give a positive result.

(P.S. A similar solution was suggested on some other post. I don't exactly remember which post.)

Update: Partition your template and test images at exactly same points.


I drastically improved my matching algorithm by combining it with a histogram comparison.

My final matching probability is calculated like this:

probability = (matchingProbability * 2 + histProbability) / 3

Also I found that inverting images can improve template matching further.

this.tmplMeanStdDev = getMeanStdDev(tmpl)
const meanArr = tmpl.mean()
const mean = (meanArr.x + meanArr.y + meanArr.z) / 3

if (this.tmplMeanStdDev < 140) {
  // low contrast
  this.matchMethod = TM_CCOEFF_NORMED
} else {
  // normal contrast
  this.matchMethod = TM_SQDIFF_NORMED
  if (mean > 65) {
    // bright template image -> invert
    this.tmpl = this.tmpl.bitwiseNot()
    this.image = this.image.bitwiseNot()

I can recommend to set up a testing environment with a lot of input to check your results automatically. Then tweak the thresholds and parameters till you reach an optimum.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.