void HOGTrainer::train() { vector<Mat> fullPosLst; vector<Mat> fullNegLst; vector<Mat> negLst; vector<Mat> posLst; vector<Mat> gradientLst; vector<int> labels; loadImages(posDir, pos, fullPosLst); samplePos(fullPosLst, posLst, size); labels.assign(posLst.size(), +1); const unsigned int old = (unsigned int) labels.size(); loadImages(negDir, neg, fullNegLst); sampleNeg(fullNegLst, negLst, size); labels.insert(labels.end(), negLst.size(), -1); cout << old << " " << labels.size(); CV_Assert(old < labels.size()); computeHog(posLst, gradientLst, size); computeHog(negLst, gradientLst, size); trainSvm(gradientLst, labels); }
esvmHog *computeHogWrapper(const char *filename,const int cellWidth,const bool enablePadding,const int padding,const int userTasks) { esvmImage *img = readImage(filename); int numRows = img->numRows; int numCols = img->numCols; int numChannels = img->numChannels; assert(numChannels == 3); int *im = img->im; esvmHog *soln; soln = computeHog(im,numRows,numCols,numChannels,cellWidth,enablePadding,padding,userTasks); free(img->im); //frees int *im as well. free(img); return soln; }
esvmHogPyr *computeHogScale(cv::Mat img,const int cellWidth, const int maxLevels,const int minDimension,const int interval, const float minScale,const bool enablePadding,const int padding,const int userTasks, const bool useMexResize) { int numRows = img.rows; int numCols = img.cols; int numChannels = 3; float sc = pow(2,1.0/interval); esvmHog **pyr = (esvmHog **)esvmCalloc(maxLevels,sizeof(esvmHog *)); float *scaleArr = (float *)esvmCalloc(maxLevels,sizeof(float)); //do the first level outside the loop. So that you don't have to make a call to resize //OpenCV resize does some weird stuff on resize factor 1.0 int *tmpim = RgbtoIm(img,numRows,numCols,numChannels); scaleArr[0]=1.0f; pyr[0] = computeHog(tmpim, numRows, numCols, numChannels, cellWidth, enablePadding, padding, userTasks); free(tmpim); int counter = 1; float *flIm; cv::Mat dst; if(useMexResize==true) { flIm = RgbtoImFlTranspose(img,numRows,numCols,numChannels); } for(int i=2;i<=maxLevels;i++) { float scale = 1.0 / pow(sc,i-1); scaleArr[i-1] = scale; int nr = round((float)numRows * scale); int nc = round((float)numCols * scale); if(scale < minScale) { break; } if(min(nr,nc)<=minDimension) { break; } int *im; if(useMexResize==false) { dst.create((int)nc,(int)nr,img.type()); cv::resize(img,dst,dst.size(), 0, 0, ESVM_INTERP); im = RgbtoIm(dst,(int)nr,(int)nc,numChannels); } else { //im = mexResize(flIm,numRows,numCols,numChannels,numRows*scale,numCols*scale); im = mexResize(flIm,numRows,numCols,numChannels,nr,nc); } pyr[i-1] = computeHog(im, nr, nc, numChannels, cellWidth, enablePadding, padding, userTasks); if(__unlikely(pyr[i-1]==NULL)) break; counter++; //if(useMexResize==false) // cvReleaseImage(&dst); free(im); if(__likely(enablePadding==true)) { if( max(pyr[i-1]->rows-(padding<<1),pyr[i-1]->cols-(padding<<1)) <= minDimension) { break; } } else { if(max(pyr[i-1]->rows,pyr[i-1]->cols) <= minDimension) { break; } } } scaleArr = (float *)realloc((void *)scaleArr,counter*sizeof(float)); esvmHogPyr *hogpyr = (esvmHogPyr *)esvmMalloc(sizeof(esvmHogPyr)); hogpyr->num = counter; hogpyr->hogs = pyr; hogpyr->scale = scaleArr; //FIXME:: We can shrink pyr from maxLevels to counter, using realloc. //It is not a serious problem since pyr actually contains maxLevels*sizeof(esvmHog *) bytes //cvReleaseImage(&img); if(useMexResize==true) free(flIm); return hogpyr; }