Ask Your Question

Is OpenCV supposed to train on positives or samples images?

asked 2016-02-04 03:35:33 -0600

Lory gravatar image

In one of my questions discussed here here there's the problem that OpenCV recognizes the object to detect only in training images. Up to today I have listened to discordant points of view: how does the .vec file have to be created? Does it need to contain only positive images (cropped images showing ONLY the interested object) or samples images (interested object with random background)? If it needs to contain only the cropped interested object, on which images does opencv_traincascade have to train? I read and read this tutorial over and over but I still don't understand which is the correct way to proceed...would anyone explain me?

edit retag flag offensive close merge delete


In my opinion (for what it worth), the general (safe) method should be:

  • collect as many as possible images of the object you want to detect (with various point of view, various situations)
  • crop the images to keep only the object you want to detect
  • use opencv_createsamples only to create the vec file for opencv_traincascade (no warping, no backrgound combination)
  • train with your cropped images and negatives images

Warping is not good as it is only a mean to create artificially a lot more images that could be different from reality.

In the question you linked, you generated too much warped images (-num 210) from only 60 real cropped images of coins.

You should start to train with only the 60 cropped images and see what happens (test, retry until satisfactory results).

Eduardo gravatar imageEduardo ( 2016-02-04 04:52:40 -0600 )edit

Yes, yesterday night I launched

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)

So, as you can see I did not generate any warped image and I launched opencv_traincascade with LBP mode. But this does not work too...nothing is detected

Lory gravatar imageLory ( 2016-02-04 05:05:46 -0600 )edit

@Eduardo: can I write you by email? Only if I don't annoy you...

Lory gravatar imageLory ( 2016-02-04 09:07:15 -0600 )edit

@Lory, if we put effort into helping you and providing your with guidelines, then please keep the discussion to this forum and not on a 1 by 1 basis with one of the people helping out. The ultimate goal for this forum is people that tend to find solutions on the base of already existing questions ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-02-04 09:09:33 -0600 )edit

Your command generates 209 new artificially images (what I called a warped image) with rotations around the Z-axis (documentation). And at the end you have 12000 positive images with only 60 images that are real that is not good at all in my opinion.

Try the easiest first by using only your real cropped images. Test and retry by adding / removing positive or negative images until you have satisfactory result.

PS: you should follow the advice of StevenPuttemans as he mostly works on this subject for his research.

Eduardo gravatar imageEduardo ( 2016-02-04 09:12:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-02-04 06:43:16 -0600

Let me repeat what I have been telling you the last 5 topics

  • Most of the tutorials out there are heavily outdated and only work for constrained lab applications.
  • I am not forcing you to read my book, but it has a step by step guide on how to do so, including how to do it in a way that has been validated in about 15 industrial working cases now.
  • Read all of the traincascade topics in this forum, tons of them have one of my detailed explanations on how to select your training data and how it all works.
  • And AGAIN --> REMOVE all the angle stuff, it screws everything up ...

Do not believe me if you do not want to, but then I kindly ask you to stop opening topics about the same question over and over again ...

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-02-04 03:35:33 -0600

Seen: 184 times

Last updated: Feb 04 '16