Creating samples

First things first, you need a tool called opencv_createsamples to prepare the positive image sample set. The negative image samples, on the other hand, are extracted automatically during the training from a provided folder containing arbitrary images that do NOT include the object of interest. The opencv_createsamples application can be found inside the bin folder of the OpenCV installation. It can be used to create the positive samples dataset, either by using a single image of the object of interest and applying distortions and transformations to it, or by using previously cropped or annotated images of the object of interest. Let's learn about the former case first.

Imagine you have the following image of a traffic sign (or any other object, for that matter) and you want to create a positive sample dataset using it:

You should also have a folder containing the source of the negative samples. As we mentioned previously, you need to have a folder containing arbitrary images that do not contain the object of interest. Let's assume we have some images similar to the following that we'll be using to create negative samples from:

Note that the size and aspect ratio of negative images, or to use the correct terminology, the background images, is not at all important. However, they must be at least as big as the minimum-detectable object (classifier size) and they must never contain images of the object of interest.

To train a proper cascade classifier, sometimes you need hundreds or even thousands of sample images that are distorted in different ways, which is not easy to create. In fact, gathering training data is one of the most time-consuming steps in creating a cascade classifier. The opencv_createsamples application can help with this problem by taking in the previous image of the object we're creating a classifier for and producing a positive samples dataset by applying distortions and using the background images. Here's an example of how it's used:

opencv_createsamples -vec samples.vec -img sign.png -bg bg.txt  
    -num 250 -bgcolor 0 -bgthresh 10 -maxidev 50 
    -maxxangle 0.7 -maxyangle 0.7 -maxzangle 0.5 
    -w 32 -h 32 

Here is a description of the parameters used in the preceding command:

  • vec is used to specify the positive samples file that will be created. In this case, it is the samples.vec file.
  • img is used to specify the input image that will be used to generate the samples. In our case, it's sign.png.
  • bg is used to specify the background's description file. A background's description file is a simple text file that contains the paths to all background images (each line in the background's description file contains the path to one background image). We have created a file named bg.txt and provided it to the bg parameter.
  • The num parameter determines the number of positive samples you want to generate using the given input image and backgrounds; 250, in our case. You can, but of course, use a higher or lower number, depending on the accuracy and duration of training that you require.
  • bgcolor can be used to define the background color in terms of its grayscale intensity. As you can see in our input image (the traffic sign image), the background color is black, thus the value of this parameter is zero.
  • The bgthresh parameter specifies the threshold of the accepted bgcolor parameter. This is especially useful in the case of compression artifacts that are common to some image formats and might cause slightly different pixel values for the same color. We have used 10 for the value of this parameter to allow a slight level of tolerance for background pixels.
  • maxidev can be used to set the maximum intensity deviation of the foreground pixel values while generating the samples. A value of 50 means the intensity of the foreground pixels can vary between their original values +/- 50.
  • maxxangle, maxyangle, and maxzangle correspond to the maximum possible rotation allowed in the x, y, and z directions when creating new samples. These values are in radians, for which we have provided 0.7, 0.7, and 0.5.
  • The w and h parameters define the width and height of the samples. We have used 32 for both of them since the object we're looking to train a classifier for fits in a square shape. These same values will be used later on, when training the classifier. Also note that this will be the minimum detectable size in your trained classifier later on.
Besides the parameters in the preceding list, the opencv_createsamples application also accepts a show parameter that can be used to display the created samples, an inv parameter that can be used to invert the colors of samples, and a randinv parameter that can be used to set or unset the random inversion of pixels in samples.

Running the preceding command will produce the given number of samples by performing rotations and intensity changes to the foreground pixels. Here are some of the resultant samples:

Now that we have a positive samples vector file, produced by opencv_createsamples, and a folder that contains the background images along with a background's description file (bg.txt from the previous example), we can start the training of our cascade classifier, but before that, let's also learn about the second method of creating our positive samples vector, which is by extracting them from various annotated images that contain our object of interest.

This second method involves using another official OpenCV tool which is used for annotating positive samples in images. This tool is called opencv_annotation and it can be used to conveniently mark the areas in a number of images that contain our positive samples, or in other words, the objects, we're going to train a cascade classifier for them. The opencv_annotation tool produces an annotation text file (after a manual annotation of the objects) that can be used with the opencv_createsamples tool to produce a positive samples vector suitable for use with the OpenCV cascade training tool that we'll learn about in the next section.

Let's assume we have a folder containing images similar to the following:

All of these images are located in a single folder and all of them contain one or more samples of the traffic sign (the object of interest) that we're looking for. We can use the following command to start the opencv_annotation tool and manually annotate the samples:

opencv_annotation --images=imgpath --annotations=anno.txt 

In the preceding command, imgpath must be replaced with the path (preferably the absolute path and with forward slashes) to the folder containing the images. anno.txt, or any other file name provided instead, will be filled with the annotation results, which can be used with opencv_createsamples to create a positive samples vector. Executing the preceding command will start the opencv_annotation tool and output the following text, which describes how to use the tool and its shortcut keys:

* mark rectangles with the left mouse button, 
* press 'c' to accept a selection, 
* press 'd' to delete the latest selection, 
* press 'n' to proceed with next image, 
* press 'esc' to stop.

Immediately after the preceding output, a window similar to the following will be displayed:

You can highlight an object using your mouse's left button, which will cause a red rectangle to be drawn. Pressing the C key will finalize the annotation and it will become red. Continue this process for the rest of the samples (if any) in the same image and press N to go to the next image. After all images are annotated, you can exit the application by pressing the Esc key.

In addition to the -images and -annotations parameters, the opencv_annotation tool also includes an optional parameter, called -maxWindowHeight, that can be used to resize images that are bigger than a given size. The resize factor in this case can be specified with another optional parameter called -resizeFactor.

The annotation file created by the opencv_annotation tool will look like the following:

signs01.jpg 2 145 439 105 125 1469 335 185 180 
signs02.jpg 1 862 468 906 818 
signs03.jpg 1 1450 680 530 626 
signs04.jpg 1 426 326 302 298 
signs05.jpg 0 
signs06.jpg 1 1074 401 127 147 
signs07.jpg 1 1190 540 182 194 
signs08.jpg 1 794 460 470 488

Each line in the annotations file contains the path to an image, followed by the number of objects of interest in that image followed by the x, y, width, and height values of the bounding rectangles of those objects. You can use the following command to produce a samples vector using this annotation text file:

opencv_createsamples -info anno.txt -vec samples.vec -w 32 -h 32 

Note that this time we used the opencv_createsamples tool with the -info parameter, which wasn't present when we used this tool to generate samples from an image and arbitrary backgrounds. We are now ready to train a cascade classifier that is capable of detecting the traffic sign we created the samples for.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.61