📄 haarscan.cc
字号:
const char *help = "\progname: haarscan.cc\n\code2html: This program detects objects (such as faces) in an image using a cascade of haar-like classifiers.\n\version: Torch3 vision2.0, 2004-2005\n\(c) Yann Rodriguez (rodrig@idiap.ch) and Sebastien Marcel (marcel@idiap.ch)\n";// image#include "ImageGray.h"#include "ImageRgb.h"#include "Rectangle2D.h"// image loader#include "xtprobeImageDiskXFile.h"#include "jpegDiskXFile.h"// pattern detector#include "PatternDetect.h"#include "MSPatternDetect.h"#include "NonOrientedPatternFusion.h"#include "PatternMerger.h"#include "ipSWhaar.h"// edge#include "ipIntegralImage.h" #include "ipSobel.h"// misc#include "DiskXFile.h"#include "CmdLine.h"#include "MTimer.h"using namespace Torch;int main(int argc, char **argv){ char *image_filename; bool verbose; int patternWmin; int patternHmin; int minWsize; int maxWsize; real scale_factor; real stepx_factor; real stepy_factor; real MeanMin; real MeanMax; real StdvMin; real StdvMax; bool pruning; real threshold_edges_min; real threshold_edges_max; char *model_filename; real model_threshold; bool realstump; real surfoverlap_fusion; real threshold_activation; char *dirname; int nbest; bool savepos; bool draw; bool savejpg; // Construct the command line. // --------------------------- CmdLine cmd; cmd.setBOption("write log", false); cmd.addText("\nArguments:"); cmd.addSCmdArg("image filename", &image_filename, "image filename"); cmd.addText("\nOptions:"); cmd.addBCmdOption("-verbose", &verbose, false, "verbose"); cmd.addText("\nScanning Options:"); cmd.addICmdOption("-patternWmin", &patternWmin, 19, "pattern width min"); cmd.addICmdOption("-patternHmin", &patternHmin, 19, "pattern height min"); cmd.addICmdOption("-minWsize", &minWsize, -1, "width min"); cmd.addICmdOption("-maxWsize", &maxWsize, -1, "width max"); cmd.addRCmdOption("-scalefactor", &scale_factor, 0.125, "scale factor"); cmd.addRCmdOption("-stepxfactor", &stepx_factor, 0.08, "step x factor"); cmd.addRCmdOption("-stepyfactor", &stepy_factor, 0.1, "step y factor"); cmd.addText("\nPruning Options:"); cmd.addBCmdOption("-pruning", &pruning, false, "use pruning"); cmd.addRCmdOption("-mmin", &MeanMin, 0.041, "mean min"); cmd.addRCmdOption("-mmax", &MeanMax, 0.890, "mean max"); cmd.addRCmdOption("-vmin", &StdvMin, 0.0204354, "variance min"); cmd.addRCmdOption("-vmax", &StdvMax, 0.382121, "variance max"); cmd.addRCmdOption("-emin", &threshold_edges_min, 0.1, "edge threshold min"); cmd.addRCmdOption("-emax", &threshold_edges_max, 0.5, "edge threshold max"); cmd.addText("\nModel Options:"); cmd.addSCmdOption("-model", &model_filename, "./models/haar5-stump-cascade19x19-12-2-150", "haar cascade model filename"); cmd.addRCmdOption("-threshold", &model_threshold, -0.0705, "Model threshold"); cmd.addBCmdOption("-realstump", &realstump, false, "real stump classifiers"); cmd.addText("\nFusion Options:"); cmd.addRCmdOption("-surfoverlapfusion", &surfoverlap_fusion, 0.2, "threshold for fusion of overlapped patterns"); cmd.addRCmdOption("-threshold_activation", &threshold_activation, 1.5, "threshold after fusion"); cmd.addText("\nMiscellaneous Options:"); cmd.addSCmdOption("-dir", &dirname, ".", "directory to store ouput files"); cmd.addBCmdOption("-savepos", &savepos, false, "save pos file"); cmd.addBCmdOption("-draw", &draw, false, "draw ppm image with detections"); cmd.addBCmdOption("-savejpg", &savejpg, false, "save in jpeg instead of jpeg"); cmd.addICmdOption("-nbest", &nbest, -1, "nbest detections (all if -1)"); cmd.read(argc, argv); Allocator *allocator = new Allocator; // extract basename from filename. // -------------------------------- char basename[256]; char *extension; char *separator; strcpy(basename, image_filename); extension = (char *) strrchr(basename, '.'); if(extension != NULL) *extension = '\0'; separator = (char *) rindex(basename, '/'); if(separator != NULL) { separator++; strcpy(basename, separator); } // load image to scan. // -------------------- Image *image; ImageDiskXFile *image_file; image_file = new(allocator) xtprobeImageDiskXFile(image_filename, "r"); image = new(allocator) ImageGray(); image->setBOption("verbose", verbose); image->loadImageXFile(image_file); allocator->free(image_file); int width = image->width; int height = image->height; Sequence *realimage = new(allocator) Sequence(1, width * height); for(int i = 0 ; i < width * height ; i++) realimage->frames[0][i] = image->data[i] / 255.0; // pruning. // --------- real *pruning_ii = NULL; if(pruning) { ipCore *edges = new(allocator) ipSobel(width, height, "float"); edges->setROption("threshold", 0.4); Sequence *realedges = new(allocator) Sequence(&edges->seq_out->frames[3], 1, width * height); ipCore *integralimagemachine_edges = new(allocator) ipIntegralImage(width, height, "gray"); edges->init(); edges->process(realimage); integralimagemachine_edges->init(); integralimagemachine_edges->process(realedges); pruning_ii = integralimagemachine_edges->seq_out->frames[0]; } // Detection. // ----------- PatternDetect *patterndetect; ipSubWindow *ip_sw = NULL; PatternMerger *merger = new(allocator) meanRectPatternMerger(); PatternFusion *pfusion; pfusion = new(allocator)NonOrientedPatternFusion(merger, 0, 100); pfusion->setParams(surfoverlap_fusion, model_threshold, true, threshold_activation); ipIntegralImage *ip_ii; ip_ii = new(allocator) ipIntegralImage(width, height, "gray", true); ip_sw = new(allocator) ipSWhaar(ip_ii->seq_out->frames[0], ip_ii->seq_out->frames[1], width, height, patternWmin, patternHmin, model_filename, model_threshold, verbose, realstump); patterndetect = new(allocator) MSPatternDetect( ip_sw, pfusion, ip_ii, width, height, patternWmin, patternHmin, minWsize, maxWsize, stepx_factor, stepy_factor, scale_factor, MeanMin, MeanMax, StdvMin, StdvMax, pruning_ii, threshold_edges_min, threshold_edges_max, 1000, verbose); // Processing. // --------------- MTimer *timer = new(allocator) MTimer(); timer->reset(); patterndetect->init(); patterndetect->process(realimage); patterndetect->stat(); timer->stop(); print("processing time: %d' %d'' %dms\n\n", timer->minutes, timer->seconds, timer->mseconds); // DiskXFile *posoutput = NULL; char str[250]; if(patterndetect->n_patterns > 0) { ImageRgb *outputimage = NULL; if(draw) { outputimage = new(allocator) ImageRgb(width, height); outputimage->copyFrom(image); } if(savepos) { sprintf(str, "%s/%s.pos", dirname, basename); posoutput = new(allocator) DiskXFile(str, "w"); } Color facecolor = green; Rectangle2D rect; int P = patterndetect->n_patterns; if(nbest != -1) { if(nbest < P) P = nbest; print("saving %d-bests\n", P); } if(savepos) posoutput->printf("%d\n", P); for(int i = 0 ; i < P ; i++) { if(verbose) print("DETECTION [%02d] %d %d %d %d %g %d %g %g\n", i, patterndetect->patterns[i]->x, patterndetect->patterns[i]->y, patterndetect->patterns[i]->w, patterndetect->patterns[i]->h, patterndetect->patterns[i]->scale, patterndetect->patterns[i]->view, patterndetect->patterns[i]->confidence, patterndetect->patterns[i]->activation); rect.reset( patterndetect->patterns[i]->x, patterndetect->patterns[i]->y, patterndetect->patterns[i]->w, patterndetect->patterns[i]->h); if(draw) rect.draw(outputimage, facecolor); if(savepos) posoutput->printf("%d %d %d %d\n", patterndetect->patterns[i]->x, patterndetect->patterns[i]->y, patterndetect->patterns[i]->w, patterndetect->patterns[i]->h); } if(verbose) print("\n"); if(draw) { if(savejpg) { sprintf(str, "%s/%s-detect.jpg", dirname, basename); jpegDiskXFile *jpeg_file = new(allocator) jpegDiskXFile(str, "w"); jpeg_file->writeHeader(outputimage->width, outputimage->height); jpeg_file->writePixmap(outputimage->pixmap); allocator->free(jpeg_file); } else { sprintf(str, "%s/%s-detect.ppm", dirname, basename); outputimage->save(str); } allocator->free(outputimage); } } delete allocator; return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -