Use of SVM and SVM
SVM can achieve the function of finding a split line (surface) in the given positive and negative samples and separating the positive and negative samples. This split line (surface) is what we call a classifier. It records the features of positive samples and the differences between the features and negative samples. When a new sample comes in, it can be compared with the classifier to determine whether the new sample is in the positive sample column.
Taking vehicle recognition as an example, SVM is generally used as follows:
1. obtain positive and negative samples.
As mentioned above, SVM can find a line (surface) between positive and negative samples. What is positive and negative samples?
The so-called positive sample means that you manually determine the sample that contains the required information. Vertex. StringMark. Of course, there are many types of information record files, and different tag files are generated as needed. A Tag file generally contains the location information, size information, and file name of the box. When SVM is used to learn, the content in the frame is obtained based on the location, size, and other features of the tag information record file.
A negative sample is a non-positive sample. For example, except for the area in the red box, the area in the picture above cannot be negative samples, and other areas in the picture can be negative samples. However, negative samples are not random and should be categorized as much as possible. In vehicle identification, negative samples should be used in road scenarios as much as possible. If the negative sample is too random, the training cost increases and the training result is inaccurate.
2. Obtain training results
After obtaining positive and negative samples, how can the machine obtain the characteristics of the samples and distinguish them?
After a positive sample is obtained, the machine itself does not know what features are in the positive sample. Although people tell the machine that this is a picture of the car, how does the machine remember the characteristics of the car? In addition, the car in each sample must be long, So What features does the algorithm use to distinguish the car during the learning process?
In practice, there are many of these features, and the general features of many images can be used as the basis for learning. The gray scale, gradient, histogram, hog feature, and haar feature of the image. As long as you tell the machine which feature is used for learning, the algorithm will first calculate the features of the sample, then learn the features of each image, and record them.
Because the positive and negative samples have been given manually, after the machine compares the features of the positive and negative samples, it will get a classification result, which is a. xml file. The classification result is a classifier that can be classified.
3. Classification
After obtaining the classification result, you can use the result to classify the new image. During classification, the same features must be used as during training. That is, if the haar feature is used during training, you must first obtain the haar feature of the new image and compare the obtained haar feature with the classification result to determine whether the feature is a vehicle.
The classification code based on opencv is given below:
# Include "opencv2/objdetect. hpp "# include" opencv2/highgui. hpp "# include" opencv2/imgproc. hpp "# include <opencv2/opencv. hpp> # include <iostream> # include <stdio. h> using namespace std; using namespace cv;/** function declaration */void detectAndDisplay (Mat frame);/** global variable */string car_cascade_name = "put. xml "; // The training result file CascadeClassifier car_cascade; string window_name =" Capture-car detection "; RNG rng (12345);/** @ main function */int main (int argc, const char ** argv) {Mat frame; VideoCapture video; VideoWriter markVideo; // used to store the video after identifying the vehicle // load the cascade classifier file if (! Car_cascade.load (car_cascade_name) {printf ("--(!) Error loading \ n "); return-1 ;}; // obtain the video information double fourcc, fps, width, height; video. open ("25.avi"); fourcc = video. get (CV_CAP_PROP_FOURCC); fps = video. get (CV_CAP_PROP_FPS); width = video. get (CV_CAP_PROP_FRAME_WIDTH); height = video. get (CV_CAP_PROP_FRAME_HEIGHT); markVideo. open ("markVideo25.avi", fourcc, fps, Size (width, height), false); // create a video and store the result if (video. isOpened () & markVideo. isOpened () {while (tr Ue) {video> frame; resize (frame, frame, Size (280,240); // reduce the image Size, accelerate detection speed // use classifier to detect the current frame if (! Frame. empty () {detectAndDisplay (frame); markVideo <frame;} else {printf ("--(!) No captured frame -- Break! "); Break;} if (char) waitKey (1) = 'C') {video. release (); markVideo. release (); break; }}} return 0;}/** @ function detectAndDisplay */void detectAndDisplay (Mat frame) {std: vector <Rect> faces; Mat frame_gray; cvtColor (frame, frame_gray, CV_BGR2GRAY); equalizeHist (frame_gray, frame_gray); // histogram equalization // -- Multi-dimensional Face Detection car_cascade.detectMultiScale (frame_gray, faces, 1.1, 3, 0, size (10, 12), Size (100,120); for (int I = 0; I <faces. size (); I ++) {rectangle (frame, Point (faces [I]. x, faces [I]. y), Point (faces [I]. x + faces [I]. width, faces [I]. y + faces [I]. height), 0xff, 3); // draw the recognized box} // -- display the result image imshow (window_name, frame );}