directory

One, download from the network or find their own pictures cut out the right picture.

Second, after the negData and posData folders are ready, use the command prompt (win + R), enter CMD to switch the location to the posData folder location.

3, find the OpenCV folder you downloaded, open OpenCV, open Build, open X64, open VC14, open bin folder.

Exe -vec pos.vec-info pos.txt -num 32 -w 100 -h 40. 4. Open CMD and enter opencv_createsamples.

5. Create a TXT file in the main folder named “TrainCascade”.

6, after you can run VS, create a VC++ project. Enter the code and test it.


One, download from the network or find their own pictures cut out the right picture.

The size and format of the images should be the same. Prepare two folders named posData (positive sample) and negdata (negative sample) respectively. The sizes of all samples must be the same. The picture in PosData is a vehicle sample. The classifier is presented with the correct sample.

Negdata images are background samples that do not contain vehicles and are used to tell the classifier what is wrong. Although the negative sample means that there is no positive sample in the sample, it is better to choose different negative samples according to different projects. For example, a project is to do face detection at the airport, so it is better to take some picture data from the scene and collect negative samples.

Second, after the negData and posData folders are ready, use the command prompt (win + R), enter CMD to switch the location to the posData folder location.

TXT file in pos.txt file in posData folder by typing dir /b/s/p/w *.jpg > pos.txt. Open pos.txt, select Edit-Replace, search JPG in search, replace JPG with JPG 100 100 40, save and copy pos.txt to the upper directory.

Then do the same for the negData folder. Enter dir /b/s/p/w *.jpg > neg.txt in CMD.

3, find the OpenCV folder you downloaded, open OpenCV, open Build, open X64, open VC14, open bin folder.

Select opencv_createsamps. exe and opencv_traincascad. exe and copy them to the desired folder alongside negData and PosData.

 

Exe -vec pos.vec-info pos.txt -num 32 -w 100 -h 40. 4. Open CMD and enter opencv_createsamples.

Pos.txt indicates that this is a positive sample;

Num 32 represents the number of positive sample photos;

W 100 H40 image resolution (high and wide)

Pos.vec will then appear in that folder

Exe -vec ng.vec-info ng.txt -num 100 -w 100 -h 40 to generate ng.vec.

5. Create a TXT file in the main folder named “TrainCascade”.

Enter “in this TXT.

opencv_traincascade.exe -data xml -vec pos.vec -bg neg.txt -numPos 32 -numNeg 100 -numStages 20 -w 100 -h 40 -mode ALL


pause

NumPos 32 indicates the number of positive sample photos. NumNeg100 indicates the number of negative sample photos. NumStage 20 indicates the number of detection times

TXT and rename traincascade. TXT to bat.

Then double-click TrainCascade.bat. This has the following effects

When training classifier, it takes up more CPU space, and the computer will run slowly and take longer time. When the processing is complete, a cascade.xml file will be generated under the folder.

6, after you can run VS, create a VC++ project. Enter the code and test it.

The code is:

#include<opencv2\opencv.hpp>

#include<iostream>

using namespace cv;

using namespace std;

String filename = “F:/ cascade.xml”; // Cascade. XML position CascadeClassifier car_classifier;

int main(int argc, char**argv) { if (! car_classifier.load(filename)) { printf(“could not laod car feature data.. \n”); return -1; }

Mat SRC = imread(“D:/ image /15.jpg”); If (src.empty()) {printf(“could not laod image… \n”); return -1; } imshow(“inupt image”, src); Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray);

vector

cars; Car_classifier. DetectMultiScale (gray, cars, 1.1, 3, 0, the Size (50, 50)); for (size_t t = 0; t < cars.size(); t++) { rectangle(src, cars[static_cast

(t)], Scalar(0, 0, 255), 2, 8, 0); }

    imshow(“detect cars”, src);

waitKey(0);

return 0;

}

Then click “Run”, because the positive and negative samples are too small, the effect is not very obvious. The results are as follows: