void event_mouse(int button, int state, int x, int y) { int err; if (state == GLUT_DOWN) { // Mouse down = find template // Do hough transform to find ball center and radius struct frame *fr = get_frame(active_window->frames, active_window->cur); err = houghTransform(active_window, active_window->cur, x, y); if (err) { return; } fr->flag |= HAS_HOUGH; active_window->guess.x = fr->hough.x; active_window->guess.y = fr->hough.y; // Make a subimage containing the template CvRect r = cvRect(fr->hough.x-fr->hough.radius, fr->hough.y-fr->hough.radius, fr->hough.radius*2, fr->hough.radius*2); CvMat *sub = cvCreateMatHeader(fr->hough.radius*2, fr->hough.radius*2, CV_32FC1); cvGetSubRect(fr->image, sub, r); active_window->tmpl = sub; // Match (could be left out) templateMatch(active_window, active_window->cur, MARGIN, sub); fr->flag |= HAS_MATCH; glutPostRedisplay(); // Calculate meters per pixel active_window->mpp = atof(active_window->argv[2])/(fr->hough.radius*2); printf("Getting mpp: %f/%f = %f\n", atof(active_window->argv[2]), fr->hough.radius*2, active_window->mpp); } }
Mat PAN::preprocess(Mat *image, Image *output,int parameter,String database){ output->x_left= 0; output->x_right = image->cols;; output->y_upper = 0; output->y_lower = image->rows; if (image->channels()>=2){ cvtColor(*image, *output->img, CV_BGR2GRAY); } //cropimage globally to get pan card and put it in output image if (parameter == 1){ //assuming we have only the pancard as the image //going to remove emblem Point matchloc; float percentage, threshold; database =database+ "emblem.png"; Mat temp = imread(database); if (temp.data == NULL){ cout << "hi there"; } templateMatch(*image, temp, matchloc, threshold, percentage); int width, height; width = temp.size().width; height = temp.size().height; Point boundary(matchloc.x + width, matchloc.y + height); if (percentage >= 85){ output->y_upper = boundary.y; } Mat mod(*image, Rect(Point(output->x_left, output->y_upper), Point(output->x_right, output->y_lower))); //imshow("test1", mod); //waitKey(); return mod; } else if (parameter == 2){ //initializing panimage by removing the borders output->img = image; CropImage(output,10,10,0); return *output->img; } }
int templateMatch(struct window *window, int frame, int diam, CvMat *tmpl) { // Init struct frame *fr = get_frame(window->frames, frame); // printf("Guess is (%d, %d), diameter is %d\n", window->guess.x, window->guess.y, diam); float init_x = (float)window->guess.x-diam, init_y = (float)window->guess.y-diam; // See if we can guess were the ball might be CvRect rect = cvRect(init_x, init_y, diam*2, diam*2); // Make sure rect is with image rect.x = rect.x < 0 ? 0 : rect.x; rect.y = rect.y < 0 ? 0 : rect.y; rect.width = rect.x+rect.width > fr->image->cols ? fr->image->cols-rect.x : rect.width; rect.height = rect.y+rect.height > fr->image->rows ? fr->image->rows-rect.y : rect.height; // Get sub rect CvMat *sub = cvCreateMatHeader(rect.height, rect.width, CV_32F); cvGetSubRect(fr->image, sub, rect); CvMat *res = cvCreateMat(sub->rows - tmpl->rows+1, sub->cols - tmpl->cols+1, CV_32F); // Match cvMatchTemplate(sub, tmpl, res, CV_TM_SQDIFF); // Find value and location of min = upper-left corner of template match CvPoint pt; double val; cvMinMaxLoc(res, &val, 0, &pt, 0, 0); // printf("#%d: value of match is %f\n", frame, val); if (val > 20000000) { // Works on sample video // printf("Doubling search area\n"); templateMatch(window, frame, diam*2, tmpl); return 0; } // Match result struct MatchResult mr; mr.x = init_x+pt.x; mr.y = init_y+pt.y; mr.found = 1; fr->match = mr; window->guess.x = mr.x; window->guess.y = mr.y; return 0; }
/********************** * * Event handlers * * *********************/ void event_key_special(int key, int x, int y) { if (active_window->state == STATE_LOADING) { return; } int step = 0, i; switch (key) { case GLUT_KEY_LEFT: step = -1; break; case GLUT_KEY_RIGHT: step = 1; break; case GLUT_KEY_UP: step = 10; break; case GLUT_KEY_DOWN: step = -10; break; case GLUT_KEY_HOME: // Perform match on all frames for (i = 1; i <= active_window->nfr; i++) { struct frame *fr = get_frame(active_window->frames, i); if (fr->flag & HAS_MATCH) { continue; } templateMatch(active_window, i, MARGIN, active_window->tmpl); fr->flag |= HAS_MATCH; calculate_speed(fr); } printf("Done matching\n"); } // Update view if (active_window->cur+step > 0 && active_window->cur+step <= active_window->nfr) { active_window->cur += step; glutPostRedisplay(); } }