int main() { hog(); hog(); hog(); cat(); waitall(); return 0; }
void FeatureHOG::calc(Mat& _input) { Mat iResize, iRGB[3], iGray; vector<float> ders; resize(_input, iResize, size); split(iResize, iRGB); //cvtColor(iResize, iGray, CV_RGB2GRAY); for (int idx = 0; idx < 3; idx++) { iGray = iRGB[idx]; /// 调整第 4 个 size 参数也可以控制直方图 bin 数量 HOGDescriptor hog(Size(24, 24), Size(24, 24), Size(4, 4), Size(12, 12), 9); hog.compute(iGray, ders, Size(24, 24), Size(0, 0)); //fprintf(stderr, "size = %lu\n", ders.size()); h[idx] = Mat(1, ders.size(), CV_32F); for (size_t i = 0; i < ders.size(); i++) { h[idx].ptr<float>(0)[i] = ders[i]; } } }
float HoGNode::calc_thresh(const std::vector<cv::Mat>& data, const cv::Rect& roi) const { cv::Mat mat = cv::Mat(data[0], roi); cv::HOGDescriptor hog(mat.size(), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 8); //cv::HOGDescriptor hog; std::vector<float> ders; std::vector<cv::Point>locs; std::vector<cv::Rect> locations; cv::Mat gray = mat; gray.convertTo(gray, CV_8UC1, 255.); //hog.detectMultiScale(gray, locations, m_threshold, cv::Size(8, 8)); hog.compute(gray, ders, cv::Size(8, 8), cv::Size(0, 0), locs); cv::Mat hogFeat; hogFeat.create(ders.size(), 1, CV_32FC1); float distance = 0; for (int i = 0; i < ders.size(); ++i) distance += abs(ders.at(i)); if (m_log_stream != nullptr) *m_log_stream << "HoGNode;" << distance << ";" << std::endl; return distance; }
// H=gradHist(M,O,[...]) - see gradientHist.m void mGradHist( int nl, mxArray *pl[], int nr, const mxArray *pr[] ) { int h, w, d, hb, wb, nChns, binSize, nOrients, softBin, useHog; bool full; float *M, *O, *H, clipHog; checkArgs(nl,pl,nr,pr,1,3,2,8,&h,&w,&d,mxSINGLE_CLASS,(void**)&M); O = (float*) mxGetPr(pr[1]); if( mxGetM(pr[1])!=h || mxGetN(pr[1])!=w || d!=1 || mxGetClassID(pr[1])!=mxSINGLE_CLASS ) mexErrMsgTxt("M or O is bad."); binSize = (nr>=3) ? (int) mxGetScalar(pr[2]) : 8; nOrients = (nr>=4) ? (int) mxGetScalar(pr[3]) : 9; softBin = (nr>=5) ? (int) mxGetScalar(pr[4]) : 1; useHog = (nr>=6) ? (int) mxGetScalar(pr[5]) : 0; clipHog = (nr>=7) ? (float) mxGetScalar(pr[6]) : 0.2f; full = (nr>=8) ? (bool) (mxGetScalar(pr[7])>0) : false; hb = h/binSize; wb = w/binSize; nChns = useHog== 0 ? nOrients : (useHog==1 ? nOrients*4 : nOrients*3+5); pl[0] = mxCreateMatrix3(hb,wb,nChns,mxSINGLE_CLASS,1,(void**)&H); if( nOrients==0 ) return; if( useHog==0 ) { gradHist( M, O, H, h, w, binSize, nOrients, softBin, full ); } else if(useHog==1) { hog( M, O, H, h, w, binSize, nOrients, softBin, full, clipHog ); } else { fhog( M, O, H, h, w, binSize, nOrients, softBin, clipHog ); } }
int ObjectDetection::GetHogDescriptor(cv::Mat& img, std::vector<float> &hogDesc) { int ht = img.cols; int width = img.rows; cv::HOGDescriptor hog(cv::Size(ht, width), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9, 1, -1, cv::HOGDescriptor::L2Hys, 0.2, true, cv::HOGDescriptor::DEFAULT_NLEVELS); hog.compute(img, hogDesc, cv::Size(16, 16), cv::Size(0,0) ); return 0; }
//HOG Features void getHOGFeatures(const Mat& image, Mat& features) { //HOG descripter HOGDescriptor hog(cvSize(128, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 3); //these parameters work well std::vector<float> descriptor; // resize input image to (128,64) for compute Size dsize = Size(128,64); Mat trainImg = Mat(dsize, CV_32S); resize(image, trainImg, dsize); // compute descripter hog.compute(trainImg, descriptor, Size(8, 8)); // copy the result Mat mat_featrue(descriptor); mat_featrue.copyTo(features); }
/* H = hog( I, [sBin], [oBin] ) - see hog.m */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int h, w, d, hb, wb, nb, hb1, wb1, sBin, oBin; double *I, *M, *H, *HG; float *O; checkArgs(nlhs,plhs,nrhs,prhs,1,1,1,3,&h,&w,&d,&I); sBin = (nrhs>=2) ? (int) mxGetScalar(prhs[1]) : 8; oBin = (nrhs>=3) ? (int) mxGetScalar(prhs[2]) : 9; hb=h/sBin; wb=w/sBin; nb=wb*hb; hb1=hb>2?hb-2:0; wb1=wb>2?wb-2:0; plhs[0] = mxCreateMatrix3( hb1, wb1, oBin*4, mxDOUBLE_CLASS, 0, (void**) &HG); if( hb1==0 || wb1==0 ) return; M = (double*) mxMalloc(h*w*sizeof(double)); O = (float*) mxMalloc(h*w*sizeof(float)); H = (double*) mxCalloc(nb*oBin, sizeof(double)); gradMag( I, M, O, h, w, d ); gradHist( M, O, H, h, w, d, sBin, oBin, true, true ); hog( H, HG, h, w, d, sBin, oBin ); mxFree(M); mxFree(O); mxFree(H); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nlhs != 1 || nrhs != 2){ mexErrMsgTxt("Usage: feature = hog(im, sbin)"); } const mxArray* mximage = prhs[0]; const mxArray* mxsbin = prhs[1]; double *im = (double *)mxGetPr(mximage); const mwSize *im_dims = mxGetDimensions(mximage); if (mxGetNumberOfDimensions(mximage) != 3 || im_dims[2] != 3 || mxGetClassID(mximage) != mxDOUBLE_CLASS) mexErrMsgTxt("Invalid input"); int sbin = (int)mxGetScalar(mxsbin); plhs[0] = hog(im, im_dims, sbin); }
std::vector<float> Extractor::getHog(std::string filename) { cv::Mat src = cv::imread(filename); cv::Mat dst; // minimum dimensions are 40x40 int rows = src.rows > 40 ? src.rows % 40 : src.rows - 40; int cols = src.cols > 40 ? src.cols % 40 : src.cols - 40; rows = rows < 20 ? -rows : 40 - rows; cols = cols < 20 ? -cols : 40 - cols; // image dimensions must be multiple of 40 cv::resize(src, dst, cv::Size(src.cols + cols, src.rows + rows)); // parameters used to change default HOG values according to image size // vector of fixed size 1568 is obtained int rowMagnitude = dst.rows / 40; int colMagnitude = dst.cols / 40; cv::HOGDescriptor hog ( cv::Size(dst.cols, dst.rows), cv::Size(10 * colMagnitude, 10 * rowMagnitude), cv::Size(5 * colMagnitude, 5 * rowMagnitude), cv::Size(5 * colMagnitude, 5 * rowMagnitude), 8, 1, -1, cv::HOGDescriptor::L2Hys, 0.5, false, cv::HOGDescriptor::DEFAULT_NLEVELS ); std::vector<float> descriptors; hog.compute(dst, descriptors, cv::Size(0, 0)); return descriptors; }
void Visualise_FHOG(const cv::Mat_<double>& descriptor, int num_rows, int num_cols, cv::Mat& visualisation) { // First convert to dlib format dlib::array2d<dlib::matrix<float,31,1> > hog(num_rows, num_cols); cv::MatConstIterator_<double> descriptor_it = descriptor.begin(); for(int y = 0; y < num_cols; ++y) { for(int x = 0; x < num_rows; ++x) { for(unsigned int o = 0; o < 31; ++o) { hog[y][x](o) = *descriptor_it++; } } } // Draw the FHOG to OpenCV format auto fhog_vis = dlib::draw_fhog(hog); visualisation = dlib::toMat(fhog_vis).clone(); }
// H=gradHist(M,O,[bin],[nOrients],[softBin],[useHog],[clip])-see gradientHist.m void mGradHist( int nl, mxArray *pl[], int nr, const mxArray *pr[] ) { int h, w, d, hb, wb, bin, nOrients; bool softBin, useHog; float *M, *O, *H, *G, clip; checkArgs(nl,pl,nr,pr,1,3,2,7,&h,&w,&d,mxSINGLE_CLASS,(void**)&M); O = (float*) mxGetPr(pr[1]); if( mxGetM(pr[1])!=h || mxGetN(pr[1])!=w || d!=1 || mxGetClassID(pr[1])!=mxSINGLE_CLASS ) mexErrMsgTxt("M or O is bad."); bin = (nr>=3) ? (int) mxGetScalar(pr[2]) : 8; nOrients = (nr>=4) ? (int) mxGetScalar(pr[3]) : 9; softBin = (nr>=5) ? (bool) (mxGetScalar(pr[4])>0) : true; useHog = (nr>=6) ? (bool) (mxGetScalar(pr[5])>0) : false; clip = (nr>=7) ? (float) mxGetScalar(pr[6]) : 0.2f; hb=h/bin; wb=w/bin; if( useHog==false ) { pl[0] = mxCreateMatrix3(hb,wb,nOrients,mxSINGLE_CLASS,1,(void**)&H); gradHist( M, O, H, h, w, bin, nOrients, softBin ); } else { pl[0] = mxCreateMatrix3(hb,wb,nOrients*4,mxSINGLE_CLASS,1,(void**)&G); H = (float*) mxCalloc(wb*hb*nOrients,sizeof(float)); gradHist( M, O, H, h, w, bin, nOrients, softBin ); hog( H, G, h, w, bin, nOrients, clip ); mxFree(H); } }
int Train::trainHOGSVM(string PosSamPos, string PosSamListFile, string NegSamPos, string NegSamListFile, string HardSamPos, string HardSamListFile, string OutputSampleFeatureMatrixFile, string OutputHOGSVMFile){ //check the user set something or not. if ( PosSamNO == 0 ) { cout<<"Please set sample numbers first."<<endl; return -1; }else if ( ObjWidth == 0 ) { cout<<"Please set object size first."<<endl; return -1; } ////////////// use HOG to train svm classifier ////////////////////////// //detect window size HOGDescriptor hog(Size(ObjWidth,ObjHeigh), Size(16,16), Size(8,8), Size(8,8), 9); //the dimensions of HOG descriptor int DescriptorDim; //svm classifier MySVM svm; ////---- image input ----//// //the picture name string ImgName; //a file store the good and bad image examples' name ifstream finPos( PosSamListFile ); ifstream finNeg( NegSamListFile ); ifstream finHard( HardSamListFile ); //---- matrix of eigenvectors(numbers of images*DescriptorDim) for training examples ---- Mat sampleFeatureMat; //---- matrix of kinds of training examples(numbers of images*1[1 means have people and -1 means not]) ---- Mat sampleLabelMat; ///////////- 1. read good image examples to generate HOG descriptor -///////////// //////////////////////////////////////////////////////////////////////////////////// //here we need to input the car image cut out //we also need to adjust the image size. ///////////////////////////////////////////////////////////////////////////////// try{ for ( int num=0; num<PosSamNO && getline(finPos,ImgName); num++ ) { //get image cout<<"Handle: "<<ImgName<<endl; ImgName = PosSamPos + ImgName; Mat src = imread(ImgName); //compute HOG descriptor vector<float> descriptor; hog.compute(src,descriptor,Size(8,8)); //use the first example to initial the result matrix. if ( num == 0) { DescriptorDim = descriptor.size(); sampleFeatureMat = Mat::zeros(PosSamNO+NegSamNO+HardExampleNO, DescriptorDim, CV_32FC1); sampleLabelMat = Mat::zeros(PosSamNO+NegSamNO+HardExampleNO, 1, CV_32FC1); } //store the HOG descriptor computed. for ( int i=0; i<DescriptorDim; i++ ) { sampleFeatureMat.at<float>(num,i) = descriptor[i]; } sampleLabelMat.at<float>(num,0) = 1; } }catch(int e){ } ///////////- 2. read bad image examples to generate HOG descriptor -///////////// try{ for ( int num=0; num<NegSamNO && getline(finNeg,ImgName); num++ ) { cout<<"Handle: "<<ImgName<<endl; ImgName = NegSamPos + ImgName; Mat src = imread(ImgName); vector<float> descriptor; hog.compute(src,descriptor,Size(8,8)); for ( int i=0; i<DescriptorDim; i++ ) { sampleFeatureMat.at<float>(num+PosSamNO,i) = descriptor[i]; } sampleLabelMat.at<float>(num+PosSamNO,0) = -1; } }catch(int e){ } ///////////- 3. read hard image examples to generate HOG descriptor -///////////// try{ for ( int num=0; num<HardExampleNO && getline(finHard,ImgName); num++ ) { cout<<"Handle: "<<ImgName<<endl; ImgName = HardSamPos + ImgName; Mat src = imread(ImgName); vector<float> descriptor; hog.compute(src,descriptor,Size(8,8)); for ( int i=0; i<DescriptorDim; i++ ) { sampleFeatureMat.at<float>(num+PosSamNO+NegSamNO,i) = descriptor[i]; } sampleLabelMat.at<float>(num+PosSamNO+NegSamNO,0) = -1; } }catch(int e){ } /* ////- output files of vector matrix -//// ofstream fout( OutputSampleFeatureMatrixFile ); for ( int i=0; i<PosSamNO+NegSamNO+HardExampleNO; i++ ) { fout<<i<<endl; for( int j=0; j<DescriptorDim; j++ ){ fout<<sampleFeatureMat.at<float>(i,j)<<" "; } fout<<endl; } */ ////- train SVM classifier -//// //iteration finish condition: over 1000 times or mistake less than FLT_EPSILON CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, FLT_EPSILON); //SVM parameters: type, dim, relaxation factor CvSVMParams param(CvSVM::C_SVC, CvSVM::LINEAR, 0, 1, 0, 0.01, 0, 0, 0, criteria); cout<<"Train SVM Classifier..."<<endl; svm.train(sampleFeatureMat, sampleLabelMat, Mat(), Mat(), param); cout<<"Train SVM finished."<<endl; char outsave[512]; strcpy(outsave,OutputHOGSVMFile.c_str()); svm.save( outsave ); return 0; }
void hogTrain(char* trainFileList, vector<string>& labels) { ifstream fs; fs.open(trainFileList); if (fs == NULL) { cerr << "Error: training file list \"" << trainFileList<< "\" does not exist\n"; exit(-1); } HOGDescriptor hog(hogWinSize, hogBlockSize, hogBlockStride, hogCellSize, hogNBins); DEBUGMODE { cout << "HOG descriptor length = " << hog.getDescriptorSize() << endl; } string label, imgpath; ofstream dataFile; string dataFilePath; dataFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(trainDataFileName); dataFile.open(dataFilePath.c_str()); cout << "Extracting HOG features ...\n"; while (fs >> label) { int catnum = hasCategory(labels, label); if (catnum < 0) { labels.push_back(label); catnum = (int) labels.size() - 1; } fs >> imgpath; DEBUGMODE { cout << "Extracting HOG features for " << imgpath << endl; } Mat img = imread(imgpath); assert(img.data); // image should not be empty Mat resizedImg = Mat::zeros(hogWinSize.width, hogWinSize.height, CV_8UC3); vector<float> featVec; resize(img, resizedImg, hogWinSize, 0, 0, INTER_CUBIC); hog.compute(resizedImg, featVec, hogWinStride); // compute HOG feature // Store features into files following the libsvm dataset format: // <label> 1:<feat_1> 2:<feat_2> ... d:<feat_d> // where d is the number of features (i.e. dimensionality). dataFile << catnum << " "; // write label for (int i = 0; i < featVec.size(); i++) { // write data dataFile << i+1 << ":" << featVec[i] << " "; } dataFile << endl; } fs.close(); dataFile.close(); string svmScaleFilePath, scaledDataFilePath, svmModelFilePath; svmScaleFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(svmScaleRangeFileName); scaledDataFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(scaledTrainDataFileName); svmModelFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(svmModelFileName); string command; // UNIX command // scale data to range [-1 1] command.append(ROOT_DIR).append(LIBSVM_DIR).append("svm-scale -l -1 -u 1 -s ").append(svmScaleFilePath).append(" ").append(dataFilePath).append(" > ").append(scaledDataFilePath); cout << "Scaling training data ... \n"; DEBUGMODE { cout << command << endl; } system(command.c_str()); // execute UNIX command // train SVM classifier command.clear(); svmTrainRoutine.append(svmParam); DEBUGMODE {} else svmTrainRoutine.append("-q ");
int main(int argc, char *args[]) { int cn = 0, srank = 0; query = getenv("QUERY_STRING"); if (query && !strncmp(query, "cn=", 3)) { cn = atoi(query + 3); } if (query && !strncmp(query, "rank=", 5)) { srank = atoi(query + 5); } chdir("/home/merc"); printf("Content-Type: text/html\n\n"); printf("<html><head><title>Server Info</title></head>\n"); printf("<BODY TEXT=#D7D700 BGCOLOR=#264A9F LINK=#FFFFBB VLINK=#CCCC00 ALINK=#FFFF9D background=/gfx/back4.gif>"); printf("<center>"); printf("<table width=\"100%%\"><tr>"); printf( " <table width=\"100%%\">" " <tr>" " <td align=\"center\"><a href=\"http://www.astonia.com/\"><img src=\"/gfx/logo.gif\" width=\"100\" height=\"60\" border=\"0\"></a></td>" " <td align=\"center\">" " <a href=\"/\">Home</a>" " <a href=\"/manual.html\">Manual</a>" " <a href=\"/terms.html\">Terms</a>" " <a href=\"/download.html\">Download</a>" " <a href=\"/contact.html\">Contact</a>" " <a href=\"/cgi-bin/info.cgi\">Server Status</a>" " <a href=\"/cgi-bin/who.cgi\">Who's Online</a>" " <a href=\"/bugs.html\">Bugs</a>" " <a href=\"/changes.html\">Changes</a>" " <a href=\"/creators.html\">Creators</a>" " <a href=\"/links.html\">Links</a>" " <a href=\"/privacy.html\">Privacy</a>" " </td>" " <td align=\"center\"><a href=\"http://www.astonia.com/\"><img src=\"/gfx/logo.gif\" width=\"100\" height=\"60\" border=\"0\"></a></td>" " </tr>" " </table>"); printf("</td></tr></table>"); printf("<img src=/gfx/barsmall.gif border=0 align=left alt=---- width=100%% height=5><br>"); printf("<table width=60%%><tr><td>\n"); if (load()) { printf("<b>Cannot access server data. Exiting... (%s)</b></td></tr></table>", strerror(errno)); exit(0); } if (args[0]) { if (strcmp(args[0], "who.cgi")==0) { who(); } else if (strcmp(args[0], "top.cgi")==0) { top(); } else if (strcmp(args[0], "info.cgi")==0) { info(cn); } else if (strcmp(args[0], "hog.cgi")==0) { hog(); } else if (strcmp(args[0], "gods.cgi")==0) { gods(); } else if (strcmp(args[0], "staff.cgi")==0) { staff(); } else if (strcmp(args[0], "effects.cgi")==0) { effects(); } else if (strcmp(args[0], "xtop.cgi")==0) { xtop(srank); } else if (strcmp(args[0], "dtop.cgi")==0) { dtop(srank); } else { printf("Internal error... (%s)\n", args[0]); } } else { printf("Internal error..."); } unload(); printf("</td></tr></table><br>"); printf( "<img src=\"/gfx/barsmall.gif\" border=0 align=\"left\" alt=\"----\" width=\"100%%\" height=5><br>" "<table width=\"100%%\" cellpadding=0 cellspacing=0 border=0><tr>" "<td width=\"33%%\" align=center><a href=/devel.html>Back to main page</a></td>" "<td width=\"33%%\" align=center> </td>" "<td width=\"33%%\" align=center><font size=-1>All material on this server is copyright " "<a href=mailto:[email protected]>D.Brockhaus</a></font></td>" "</tr></table>" "</center><br><br>" "</body></html>"); return(0); }
int main(int argc, char* argv []) { char inputPath[100], labelPath[100], candPath[100], classifierPath[100], outputPath[100]; printf("please input: \t<images folder>\n\t\t<labels folder>\n\t\t<candidates folder>\n\t\t<classifier>\n\t\t<output detect folder>\n"); scanf("%s", inputPath); scanf("%s", labelPath); scanf("%s", candPath); scanf("%s", classifierPath); scanf("%s", outputPath); int MemDinInicial, MemDinFinal; MemDinInicial = iftMemoryUsed(); iftSVM* svm = iftReadSVM(classifierPath); iftDir* imgsDir = iftLoadFilesFromDirectory(inputPath, "pgm"); iftDir* labelsDir = iftLoadFilesFromDirectory(labelPath, "pgm"); iftDir* candsDir = iftLoadFilesFromDirectory(candPath, "pgm"); if(imgsDir->nfiles!=labelsDir->nfiles ||imgsDir->nfiles!=candsDir->nfiles) { iftError("Different number of images and/or labels and/or candidates.", argv[0]); } iftImage* candImg; iftImage* origImg; iftImage* labelImg; iftImage* maskImg; iftImage* normImg; iftImage* magImg; iftImage* orientImg; iftDataSet* Zt; float meanAcc = 0.0f; float minAcc = 100.0; int DESCRIPTOR_SIZE = (HOG_N1/HOG_N2-HOG_N3+1)*(HOG_M1/HOG_M2-HOG_M3+1)*HOG_N3*HOG_M3*9; for (int i = 0; i <imgsDir->nfiles; ++i) { printf("Image: %s ", imgsDir->files[i]->pathname); labelImg = iftReadImageByExt(labelsDir->files[i]->pathname); origImg = iftReadImageByExt(imgsDir->files[i]->pathname); candImg = iftReadImageByExt(candsDir->files[i]->pathname); normImg = normalize(origImg); gradient(normImg, &magImg, &orientImg); int numCandidates = countNumPixelsCandidatesTotal(candImg); Zt = iftCreateDataSet(numCandidates, DESCRIPTOR_SIZE); iftSetStatus(Zt, TEST); int j=0; for (int p = 0; p < origImg->n; ++p) { if(candImg->val[p] > 0) { iftVoxel v = iftGetVoxelCoord(candImg, p); iftFeatures* feat = hog(magImg, orientImg, v.x, v.y); for (int t = 0; t < feat->n ; t++) { Zt->sample[j].feat[t] = feat->val[t]; Zt->sample[j].id = p; } iftDestroyFeatures(&feat); j++; } } iftSVMClassifyOVO(svm, Zt, TEST); float acc = 0.0f; int numPixelsPlate = 0, numPixels = 0, accNo = 0; maskImg = iftCopyImage(origImg); j=0; for (int p = 0; p < candImg->n; ++p) { if(candImg->val[p] > 0) { if(Zt->sample[j].label == 1) { maskImg->val[p] = origImg->val[p] + 1; if (labelImg->val[p] > 0) { acc+=1.0; } else { accNo++; } numPixels++; } else { maskImg->val[p] = 0; } j++; } else { maskImg->val[p] = 0; } if (labelImg->val[p] > 0) { numPixelsPlate++; } } acc/= numPixelsPlate; if(acc < minAcc) minAcc = acc; printf("Detection precision: %4.2f - %4.2f\n", acc, (float)accNo/numPixels); meanAcc += acc/imgsDir->nfiles; char* detectedfile = iftJoinPathnames(outputPath, iftBasename(imgsDir->files[i]->pathname)); // iftWriteImageP5(origImg, detectedfile); iftWriteImageP5(maskImg, detectedfile); iftDestroyDataSet(&Zt); iftDestroyImage(&candImg); iftDestroyImage(&origImg); iftDestroyImage(&labelImg); iftDestroyImage(&normImg); iftDestroyImage(&magImg); iftDestroyImage(&orientImg); } iftDestroyDir(&imgsDir); iftDestroyDir(&labelsDir); iftDestroySVM(svm); printf("\n\nMean Detection Precision: %4.2f\n", meanAcc); printf("%4.2f\n", minAcc); MemDinFinal = iftMemoryUsed(); if (MemDinInicial!=MemDinFinal) printf("\n\nDinamic memory was not completely deallocated (%d, %d)\n", MemDinInicial,MemDinFinal); return 0; }
int TextRecognizer::recognize(IplImage *input, const struct TextDetectionParams ¶ms, std::string svmModel, std::vector<Chain> &chains, std::vector<std::pair<Point2d, Point2d> > &compBB, std::vector<std::pair<CvPoint, CvPoint> > &chainBB, std::vector<std::string>& text) { // Convert to grayscale IplImage * grayImage = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 1); cvCvtColor(input, grayImage, CV_RGB2GRAY); for (unsigned int i = 0; i < chainBB.size(); i++) { cv::Point center = cv::Point( (chainBB[i].first.x + chainBB[i].second.x) / 2, (chainBB[i].first.y + chainBB[i].second.y) / 2); /* work out if total width of chain is large enough */ if (chainBB[i].second.x - chainBB[i].first.x < input->width / params.maxImgWidthToTextRatio) { LOGL(LOG_TXT_ORIENT, "Reject chain #" << i << " width=" << (chainBB[i].second.x - chainBB[i].first.x) << "<" << (input->width / params.maxImgWidthToTextRatio)); continue; } /* eliminate chains with components of lower height than required minimum */ int minHeight = chainBB[i].second.y - chainBB[i].first.y; for (unsigned j = 0; j < chains[i].components.size(); j++) { minHeight = std::min(minHeight, compBB[chains[i].components[j]].second.y - compBB[chains[i].components[j]].first.y); } if (minHeight < params.minCharacterheight) { LOGL(LOG_CHAINS, "Reject chain # " << i << " minHeight=" << minHeight << "<" << params.minCharacterheight); continue; } /* invert direction if angle is in 3rd/4th quadrants */ if (chains[i].direction.x < 0) { chains[i].direction.x = -chains[i].direction.x; chains[i].direction.y = -chains[i].direction.y; } /* work out chain angle */ double theta_deg = 180 * atan2(chains[i].direction.y, chains[i].direction.x) / PI; if (absd(theta_deg) > params.maxAngle) { LOGL(LOG_TXT_ORIENT, "Chain angle " << theta_deg << " exceeds max " << params.maxAngle); continue; } if ((chainBB.size() == 2) && (absd(theta_deg) > 5)) continue; LOGL(LOG_TXT_ORIENT, "Chain #" << i << " Angle: " << theta_deg << " degrees"); /* create copy of input image including only the selected components */ cv::Mat inputMat = cv::Mat(input); cv::Mat grayMat = cv::Mat(grayImage); cv::Mat componentsImg = cv::Mat::zeros(grayMat.rows, grayMat.cols, grayMat.type()); std::vector<cv::Point> compCoords; for (unsigned int j = 0; j < chains[i].components.size(); j++) { int component_id = chains[i].components[j]; cv::Rect roi = cv::Rect(compBB[component_id].first.x, compBB[component_id].first.y, compBB[component_id].second.x - compBB[component_id].first.x, compBB[component_id].second.y - compBB[component_id].first.y); cv::Mat componentRoi = grayMat(roi); compCoords.push_back( cv::Point(compBB[component_id].first.x, compBB[component_id].first.y)); compCoords.push_back( cv::Point(compBB[component_id].second.x, compBB[component_id].second.y)); compCoords.push_back( cv::Point(compBB[component_id].first.x, compBB[component_id].second.y)); compCoords.push_back( cv::Point(compBB[component_id].second.x, compBB[component_id].first.y)); cv::Mat thresholded; cv::threshold(componentRoi, thresholded, 0 // the value doesn't matter for Otsu thresholding , 255 // we could choose any non-zero value. 255 (white) makes it easy to see the binary image , cv::THRESH_OTSU | cv::THRESH_BINARY_INV); #if 0 cv::Moments mu = cv::moments(thresholded, true); std::cout << "mu02=" << mu.mu02 << " mu11=" << mu.mu11 << " skew=" << mu.mu11 / mu.mu02 << std::endl; #endif cv::imwrite("thresholded.png", thresholded); cv::threshold(componentRoi, componentsImg(roi), 0 // the value doesn't matter for Otsu thresholding , 255 // we could choose any non-zero value. 255 (white) makes it easy to see the binary image , cv::THRESH_OTSU | cv::THRESH_BINARY_INV); } cv::imwrite("bib-components.png", componentsImg); cv::Mat rotMatrix = cv::getRotationMatrix2D(center, theta_deg, 1.0); cv::Mat rotatedMat = cv::Mat::zeros(grayMat.rows, grayMat.cols, grayMat.type()); cv::warpAffine(componentsImg, rotatedMat, rotMatrix, rotatedMat.size()); cv::imwrite("bib-rotated.png", rotatedMat); /* rotate each component coordinates */ const int border = 3; cv::transform(compCoords, compCoords, rotMatrix); /* find bounding box of rotated components */ cv::Rect roi = getBoundingBox(compCoords, cv::Size(input->width, input->height)); /* ROI area can be null if outside of clipping area */ if ((roi.width == 0) || (roi.height == 0)) continue; LOGL(LOG_TEXTREC, "ROI = " << roi); cv::Mat mat = cv::Mat::zeros(roi.height + 2 * border, roi.width + 2 * border, grayMat.type()); cv::Mat tmp = rotatedMat(roi); #if 0 cv::Mat roiMat = inputMat(roi); char *filename_roi; asprintf(&filename_roi, "bib-%05d-%d.png", this->bsid+1, i); cv::imwrite(filename_roi, roiMat); free(filename_roi); #endif /* copy bounded box from rotated mat to new mat with borders - borders are needed * to improve OCR success rate */ tmp.copyTo( mat( cv::Rect(cv::Point(border, border), cv::Point(roi.width + border, roi.height + border)))); /* resize image to improve OCR success rate */ float upscale = 3.0; cv::resize(mat, mat, cvSize(0, 0), upscale, upscale); /* erode text to get rid of thin joints */ int s = (int) (0.05 * mat.rows); /* 5% of up-scaled size) */ cv::Mat elem = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2 * s + 1, 2 * s + 1), cv::Point(s, s)); cv::erode(mat, mat, elem); cv::imwrite("bib-tess-input.png", mat); // Pass it to Tesseract API tess.SetImage((uchar*) mat.data, mat.cols, mat.rows, 1, mat.step1()); // Get the text char* out = tess.GetUTF8Text(); do { if (strlen(out) == 0) { break; } std::string s_out(out); boost::algorithm::trim(s_out); if (s_out.size() != chains[i].components.size()) { LOGL(LOG_TEXTREC, "Text size mismatch: expected " << chains[i].components.size() << " digits, got '" << s_out << "' (" << s_out.size() << " digits)"); break; } /* if first character is a '0' we have a partially occluded number */ if (s_out[0] == '0') { LOGL(LOG_TEXTREC, "Text begins with '0' (partially occluded)"); break; } if (!is_number(s_out)) { LOGL(LOG_TEXTREC, "Text is not a number ('" << s_out << "')"); //break; } /* adjust width to size of 6 digits */ int charWidth = (chainBB[i].second.x - chainBB[i].first.x) / s_out.size(); int width = 6 * charWidth; /* adjust to 2 width/height aspect ratio */ int height = width / 2; int midx = center.x; int midy = center.y; cv::Rect roi = cv::Rect(midx - width / 2, midy - height / 2, width, height); if ((roi.x >= 0) && (roi.y >= 0) && (roi.x + roi.width < inputMat.cols) && (roi.y + roi.height < inputMat.rows)) { cv::Mat bibMat = inputMat(roi); if (s_out.size() <= (unsigned) params.modelVerifLenCrit) { if (svmModel.empty()) { LOGL(LOG_TEXTREC, "Reject " << s_out << " on no model"); break; } if (minHeight < params.modelVerifMinHeight) { LOGL(LOG_TEXTREC, "Reject " << s_out << " on small height"); break; } /* if we have an SVM Model, predict */ CvSVM svm; cv::HOGDescriptor hog(cv::Size(128, 64), /* windows size */ cv::Size(16, 16), /* block size */ cv::Size(8, 8), /* block stride */ cv::Size(8, 8), /* cell size */ 9 /* nbins */ ); std::vector<float> descriptor; /* resize to HOGDescriptor dimensions */ cv::Mat resizedMat; cv::resize(bibMat, resizedMat, hog.winSize, 0, 0); hog.compute(resizedMat, descriptor); /* load SVM model */ svm.load(svmModel.c_str()); float prediction = svm.predict(cv::Mat(descriptor).t()); LOGL(LOG_SVM, "Prediction=" << prediction); if (prediction < 0.5) { LOGL(LOG_TEXTREC, "Reject " << s_out << " on low SVM prediction"); break; } } /* symmetry check */ if ( //(i == 4) && (1)) { cv::Mat inputRotated = cv::Mat::zeros(inputMat.rows, inputMat.cols, inputMat.type()); cv::warpAffine(inputMat, inputRotated, rotMatrix, inputRotated.size()); int minOffset = 0; double min = 1e6; //width = 12 * charWidth; for (int offset = -50; offset < 30; offset += 2) { /* resize to HOGDescriptor dimensions */ cv::Mat straightMat; cv::Mat flippedMat; /* extract shifted ROI */ cv::Rect roi = cv::Rect(midx - width / 2 + offset, midy - height / 2, width, height); if ((roi.x >= 0) && (roi.y >= 0) && (roi.x + roi.width < inputMat.cols) && (roi.y + roi.height < inputMat.rows)) { straightMat = inputRotated(roi); cv::flip(straightMat, flippedMat, 1); cv::Scalar mssimV = getMSSIM(straightMat, flippedMat); double avgMssim = (mssimV.val[0] + mssimV.val[1] + mssimV.val[2]) * 100 / 3; double dist = 1 / (avgMssim + 1); LOGL(LOG_SYMM_CHECK, "offset=" << offset << " dist=" << dist); if (dist < min) { min = dist; minOffset = offset; cv::imwrite("symm-max.png", straightMat); cv::Mat visualImage; } } } LOGL(LOG_SYMM_CHECK, "MinOffset = " << minOffset << " charWidth=" << charWidth); if (absd(minOffset) > charWidth / 3) { LOGL(LOG_TEXTREC, "Reject " << s_out << " on asymmetry"); std::cout << "Reject " << s_out << " on asymmetry" << std::endl; break; } } /* save for training only if orientation is ~horizontal */ if (abs(theta_deg) < 7) { char *filename; std::cout << " ------ " << s_out << std::endl; asprintf(&filename, "bib-%05d-%s.png", this->bsid++, s_out.c_str()); cv::imwrite(filename, bibMat); free(filename); } } else { LOGL(LOG_TEXTREC, "Reject as ROI outside boundaries"); break; } /* all fine, add this bib number */ text.push_back(s_out); LOGL(LOG_TEXTREC, "Bib number: '" << s_out << "'"); } while (0); free(out); } cvReleaseImage(&grayImage); return 0; }
bool HogWrapper::load(XMLNode &root) { if (root.isEmpty()) { SVL_LOG(SVL_LOG_WARNING, "Haar Feature Extractor XML root is empty"); return false; } if (!root.getName()) { SVL_LOG(SVL_LOG_WARNING, "HogWrapper::load given XML file with root with no name."); return false; } if (strcmp(root.getName(), "svlFeatureExtractor") != 0) { SVL_LOG(SVL_LOG_WARNING, "Attempt to read HogWrapper feature extractor from XML node that is not a feature extractor. Expected \"svlFeatureExtractor\", received \""<<root.getName()); return false; } //todo-- add error checking _windowSize.width = atoi(root.getAttribute("width")); _windowSize.height = atoi(root.getAttribute("height")); SVL_LOG(SVL_LOG_MESSAGE, "HogWrapper loaded with size "<<_windowSize.width << "x" << _windowSize.height); _validChannel = atoi(root.getAttribute("validChannel")); cv::Size blockSize; blockSize.width = atoi(root.getAttribute("blockWidth")); blockSize.height = atoi(root.getAttribute("blockHeight")); cv::Size blockStride; blockStride.width = atoi(root.getAttribute("blockStrideX")); blockStride.height = atoi(root.getAttribute("blockStrideY")); int nbins = atoi(root.getAttribute("nbins")); int derivAperture = atoi(root.getAttribute("derivAperture")); double winSigma = atof(root.getAttribute("winSigma")); int histogramNormType = atoi(root.getAttribute("histogramNormType")); double L2HysThreshold = atoi(root.getAttribute("L2HysThreshold")); bool gammaCorrection = atoi(root.getAttribute("gammaCorrection")); cv::Size windowSize(_windowSize); cv::Size cellSize; cellSize.width = atoi(root.getAttribute("cellWidth")); cellSize.height = atoi(root.getAttribute("cellHeight")); _hog = new cv::HOGDescriptor(windowSize, blockSize, cellSize, blockStride, nbins, derivAperture, winSigma, histogramNormType, L2HysThreshold, gammaCorrection); _hog = new cv::HOGDescriptor(windowSize, blockSize, cellSize, blockStride, nbins); cv::HOGDescriptor hog2(windowSize, blockSize, cellSize, blockStride, nbins); assert(_hog); cv::HOGDescriptor hog(cv::Size(64,128), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9); /* char * hogp = (char *) & hog; char * _hogp = (char *) &(*_hog); cout << "Side by side: " << endl; for (unsigned i = 0; i < sizeof(cv::HOGDescriptor); i++) { cout << (int) hogp[i] << " " << (int) _hogp[i]; if ((int) hogp[i] != (int) _hogp[i]) cout << " !!!!!!!!"; cout << endl; } assert(hog.winSize == _hog->winSize); assert(hog.blockSize == _hog->blockSize); assert(hog.blockStride == _hog->blockStride); assert(hog.cellSize == _hog->cellSize); assert(hog.nbins == _hog->nbins); assert(hog.derivAperture == _hog->derivAperture); assert(hog.winSigma == _hog->winSigma); assert(hog.histogramNormType == _hog->histogramNormType); assert(hog.L2HysThreshold == _hog->L2HysThreshold); assert(hog.gammaCorrection == _hog->gammaCorrection); assert(hog.svmDetector.size() == _hog->svmDetector.size()); for (unsigned i = 0; i < hog.svmDetector.size(); i++) assert(hog.svmDetector[i] == _hog->svmDetector[i]); cout << "(right after construction)" << endl; */ return true; }
int main(int argc, char **argv) { if (argc < 5) { cout<<"Usage: "<<argv[0]<<" projMatList rMeanList codeBookList outputBase [windowSize] [stepSize] [intSize]"<<endl; return 0; } string projMatList(argv[1]); string rMeanList(argv[2]); string codeBookList(argv[3]); string outputBase(argv[4]); int windowSize = 100; int stepSize = 100; int intSize = 100; if (argc > 5) windowSize = atoi(argv[5]); if (argc > 6) stepSize = atoi(argv[6]); if (argc > 7) intSize = atoi(argv[7]); if (windowSize % intSize != 0 || stepSize % intSize != 0) { cout<<"Integral size must be the gcd of window size and step size."<<endl; return 0; } string types[5] = {"traj", "hog", "hof", "mbhx", "mbhy"}; vector<TeVector*> tevs(5, NULL); ifstream fin1, fin2, fin3; fin1.open(projMatList.c_str()); if (!fin1.is_open()) { cout<<"Cannot open "<<projMatList<<endl; return 0; } fin2.open(codeBookList.c_str()); if (!fin2.is_open()) { cout<<"Cannot open "<<codeBookList<<endl; return 0; } fin3.open(rMeanList.c_str()); if (!fin3.is_open()) { cout<<"Cannot open "<<rMeanList<<endl; return 0; } string projMatFile, codeBookFile, rMeanFile; //for (int i = 0; i < fvs.size(); i++) { for (int i = 1; i < tevs.size(); i++) { getline(fin1, projMatFile); getline(fin2, codeBookFile); getline(fin3, rMeanFile); tevs[i] = new TeVector(codeBookFile, rMeanFile, projMatFile); tevs[i]->initTeV(intSize); // 1 layer of spatial pyramids } fin1.close(); fin2.close(); fin3.close(); string line; cout<<"Start loading points..."<<endl; while (getline(cin, line)) { DTFeature feat(line); //TODO: Store feature of DT with vector<double> //vector<double> traj(feat.traj, feat.traj+TRAJ_DIM); vector<double> hog(feat.hog, feat.hog+HOG_DIM); vector<double> hof(feat.hof, feat.hof+HOF_DIM); vector<double> mbhx(feat.mbhx, feat.mbhx+MBHX_DIM); vector<double> mbhy(feat.mbhy, feat.mbhy+MBHY_DIM); //tevs[0]->addPoint(traj, feat.frameNum); tevs[1]->addPoint(hog, feat.frameNum); tevs[2]->addPoint(hof, feat.frameNum); tevs[3]->addPoint(mbhx, feat.frameNum); tevs[4]->addPoint(mbhy, feat.frameNum); } cout<<"Points load complete."<<endl; //for (int i = 0; i < tevs.size(); i++) { for (int i = 1; i < tevs.size(); i++) { ofstream fout; string outName = outputBase + "." + types[i] + ".tev.txt"; fout.open(outName.c_str()); vector<double> tev = tevs[i]->getTeV(); fout<<tev[0]; for (int j = 1; j < tev.size(); j++) fout<<" "<<tev[j]; fout<<endl; fout.close(); outName = outputBase + "." + types[i] + ".tev.seq"; fout.open(outName.c_str()); vector<vector<double> > localTeVs = tevs[i]->getTeV(stepSize, windowSize); for (int j = 0; j < localTeVs.size(); j++) { fout<<localTeVs[j][0]; for (int k = 1; k < localTeVs[j].size(); k++) fout<<" "<<localTeVs[j][k]; fout<<endl; } fout.close(); tevs[i]->clearTeV(); } for (int i = 1; i < tevs.size(); i++) //for (int i = 0; i < fvs.size(); i++) delete tevs[i]; return 0; }
int main (int argc, char** argv) { int ret, c; int i, repeat = 5; int cpu = 2; static int errortype = 1; static int verbose = 1; static int disableHuge = 0; static int madvisePoison = 0; static int poll_exit=0; static long length; struct bitmask *nodes, *gnodes; int gpolicy; unsigned long error_opt; void *vaddrmin = (void *)-1UL, *vaddrmax = NULL; static size_t pdcount=0; unsigned long mattr, addrend, pages, count, nodeid, paddr = 0; unsigned long addr_start=0, nodeid_start=-1, mattr_start=-1; unsigned int pagesize = getpagesize(); char pte_str[20]; struct dlook_get_map_info req; static page_desc_t *pdbegin=NULL; page_desc_t *pd, *pdend; length = memsize("100k"); nodes = numa_allocate_nodemask(); gnodes = numa_allocate_nodemask(); progname = argv[0]; while (1) { static struct option long_options[] = { {"verbose", no_argument, &verbose, 1}, {"delay", no_argument, &delay, 1}, {"disableHuge", no_argument, &disableHuge, 1}, {"poll", no_argument, &poll_exit, 1}, {"madvisePoison", no_argument, &madvisePoison, 1}, {"manual", no_argument, &manual, 1}, {"cpu", required_argument, 0, 'c'}, {"errortype", required_argument, 0, 'e'}, {"help", no_argument, 0, 'h'}, {"length", required_argument, 0, 'l'} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "hc:e:l:", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 'c': cpu = atoi(optarg); break; case 'e': errortype = atoi(optarg); break; case 'h': help(); case 'l': /* Not exposed */ printf ("option -l with value `%s'\n", optarg); length = memsize("optarg"); break; case '?': /* getopt_long already printed an error message. */ exit(-1); } } cpu_process_setaffinity(getpid(), cpu); error_opt = get_etype(errortype); buf = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); if (mbind((void *)buf, length, MPOL_DEFAULT, nodes->maskp, nodes->size, 0) < 0){ perror("mbind error\n"); } /* Disable Hugepages */ if (disableHuge) madvise((void *)buf, length, MADV_NOHUGEPAGE); if (madvisePoison) madvise((void *)buf, length,MADV_HWPOISON ); gpolicy = -1; if (get_mempolicy(&gpolicy, gnodes->maskp, gnodes->size, (void *)buf, MPOL_F_ADDR) < 0) perror("get_mempolicy"); if (!numa_bitmask_equal(gnodes, nodes)) { printf("nodes differ %lx, %lx!\n", gnodes->maskp[0], nodes->maskp[0]); } strcpy(pte_str, ""); addrend = ((unsigned long)buf)+length; pages = (addrend-((unsigned long)buf))/pagesize; if (pages > pdcount) { pdbegin = realloc(pdbegin, sizeof(page_desc_t)*pages); pdcount = pages; } req.pid = getpid(); req.start_vaddr = (unsigned long)buf; req.end_vaddr = addrend; req.pd = pdbegin; sigaction(SIGBUS, &recover_act, NULL); /*Fault in Pages */ if(!poll_exit) hog((void *)buf, length); /* Get mmap phys_addrs */ if ((fd = open(UVMCE_DEVICE, O_RDWR)) < 0) { printf("Failed to open: %s\n", UVMCE_DEVICE); exit (1); } if (ioctl(fd, UVMCE_DLOOK, &req ) < 0){ printf("Failed to INJECT_UCE\n"); exit(1); } process_map(pd,pdbegin, pdend, pages, buf, addrend, pagesize, mattr, nodeid, paddr, pte_str, nodeid_start, mattr_start, addr_start); printf("\n\tstart_vaddr\t 0x%016lx length\t 0x%x\n\tend_vaddr\t 0x%016lx pages\t %ld\n", buf , length, addrend, pages); uv_inject(pd,pdbegin, pdend, pages, (unsigned long)buf, addrend, pagesize, mattr, nodeid, paddr, pte_str, nodeid_start, mattr_start, addr_start, error_opt); if (delay){ printf("Enter char to consume bad memory.."); getchar(); } if (error_opt != UVMCE_PATROL_SCRUB_UCE){ consume_it((void *)buf, length); } out: close(fd); return 0; }