Why does OpenCV recognize the object only in training images? [closed]

asked 2016-02-02 07:03:17 -0500

Lory gravatar image

updated 2016-02-03 10:33:51 -0500

In order to make my iOS app recognize 1€, 2€ and 0.50€ coins I have been trying to use opencv_createsamples and opencv_traincascade to create my own classifier.xml. So, I cropper 60 images of 2€ coins from a short video like the following:

image description

Then, I combined them with random backgrounds using opencv_createsamples. I obtained 12000 images similar to this:

image description

and I ran the following commands:

opencv_createsamples -img positives/i.jpg -bg negatives.txt -info i.txt -num 210 -maxidev 100 -maxxangle 0.0 -maxyangle 0.0 -maxzangle 0.9 -bgcolor 0 -bgthresh 0 -w 48 -h 48 (for i from 0 to 60)

cat *.txt > positives.txt

opencv_createsamples -info positives.txt -bg negatives.txt -vec 2.vec -num 12600 -w 48 -h 48

opencv_traincascade -data final -vec 2.vec -bg negatives.txt -numPos 12000 -numNeg 3000 -numStages 20 -featureType LBP -precalcValBufSize 2048 -precalcIdxBufSize 2048 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -w 48 -h 48

Training stopped at 13-th stage. Once I got a cascade.xml I tried it at once (with detectMultiScale()) on a simple image taken by my smartphone but nothing is detected:

image description

while if I give as input one of the images used as traning, then it works very good:

image description

I can't really understand why this is happening and it's driving me insane, most of all because I have been trying to make it work for weeks...would you please tell me where I am making the mistake?

The short program I wrote is here:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**) {

Mat src = imread("2b.jpg");

Mat src_gray;

std::vector<cv::Rect> money;

CascadeClassifier euro2_cascade;

cvtColor(src, src_gray, CV_BGR2GRAY );
equalizeHist(src_gray, src_gray);

if ( !euro2_cascade.load( "cascade.xml" ) ) {
    printf("--(!)Error loading\n");
    return -1;

euro2_cascade.detectMultiScale( src_gray, money, 1.1, 3, 0|CASCADE_SCALE_IMAGE/*CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE*/, cv::Size(10, 10),cv::Size(2000, 2000) );
printf("%d\n", int(money.size()));

for( size_t i = 0; i < money.size(); i++ ) {
    cv::Point center( money[i].x + money[i].width*0.5, money[i].y + money[i].height*0.5 );
    ellipse( src, center, cv::Size( money[i].width*0.5, money[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

namedWindow( "Display window", WINDOW_AUTOSIZE );
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-26 07:52:34.713039



cascade classifiers are not rotational invariant. (your whole idea probably won't work)

berak gravatar imageberak ( 2016-02-02 07:15:31 -0500 )edit

Ok. So please would you tell me which is the best way to proceed? Because I keep on making mistakes and I don't know how to improve...

Lory gravatar imageLory ( 2016-02-02 07:20:31 -0500 )edit

Why not tying the feature homography if only one object?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-02 08:22:31 -0500 )edit

coins also have a backside.

berak gravatar imageberak ( 2016-02-02 09:33:36 -0500 )edit

then use 2 images... :p

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-02 10:24:17 -0500 )edit

I'm using OpenCV 3 so I downloaded opencv_contrib for my version and tried to compile xfeatures2d module but I get a CMake error in CMakeList file...moreover xfeature2d is not even present inside opencv framework for xcode so even if I made it work on my pc then I could not use it in ios anyway...

Lory gravatar imageLory ( 2016-02-02 12:51:09 -0500 )edit

@berak and @Lory, OpenCV cascade classifiers on its own are not rotation invariant, BUT it is possible to make them rotation invariant by creating a rotated image matrix in the third dimension. It is all discussed inside the OpenCV 3 Blueprints book, chapter 5.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-02-04 05:51:22 -0500 )edit

^^ lol, yea, just reading that ;)

berak gravatar imageberak ( 2016-02-04 05:56:09 -0500 )edit

@StevenPuttemans thank you for your kind and useful reply. Anyway I've just discovered that it won't solve the problem because I've just tried to detect a straight coin (and not rotated like the one in my question) but it's not detected...so the problem is upstream

Lory gravatar imageLory ( 2016-02-04 06:01:37 -0500 )edit

@Lory, look at all the other topics, and concise them in 1 ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-02-04 07:14:36 -0500 )edit