Cascade Classifier - detectmultiscale does not return MatOfRect objects

asked 2017-10-06 13:52:14 -0500

holdman gravatar image

I work on my android application as It mentioned here . I have difficulties with class Cascade Classifier. I use HAAR classifier. I load a trained cascade classifier in xml and then I use method detectMultiScale with parameters (Mat image, MatOfRect detectedObjects). But when I run the application, I always get detected 0 objects, even If I take a photo with an object in the image. I use Opencv 3.2.0. I tried also LBP classifier and still, it is the same.

I trained my classifier in this application . Does someone have the experience with this GUI training classifier? A type of input image is CV_8UC1.

I don't see the problem with loading xml because the statement below is running well, but without returning detected objects as it was mentioned. I haven't tried to train the classifier in the command line yet. But It would be bit of problem because I am on windows platform. But I'll try it.
Or did I miss something? Let me know.

private CascadeClassifier cascadeClassifier = new CascadeClassifier("/storage/emulated/0/Pics/cascade1.xml");
private MatOfRect matOfRect = new MatOfRect();

public void classifier(Mat object){
        if(!cascadeClassifier.empty()) {

                Imgproc.resize(object, object, new Size(170, 240));
                cascadeClassifier.detectMultiScale(object, matOfRect);

                MyLog.i(MyLog.TAG, matOfRect.size().toString());//It always prints size 1x0


The classifier was trained on this kind of object.

image description

edit retag flag offensive close merge delete


did you keep any logs from the training ?

there's a detectMultiScale() overload, where you can specify minNeighbours. start with 0, and see, if you get any false predictions, (then increase). if not so, throw away the classifier, and start all over.

berak gravatar imageberak ( 2017-10-07 01:59:03 -0500 )edit

I have logs from the training. I tried overloaded detectMultiScale but still, I usually get the same size of matOfRect 1x0 but sometimes I get the size 1x1 and there is a double array with a length of 4. I also tried to train the classifier with more negative images ( a little bit over 1000 images and 10 positive images). Previously I trained around 200 negative images and 10 positive images. It was trained for a purpose to detect one object which is above showed. But the result is the same. I attempt to use last overloaded method and still it is the same story.

I've found from the example OpenCV android. They load the classifier from computer to android by code. But I save the classifier into a phone and load from the phone. If this is not the problem.

holdman gravatar imageholdman ( 2017-10-07 09:56:07 -0500 )edit
  • sometimes I get the size 1x1 and there is a double array with a length of 4 -- yes, that shoulld be your rectangle.

  • "They load the classifier from computer to android by code." -- they have to, because the cascade is originally in the res folder, zipped in the apk. if you put it on your sdcard instead, you don't have to copy it. since you already get past the empty() check (good idea!), that's not the problem.

let's face it: something in your training went bad. we can't tell, without knowing more details (logs, again ?)

maybe there was too much variation in the positive images ? remember, it can only detect a single class of things.

berak gravatar imageberak ( 2017-10-07 10:10:02 -0500 )edit

oh wait, what about portrait vs landscape mode ? (your image is not square)

berak gravatar imageberak ( 2017-10-07 10:11:16 -0500 )edit

It is not square. A long time ago I have a problem with the camera.In OpenCV hasn't been solved the problem yet. So I hold the phone (in my hand) as portrait, and camera in the phone which is displayed on the screen as a landscape mode. Because I want the camera all over my screen. It is fixed. But all images are the same size as the image above. Or is it that problem with images are not square?

holdman gravatar imageholdman ( 2017-10-07 10:23:28 -0500 )edit

no, i was more guessing, it might be 90° rotated (which would be a problem with the cascade classifier)

berak gravatar imageberak ( 2017-10-07 10:27:43 -0500 )edit

Here is a log from latest HAAR training.

holdman gravatar imageholdman ( 2017-10-07 11:37:42 -0500 )edit

simply put: the log says it could not do any better on your data.

berak gravatar imageberak ( 2017-10-07 11:49:33 -0500 )edit

So what type of images should I use to train it? I've added images here to understand more. Or should I train with coloured images?

holdman gravatar imageholdman ( 2017-10-07 12:25:52 -0500 )edit

So the problem is only I used wrong images to train? :D

holdman gravatar imageholdman ( 2017-10-07 13:29:11 -0500 )edit