A
A
Andrey2017-01-31 14:16:28
Python
Andrey, 2017-01-31 14:16:28

How to deal with false positive results in OpenCV?

Guys, I'm trying to set up OpenCV so that I can find the right product in the photo. But I ran into the problem of false positives (supposedly shows the borders of the product in the place where it does not exist).
For example, there is such a photo of a mouse:
9b6cf859e04749319328566c84334da8.jpg
Based on it, I make my own haar cascade.
Then, using this code, I try to find the mouse in the photo:

import cv2
import sys

imagePath = sys.argv[1]
cascMouse = 'logitech_cascade.xml'
mouseCascade = cv2.CascadeClassifier(cascMouse)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

mouses = mouseCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(100, 100),
)

for (x, y, w, h) in mouses:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 255), 2)

cv2.imshow("Electronic items detector", image)

I get the following result:
d4e64188dfb34120b1bad1c80d7e7b3d.png
We see false positives. I'm trying to play with the parameters of detectMultiScale()
mouses = mouseCascade.detectMultiScale(
    gray,
    scaleFactor=1.5,
    minNeighbors=10,
    minSize=(100, 100),
)

The result gets better:
1c89d095bbe74299b1abc8fc7d59a650.png
Trying to find the same object in another photo:
51758791415348d59772f00231311702.png
With the same settings, it falsely detects the area where there is no object and does not detect the area where the same object is.
What is the best way to configure OpenCV? I am sure that I am doing something wrong and most likely I am missing some points.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question