True Value calibration of training samples

Source: Internet
Author: User

Machine Learning Algorithms are always inseparable from training samples. Generally, the images you find do not only contain positive samples, but must contain both positive and negative samples. For example, you are planning to use machine learning algorithms for face detection. You need to find the positive and negative samples of the face (non-face images). At this time, positive and negative samples are often not so easy to find (of course, many public training sample libraries can be found online for face training samples, but what if you want to find a training sample of a vehicle? What about facial expression training samples? Cat training samples? Aircraft training samples ?...); At this time, you need to take or download a lot of images containing positive samples (such as faces). Of course, some areas of this image are faces (positive samples ), other areas are non-human faces (negative samples). Obviously, a software such as a drawing tool is used to manually deduct the positive sample area of a picture as a positive sample,
The remaining area, as a negative sample, is a feasible method; you can do this, but as a programmer, it seems to write a program, traverse all the pictures in the folder, and display the pictures in turn, you can click a few times to get the positive sample area and the negative sample area, which is more efficient. The former is a purely manual approach, while the latter is a semi-manual approach. When you take enough positive and negative samples and train the classifier, you can use the machine (computer) the positive sample area (FACE) in the image can be automatically deducted. This is a fully automatic method, let machines replace humans with efficient repetitive work. Of course, before we get a training sample, you still need to use a pure or semi-artificial method to solve the problem of training samples. The author provides a semi-artificial program for your friends to use in the sample production process in the future;

Reference code:

# Include "stdafx. H "# include" windows. H "# include <vector> # include <string> # include" opencv. HPP "# include" iostream "# include" fstream "using namespace STD; typedef STD: vector <STD: String> file_lists; static int str_compare (const void * arg1, const void * arg2) {return strcmp (* (STD: string *) arg1 ). c_str (), (* (STD: string *) arg2 ). c_str (); // compare the string arg1 and arg2} file_lists scandirectory (const STD: string & path, Const STD: string & Extension) {win32_find_data WFD; // win32_find_data: contains information about the file that is found by the // findfirstfile, findfirstfileex, or findnextfile function handle hhandle; string searchpath, searchfile; file_lists vfilenames; int nbfiles = 0; searchpath = path + "/*" + extension; hhandle = findfirstfile (searchpath. c_str (), & WFD); // searches a directory for a file or sub Directory // with a name that matches a specific name if (invalid_handle_value = hhandle) {fprintf (stderr, "error (% s, % d): cannot find (*. % s) files in directory % s/n ", _ file __, _ line __, extension. c_str (), path. c_str (); exit (0);} do {//. or .. if (WFD. cfilename [0] = '. ') {continue;} // If exists sub-directory if (WFD. dwfileattributes & file_attribute_directory) // dwfileattributes: The file Tributes of a file {// file_attribute_directory: The handle identifies a directory continue;} else // If file {searchfile = path + "/" + WFD. cfilename; vfilenames. push_back (searchfile); nbfiles ++ ;}} while (findnextfile (hhandle, & WFD )); // call this member function to continue a file search begun // with a call to cgopherfilefind: findfile findclose (hhandle); // closes a file search handle opened by Findfirstfile, findfirstfileex, // or findfirststreamw function // sort the filenames qsort (void *) & (vfilenames [0]), (size_t) nbfiles, sizeof (string ), str_compare); // performs a Quick Sort return vfilenames;} bool rbtndown = false; const int ptssize = 4; cvpoint PTS [ptssize]; int ptscount = 0; void mouseon (int e, int X, int y, int flags, void * PARAM) {If (E = cv_event_lbuttondown) {PTS [ptscount ++] = cvpoint (X, Y);} else if (E = cv_event_rbuttondown) {rbtndown = true ;}} void gettrainsample () {string folderin, fileext, folderout; ifstream filein; filein. open ("config. ini ", IOS: In); If (! Filein) {cout <"config. INI open error "<Endl; System (" pause "); exit (-1);} Char STR [512]; memset (STR, '\ 0 ', 512 * sizeof (char); filein> STR; folderin = STR; memset (STR, '\ 0', 512 * sizeof (char); filein> STR; fileext = STR; memset (STR, '\ 0', 512 * sizeof (char); filein> STR; folderout = STR; file_lists files = scandirectory (folderin, fileext ); int size = files. size (); cout <"Total number of images in the folder:" <size <Endl; string filename; strin G path; string ptsname; int Len; cvnamedwindow ("IMG", 0); cvsetmousecallback ("IMG", mouseon); For (INT I = 0; I <size; I ++) {cout <I + 1 <"/" <size <Endl; int idx = files [I]. find_last_of ('\/'); filename. clear (); filename = files [I]. substr (idx + 1, files [I]. length ()-idx); Path = folderout + "/" + filename; ptsname = path; Len = ptsname. length (); If (ptsname [len-4] = '. ') {ptsname [len-1] = 'T'; ptsname [len-2] = 'X'; ptsnm E [len-3] = 'T';} else {ptsname [len-1] = '\ 0'; ptsname [len-2] = 'T'; ptsname [len-3] = 'X '; ptsname [len-4] = 'T';} ofstream fileout; fileout. open (ptsname. c_str (), IOS: Out); iplimage * pimg = cvloadimage (files [I]. c_str (); If (! Pimg) {cout <"IMG load error, filename:" <files [I]. c_str (); continue;} cvsaveimage (path. c_str (), pimg); While (! Rbtndown) {cvshowimage ("IMG", pimg); cvwaitkey (1); If (ptscount = ptssize) {int Minx, miny, Maxx, Maxy; minx = Maxx = PTS [0]. x; miny = Maxy = PTS [0]. y; For (Int J = 1; j <ptssize; j ++) {Minx = Minx <PTS [J]. x? Minx: PTS [J]. X; miny = miny <PTS [J]. Y? Miny: PTS [J]. Y; Maxx = Maxx> PTS [J]. X? Maxx: PTS [J]. X; Maxy = Maxy> PTS [J]. Y? Maxy: PTS [J]. y;} fileout <Minx <"" <miny <"" <Maxx <"" <Maxy <Endl; ptscount = 0; cvrectangle (pimg, cvpoint (Minx, miny), cvpoint (Maxx, Maxy), cv_rgb (255, 0); cvshowimage ("IMG", pimg); cvwaitkey (1 );}} rbtndown = false; ptscount = 0; cvreleaseimage (& pimg); fileout. close () ;}} void usage () {cout <"config. INI description "<Endl; cout <" from config. the INI File Reads three lines of information in sequence: \ N input image folder path \ N input image suffix \ N output path; \ n \ nconfig. examples of the content in ini: \ NC :\\ inputdir \ n.bmp \ nd :\\ result "<Endl; cout <" Instructions for using this program: "<Endl; cout <" you can click the left-click button four times to select a rectangular area in the image. After several Rectangular areas are selected, right-click the image to switch to the next image. "<Endl; cout <"//////////////////////////////////// /// // "<Endl ;} int _ tmain (INT argc, _ tchar * argv []) {usage (); gettrainsample (); System ("pause"); Return 0 ;}

Note:

(1) The above Code requires opencv. Please configure relevant lib and DLL on your own;

(2) during compilation, do not select the Unicode character set, for example, in vs2008 or vs2010. Set the following, project --> Property --> General --> character set --> not set;

(3) create a config. ini file in the path of the executable file, which contains three lines:

Input Image path

Image suffix

Output path;

Config. ini reference settings:

E: \ Images
. BMP
E: \ result

(4) do not include Chinese paths in the input and output paths in config. ini. errors may occur;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.