2

i've been trying to write an app that recognizes the cap of a pencil (in this case).

i've been taking about 40 pictures of the object, and 635 pictures without the object. then i've been using the perl script "createsamples.pl" to generate 3000 positive images from the 40 positive pictures with this command (i also tried to generate 1500 images, same result):

createsamples.pl p.dat n.dat output

p.dat is the file with the names of the 40 positive images, n.dat is the file with the names of the 636 negative images.

i've been merging the created vector files into one vector file, then i've been trying to start the training like this:

opencv_traincascade -data cascades -vec samples.vec -bg n.dat -numPos 1000 -numNeg 635 -numStages 1 -w 20 -h 20 -featureType LBP

this finished in a few seconds, but gave me countless false detections when i tried it in my app.

so i tried again with more stages, up to 20.

===== TRAINING 19-stage =====
BEGIN
POS count : consumed   2000 : 2302
NEG count : acceptanceRatio    635 : 0.000145818
Precalculation time: 1.487
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|    0.997| 0.829921|
+----+---------+---------+
|   3|    0.997| 0.829921|
+----+---------+---------+
|   4|   0.9985| 0.856693|
+----+---------+---------+
|   5|   0.9965| 0.694488|
+----+---------+---------+
|   6|   0.9955| 0.645669|
+----+---------+---------+
|   7|   0.9955|      0.6|
+----+---------+---------+
|   8|    0.996| 0.579528|
+----+---------+---------+
|   9|   0.9955| 0.544882|
+----+---------+---------+
|  10|   0.9955| 0.540157|
+----+---------+---------+
|  11|   0.9955| 0.359055|
+----+---------+---------+
END>

on the this stage, i still got false detections, not that many anymore, but they are still there.

so i tried more stages, up to 30. but on the 21st stage training, i already got this acceptance ratio:

===== TRAINING 20-stage =====
BEGIN
POS count : consumed   2000 : 2320
NEG count : acceptanceRatio    635 : 9.72903e-005
Precalculation time: 1.493
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|    0.997| 0.831496|
+----+---------+---------+
|   3|    0.997| 0.831496|
+----+---------+---------+
|   4|    0.999| 0.779528|
+----+---------+---------+
|   5|   0.9955|  0.64252|
+----+---------+---------+
|   6|   0.9965| 0.694488|
+----+---------+---------+
|   7|   0.9955| 0.689764|
+----+---------+---------+
|   8|   0.9955| 0.628346|
+----+---------+---------+
|   9|    0.996| 0.645669|
+----+---------+---------+
|  10|   0.9965| 0.541732|
+----+---------+---------+
|  11|   0.9955|  0.43622|
+----+---------+---------+
END>

this acceptance ratio means that the classifier is overtrained, right? atleast thats what people mentioned on stackoverflow on a few questions.

im using a videocapture and hold the object (cap of pencil) in front of the camera in order to detect it. in most of the frames it still gets detected, but any other object that lies on the same background as the cap is getting detected aswell.

the 40 positive pictures look like that:

https://i.stack.imgur.com/7H5XM.jpg

10 of them were taken on a gray background, 10 were taken on a red background, 10 were taken on a silver background, 10 were taken with the cap lying on my keyboard.

so all in all, my questions:

1) are the 40 pictures i am using ok to use? or are the supposed to have no background in them at all? because right now, when im pointing my videocapture at the red background i was using before, then i get false detections. example of what happens (REMOVE SPACE PLEASE): http: //i.imgur.com/w0szcTh.jpg?1

2) am i totally on the wrong way to reach my goal?

3) the best tutorial i found about what im trying was this one: http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html his pictures show background aswell, but it seems he does not get false detections. do you know what else could be wrong with my approach?

thanks in advance guys, i hope any of you can help me out here.

user2677466
  • 139
  • 1
  • 13

1 Answers1

0

I think, that for this problem, color detection+contours finding should be more effective way. Also take a look at http://code.google.com/p/cvblob/ .

Haar cascades works with grayscale images and your object have not many visual features allowing to distinct it from other objects, that's why your detector has a lot of false detectoins.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • thanks for your answer @Andrey Smorodov. the attempt with the cap is just an example for now, my main purpose will be to detect different types of buttons, for example in a car. many of them will have the same shape and color, but most of them will have any sign or text on them, so that would give them visual features, right? would you think that my current aproach is the right thing for detecting such buttons? also, do you have any hints if i made any mistakes within the training process so far? thanks again :) – user2677466 Aug 21 '13 at 16:47
  • In such case, may be it'll be better to use template matching. It works good if image of object of interest is aways the same. You must have database with images of elements and then for each element you run template match function. http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html it also uses color information. Also I think, that HOG+SVM sould work well for this task. – Andrey Smorodov Aug 21 '13 at 17:52
  • thanks again @Andrey Smorodov. so basically, i would then need to have a picture of each button stored in a database, and then if i take a picture of one of the buttons it is matched against the pictures in the database, right? is that template matching available on android? – user2677466 Aug 21 '13 at 18:33
  • You need run all database images, and get one with max similarity, then comapare evaluated similarity with threshold to cut off false positives. Yes, here is an example: http://stackoverflow.com/questions/17001083/opencv-template-matching-example-in-android – Andrey Smorodov Aug 21 '13 at 18:39
  • lets assume i got 50 database images, that would take some time to run all of them against one picture, right? my goal is to reach a match in max. 2 seconds, would that still work you think? i have no clue how fast that matchTemplate method is. thanks again :) – user2677466 Aug 21 '13 at 18:55
  • It depends on image and template sizes and machine power. But as I can see in this example (in video in page bottom) it works realtime (15-20 frames/sec): http://www.larmor.com/portal/index.php?option=com_content&task=view&id=27&Itemid=60 – Andrey Smorodov Aug 21 '13 at 19:34
  • thanks again for your answer. i tried the code given in the link you gave me: http://stackoverflow.com/questions/17001083/opencv-template-matching-example-in-android .unfortunately im getting this error when im trying to execute it: OpenCV Error: Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1) in unknown function i was inserting this code: System.out.println(img.cols()+" "+img.rows()+" "+templ.cols()+" "+templ.rows()); after the Highgui.imread(file) lines, and the cols and rows are 0 :/ do you have any clue why this could be? – user2677466 Aug 22 '13 at 06:51
  • It may be because of image files paths are not valid, and imread can't read image and template. – Andrey Smorodov Aug 22 '13 at 07:37
  • that was indeed the problem, forgot a sign in the path :/ i tried the code now and it works, atleast sometimes. for example, my input image is this: http://i.imgur.com/jl45LIw.jpg and my template image is this: http://i.imgur.com/XixlG2j.jpg . sadly the result looks like this: http://i.imgur.com/0N6dSe6.jpg . do you see the green sqaure? its not where it should be. it works when the input image is EXACTLY the same as the template image, just not cropped. – user2677466 Aug 22 '13 at 09:01
  • But then Haar classifier, I think, also will not perform well. You need train classifier on objects it will works with. You can try use SURF or SIFT features based detectors. But they are rather slow. I don't know about java examples but there are C++ example in opencv distribution matching_to_many_images.cpp. – Andrey Smorodov Aug 22 '13 at 09:59
  • i've been trying with featuredetectors before, but that is way to slow for my purpose sadly. so summary so far: trained haar classifier wont work as you say, template matching wont work as i tried now and featuredetection is too slow. do you have any other thing in mind, that could work for my purpose and will be faster? thanks so far. – user2677466 Aug 22 '13 at 10:22
  • May be get templates not in frontal view but from view they will appear in real image? If camera always look from one/or fixed set of positions, may be templates should be made from these positions. – Andrey Smorodov Aug 22 '13 at 10:50
  • thanks again for your reply. the problem is, i dont know from which angle the user will take his picture. so basically i can't know in which angle my template should be. also i tried with the image being in frontal view aswell, but just a few degrees rotated, and it didnt work either. – user2677466 Aug 22 '13 at 11:23