#include #include #include #include #include extern void* init_template_match(void); extern int add_image_template(void* ptemplate_match, const char* template_img_path, const char* label, int x, int y , int width, int height); extern void* init_image_feature(void* ptemplate_match, const char* img_path); extern float match_image(void* ptemplate_match, void* pimage_feature, char* label); extern void release_image_feature(void* pimage_feature); extern void release_template_match(void* ptemplate_match); char template_file_buffer[128] = {"templates.txt"}; char test_file_buffer[128] = {"test.txt"}; char test_label_buffer[128] = {"anhuiweishi"}; char* g_pTemplateFile = template_file_buffer; char* g_pTestFile = test_file_buffer; char* g_plabel = test_label_buffer; int g_firstN = 1; char helpBuffer[128] = "usage:logoRecognition -t templates -i image [-l label]\n"; int read_template_file(const char* templates_file, void* ptm) { if(!templates_file) { printf("templates_file is null!\n"); exit(-1); } FILE *fp = fopen(templates_file, "r+"); if(!fp) { printf("open templates_file error\n"); exit(-1); } char record[128]={0}; char *label, *x, *y, *width, *height; x = y = width = height = NULL; while(fgets(record, 128, fp) != NULL) { int pos = 0; //strip the end '\n' while(record[pos] != '\0' && record[pos] != '\n') pos++; if(record[pos] == '\n') record[pos] = '\0'; //seperate filename and label by ',' pos = 0; while(record[pos] != '\0' && record[pos] != ',') pos++; if(record[pos] == '\0') { printf("templates_file format error\n"); exit(-1); } record[pos] = '\0'; label = record + pos + 1; //get x pos++; while(record[pos] != '\0' && record[pos] != ',') pos++; if(record[pos] == '\0') { printf("templates_file format error\n"); exit(-1); } record[pos] = '\0'; x = record + pos + 1; //get y pos++; while(record[pos] != '\0' && record[pos] != ',') pos++; if(record[pos] == '\0') { printf("templates_file format error\n"); exit(-1); } record[pos] = '\0'; y = record + pos + 1; //get width pos++; while(record[pos] != '\0' && record[pos] != ',') pos++; if(record[pos] == '\0') { printf("templates_file format error\n"); exit(-1); } record[pos] = '\0'; width = record + pos + 1; //get height pos++; while(record[pos] != '\0' && record[pos] != ',') pos++; if(record[pos] == '\0') { printf("templates_file format error\n"); exit(-1); } record[pos] = '\0'; height = record + pos + 1; add_image_template(ptm, record, label, atoi(x), atoi(y), atoi(width), atoi(height)); } fclose(fp); return 0; } int parse_args(int argc, char**argv) { if(argc != 5 && argc != 7) { printf("%s", helpBuffer); exit(-1); } if( strcmp(argv[1] ,"-t") == 0 && strcmp(argv[3] , "-i") == 0) { sprintf(template_file_buffer, "%s", argv[2]); sprintf(test_file_buffer, "%s", argv[4]); } else if( strcmp(argv[3] ,"-t") == 0 && strcmp(argv[1] , "-i") == 0 ) { sprintf(template_file_buffer, "%s", argv[4]); sprintf(test_file_buffer, "%s", argv[2]); } else { printf("%s", helpBuffer); exit(-1); } if(argc == 7 && strcmp(argv[5], "-l") == 0) { sprintf(test_label_buffer, "%s", argv[6]); } return 0; } int main(int argc, char** argv) { parse_args(argc, argv); void *ptm = init_template_match(); if(!ptm) { printf("init template match error, abort\n"); exit(-1); } read_template_file(g_pTemplateFile, ptm); void *pfeature = init_image_feature(ptm, g_pTestFile); if(!pfeature) { printf("init image feature error, abort\n"); exit(-1); } char result[32] = {0}; float score = match_image(ptm, pfeature, g_plabel); printf("the match score is :%03f template:%20s\n", score, g_plabel); release_image_feature(pfeature); release_template_match(ptm); return 0; }