3

I trained my pc with opencv_traincascade all one day long to detect 2€ coins using more than 6000 positive images similar to the following:

enter image description here

Now, I have just tried to run a simple OpenCV program to see the results and to check the file cascade.xml. The final result is very disappointing:

enter image description here

There are many points on the coin but there are also many other points on the background. Could it be a problem with my positive images used for training? Or maybe, am I using the detectMultiScale() with wrong parameters?

Here's my code:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**) {

  Mat src = imread("2c.jpg", CV_LOAD_IMAGE_COLOR); 

  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( "/Users/lory/Desktop/cascade.xml" ) ) {
     printf("--(!)Error loading\n");
     return -1;
  }

  euro2_cascade.detectMultiScale( src_gray, money, 1.1, 0, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, cv::Size(10, 10),cv::Size(2000, 2000) );

  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 );
  imwrite("result.jpg",src);
}

I have also tried to reduce the number of neighbours but the effect is the same, just with many less points... Could it be a problem the fact that in positive images there are those 4 corners as background around the coin? I generated png images with Gimp from a shot video showing the coin, so I don't know why opencv_createsamples puts those 4 corners.

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 1
    See my answer in this question. if you have 6000 images, then you could try hog descriptors from dlib. http://stackoverflow.com/questions/34344825/object-detection-and-isolation-using-opencv/34359260#34359260 – TruckerCat Jan 29 '16 at 16:17
  • @R_Valdez Thank you for your kind and interesting comment. Is there any simple tutorial you can link me? Because I'm very new to all this and I get confused at once... – SagittariusA Jan 29 '16 at 16:29
  • I don't know any tutorial for this. But the dlib example code is very well commented. It's nearly like an tutorial. I recommend to you, that you download dlib and play whith the example code. Also you shoud read the blogpost it's very helpfull (http://blog.dlib.net/2014/02/dlib-186-released-make-your-own-object.html) – TruckerCat Jan 29 '16 at 16:36
  • you should not use the whole positive images but only the coin with only some little background around it (since the coin is round, or use only the interior-rectangle of the coin for training) – Micka Jan 29 '16 at 17:41
  • Not sure but did you take a single coin photo and just added this (same image) to many different background to use them as positive images? Instead you should get lots of different images of coins-only (slightly different angles for example, different cameras etc) and use those as positive images. – Micka Jan 29 '16 at 17:43
  • have a look at this http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html and how he cropped the images around the bananas – Micka Jan 29 '16 at 17:47
  • I got 100 images/frames with a single 2€ from short video I made with my iphone. I then used VideoCapture to get the images. Then I combined them with more than 3000 background images using command opencv_createsamples. I strictly followed this tutorial: http://www.memememememememe.me/training-haar-cascades/ – SagittariusA Jan 29 '16 at 17:48
  • @Micka Ok, I've understood what you say. I don't know what to think. All other tutorials said I have to combine coin images with random background and those results are used as positives... :( – SagittariusA Jan 29 '16 at 17:53
  • @Micka if you scroll down the tutorial you have linkes, you'll see that that guy does the same by combining the cropped banana images with random background. Just as I do... – SagittariusA Jan 29 '16 at 17:56

2 Answers2

3

Those positive images are just plain wrong

The more "noise" you give your images on the parts of the training data then the more robust it will be, but yes the longer it will take to train. This is however where your negative sampels will come into action. If you have as many negative training samples as possible with as many ranges as possible then you will create more robust detectors. You need to make sure your positive images only have your coins in, all your negative images have everything but coins in them

I've seen a couple of your questions up to now & I think you want to detect three different types of euro coins. You would be best training three classifiers all on those different coins and then running all three on your images.

I think also you are missing a key piece of knowledge on how HAAR works (or LBP or whatever) effectively it creates a set of "features" from your positive images then tries to find those features in the images you run the classifier over. It creates these features by working out what is different between your positive images and your negative images. You don't want anything that isnt going to be the thing you are trying to detect in your positive images.

Edit 1 - An Example

Imagine creating a classifier for a road stop sign, which is a similar detection to coins. It's big, it's red & it's hexagonal. Creating a classifier for this is relatively easy - as long as you don't confuse the training stage with erroneous data.

Edit 2 - Scaling of images:

You have to also remember that when running the detection stage it takes your classifier and starts small and then scales up. Large, obvious features will get detected quicker - in my previous example big red blobs & hexagonal shapes. It would then start on small features i.e. text, or numbers.

Edit 3 - a much better example

This example shows you really well how training a cascade object detector works. In fact it even has the same example as with a stop sign!

GPPK
  • 6,546
  • 4
  • 32
  • 57
  • I'm sorrt if I insist in saying this but I'm really confused: in many tutorials I have read that positive images are random background plus the coin. Noe you say that I must use only the coin as positives... So what shall I do? Please explain me as you would talk to a stupid because I'm reslly confused. – SagittariusA Jan 29 '16 at 18:27
  • 2
    Those tutorials are incorrect. You want your positive images to be only the thing you want to detect and then your negative samples to include *everything else* (or at least as much as practically possible – GPPK Jan 29 '16 at 18:28
  • I've updated my answer to give some more background knowledge and a better tutorial – GPPK Jan 29 '16 at 18:33
  • Thank you for you kind reply. very interesting... I'm afraid I won't learn everything in one night but I have very little time. Thus, shall I lauch `opencv_traincascade` where positive images are ONLY with coins, shall I not? – SagittariusA Jan 29 '16 at 18:35
  • Yeah, positive samples with only the thing you want to detect, negative samples with everything else. Learn and live by that phrase – GPPK Jan 29 '16 at 18:36
  • 1
    Thank you GPPK...I'll launch computation at once and hope it'll will be fine. I'll study your documentation, also because I have to write some state of art in my thesis...You've given my my job for the weekend. I'll let you know. Thank you from heart for now. – SagittariusA Jan 29 '16 at 18:37
  • the last thing! When I launch this command `opencv_traincascade -data final -vec 2.vec -bg negatives.txt -numPos 12000 -numNeg 3000 -numStages 20 -featureType HAAR -precalcValBufSize 2048 -precalcIdxBufSize 2048 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -w 48 -h 48 -mode ALL` there'no reference to positives.txt...thus, positives images are referenced by inside the .vec file, right? – SagittariusA Jan 29 '16 at 18:53
  • Yeah the vec is created by the other tool and they are in there – GPPK Jan 29 '16 at 18:54
  • I'm sorry but I don't really understand...I have positives.txt which contains a list of all images only with a coin and they are 60. Then I have 12000 samples images which are random background images with a coin (created by `opencv_createsamples`)...so, how shall I generate this .vec file? please write the command because I don't really understand – SagittariusA Jan 29 '16 at 19:20
  • Everywhere I read that vec file is created from samples but you said it's not like that, it's created only by positives (images with only a coin)... confused :( – SagittariusA Jan 29 '16 at 19:23
  • @GPPK what about the positive samples' sizes? Is it better or even necessary to provide them all in the same scale/size? For example the minimum detectable size? – Micka Jan 30 '16 at 10:18
  • @GPPK with this command `opencv_createsamples -vec i.vec -w 48 -h 48 -num 210 -img ./positives/i.jpg -maxidev 100 -maxxangle 0 -maxyangle 0 -maxzangle 0.9 -bgcolor 0 -bgthresh 0` (for i from 0 to 60) I created a vec file considering only positive cropped images of the coin (without any random background)...is that what you meant? – SagittariusA Feb 03 '16 at 20:32
  • Obviously I merged all .vec file into one – SagittariusA Feb 03 '16 at 20:40
1

To detect image of euro coin you can use several methods:

1) Train OpenCV cascade (HAAR or LBP). Don't forget use the great amount of false images. Also extend image of coin (add border).

2) Estimate image with abs gradients of original image. Use Hough Transform to detect circles (coin has shape of circle).

Maxim
  • 33
  • 5
  • As I said in mu question I did train OpenCV cascade with many images (6000 positives and more then 3000 negatives) and computation took very long time, but that is the result...I can't explain myself why there are so many points outside the coin... – SagittariusA Jan 29 '16 at 13:57
  • Effectiveness of HAAR classifier is strongly depends from training options. For examples, my options to detect license plates: -featureType HAAR -numStages 16 -minhitrate 0.999 -maxFalseAlarmRate 0.5 -numPos 884 -numNeg 1260 -w 24 -h 24 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048 – Maxim Jan 29 '16 at 14:03
  • So, it does not depend on positive images does it? I mean the fact there are those 4 corners around the coin...I will try another training with the options you have written...thank you – SagittariusA Jan 29 '16 at 14:05
  • The coin's image mustn't touch edge of the image (in train sample). – Maxim Jan 29 '16 at 14:07
  • sorry, all this is very new to me...what do you mean? – SagittariusA Jan 29 '16 at 14:09
  • Try to use LBP - it is much faster (for train and for work). – Maxim Jan 29 '16 at 14:10
  • just yesterday I tried to run my program with a `cascade.xml` generated by LBP but it did not work. I did not get ANY positive point on my photo, it was completely empty :( – SagittariusA Feb 01 '16 at 06:57