int main() { CamImage imodel[3], image, color_image; CamKeypoints points[3], points2; int i, j; CamKeypointsMatches matches; char filename[256]; char *model_images[] = {"edog5", "dvd", "mrpotato4"}; #define DISPLAYED_MODEL 2 #define NB_SCENES 5 char *test_images[NB_SCENES] = {"scene1", "scene3", "scene4", "scene5", "scene7"}; const int cx[3] = {450, 390, 550}; const int cy[3] = {340, 510, 450}; const int width[3] = {720, 690, 500}; const int height[3] = {640, 910, 500}; CamAffineTransform t; int error, c, x1, y1, x2, y2, score, nbFeatures; CamPoint xy[7], uv[7]; CamImage dimage; CamROI roi; const int nb_keypoints = 500; for (i = 0; i < 3; i++) { sprintf(filename, "resources/photos/%s.bmp", model_images[i]); printf("Feature point detection on %s ...\n", model_images[i]); imodel[i].imageData = NULL; camLoadBMP(&imodel[i], filename); /* camAllocateImage(&image, imodel[i].width, imodel[i].height, CAM_DEPTH_8U); camRGB2Y(&imodel[i], &image); */ camAllocateYUVImage(&image, imodel[i].width, imodel[i].height); camRGB2YUV(&imodel[i], &image); camAllocateKeypoints(&points[i], 0); points[i].id = i; camFastHessianDetector(&image, &points[i], 100, CAM_UPRIGHT); /* camDrawKeypoints(&points[i], &image, 128); sprintf(filename, "output/%s.pgm", model_images[i]); camSavePGM(&image, filename); */ camDeallocateImage(&image); } camAllocateRGBImage(&dimage, imodel[0].width, imodel[0].height * 2); camSetROI(&roi, 0, 0, imodel[0].height, imodel[0].width, imodel[0].height); camAllocateKeypointsMatches(&matches, 2048); nbFeatures = 0; score = 0; for (i = 0; i < NB_SCENES; i++) { sprintf(filename, "resources/photos/%s.bmp", test_images[i]); printf("Feature point detection on %s ...\n", test_images[i]); color_image.imageData = NULL; camLoadBMP(&color_image, filename); /* camAllocateImage(&image, color_image.width, color_image.height, CAM_DEPTH_8U); camRGB2Y(&color_image, &image); */ camAllocateYUVImage(&image, color_image.width, color_image.height); camRGB2YUV(&color_image, &image); camAllocateKeypoints(&points2, 0); camFastHessianDetector(&image, &points2, nb_keypoints, CAM_UPRIGHT); printf("# features = %d\n", points2.nbPoints); nbFeatures += points2.nbPoints; // Create result image dimage.roi = NULL; camCopy(&imodel[DISPLAYED_MODEL], &dimage); dimage.roi = &roi; camCopy(&color_image, &dimage); dimage.roi = NULL; camDrawKeypoints(&points[DISPLAYED_MODEL], &dimage, 128); for (j = 0; j < 3; j++) { camKeypointsMatching2(&points[j], &points2, &matches); // Find affine parameters if (camFindAffineTransform2(&matches, &t, &error)) { score += matches.nbMatches - matches.nbOutliers; if (j == DISPLAYED_MODEL) { // Draw lines between model and target for (c = 0; c < matches.nbMatches; c++) { if (matches.pairs[c].mark != -1) { camDrawKeypoint(matches.pairs[c].p1, &dimage, CAM_RGB(255, 0, 0)); x1 = matches.pairs[c].p1->x; y1 = matches.pairs[c].p1->y; x2 = matches.pairs[c].p2->x; y2 = matches.pairs[c].p2->y; y2 += image.height; camDrawLine(&dimage, x1, y1, x2, y2, CAM_RGB(0, 255, 0)); } } for (c = 0; c < matches.nbMatches; c++) { matches.pairs[c].p2->y += image.height; camDrawKeypoint(matches.pairs[c].p2, &dimage, 128); } } // Draw box on model and target xy[0].x = xy[3].x = cx[j] - width[j] / 2; xy[0].y = xy[1].y = cy[j] - height[j] / 2; xy[1].x = xy[2].x = cx[j] + width[j] / 2; xy[2].y = xy[3].y = cy[j] + height[j] / 2; xy[4].x = xy[5].x = cx[j]; xy[4].y = xy[6].y = cy[j]; xy[5].y = cy[j] - height[j] / 4; xy[6].x = cx[j] + width[j] / 4; for (c = 0; c < 7; c++) { camApplyAffineTransform(&xy[c], &uv[c], &t); uv[c].y += image.height; } if (j == DISPLAYED_MODEL) { camDrawLine(&dimage, xy[0].x, xy[0].y, xy[1].x, xy[1].y, 0); camDrawLine(&dimage, xy[2].x, xy[2].y, xy[1].x, xy[1].y, 0); camDrawLine(&dimage, xy[2].x, xy[2].y, xy[3].x, xy[3].y, 0); camDrawLine(&dimage, xy[0].x, xy[0].y, xy[3].x, xy[3].y, 0); camDrawLine(&dimage, xy[4].x, xy[4].y, xy[5].x, xy[5].y, 0); camDrawLine(&dimage, xy[6].x, xy[6].y, xy[4].x, xy[4].y, 0); } camDrawLine(&dimage, uv[0].x, uv[0].y, uv[1].x, uv[1].y, 0); camDrawLine(&dimage, uv[2].x, uv[2].y, uv[1].x, uv[1].y, 0); camDrawLine(&dimage, uv[2].x, uv[2].y, uv[3].x, uv[3].y, 0); camDrawLine(&dimage, uv[0].x, uv[0].y, uv[3].x, uv[3].y, 0); camDrawLine(&dimage, uv[4].x, uv[4].y, uv[5].x, uv[5].y, 0); camDrawLine(&dimage, uv[6].x, uv[6].y, uv[4].x, uv[4].y, 0); } } sprintf(filename, "output/%s.bmp", test_images[i]); camSaveBMP(&dimage, filename); camDeallocateImage(&image); camDeallocateImage(&color_image); camFreeKeypoints(&points2); } for (i = 0; i < 3; i++) { camDeallocateImage(&imodel[i]); camFreeKeypoints(&points[i]); } camDeallocateImage(&dimage); camFreeKeypointsMatches(&matches); printf("# features in scenes = %d\n", nbFeatures); printf("# matching features = %d (%lg%%)\n", score, score*100.0/nbFeatures); }
int camLoadBitmapFont(CamBitmapFont *font, char *filename) { CamImage bitmap; CamROI roi; int delimiter_color,x,i,px; unsigned char *ptr; CamTable clusters,LUT; CamRLEImage temp; char s[256]; if (!camLoadBMP(&bitmap,filename)) { sprintf(s,"Couldn't load bitmap file %s.",filename); camError("camBitmapFontLoad",s); return 0; } font->height=bitmap.height-1; // Try to figure out how many letters are there in this font image font->nb_chars=0; font->first_char=33; #define GET_COLOR_PIXEL(ptr) (((int)*(ptr))+(((int)*((ptr)+1))<<8)+(((int)*((ptr)+2))<<16)) ptr=bitmap.imageData; delimiter_color=GET_COLOR_PIXEL(ptr); for (x=0;x<bitmap.width;x++,ptr+=3) { if (GET_COLOR_PIXEL(ptr)==delimiter_color) font->nb_chars++; } // OK, now we can allocate memory for these font->masks=(CamRLEImage*)malloc(sizeof(CamRLEImage)*font->nb_chars); font->letters=(CamImage*)malloc(sizeof(CamImage)*font->nb_chars); // Let's prepare the data for font loading ptr=bitmap.imageData; px=0; roi.coi=0; bitmap.roi=&roi; clusters.size=12; for (i=0;i<6;i++) clusters.t[i*2]=clusters.t[i*2+1]=*(ptr+i); i=0; LUT.size=3; LUT.t[0]=1; LUT.t[1]=0; LUT.t[2]=0; camRLEAllocate(&temp,10000); // Ready. Let's go for all the characters for (x=1,ptr+=3;x<bitmap.width;x++,ptr+=3) { if (GET_COLOR_PIXEL(ptr)==delimiter_color) { // We've found the next character camRLEAllocate(&font->masks[i],(x-px)*font->height+2); camAllocateRGBImage(&font->letters[i],x-px,font->height); roi.xOffset=px; roi.yOffset=1; roi.width=x-px; roi.height=font->height; camCopy(&bitmap,&font->letters[i]); camRLEEncodeColor(&bitmap,&temp,&clusters); camRLEApplyLUT(&temp,&font->masks[i],&LUT); px=x; i++; } } // We've found the last character camRLEAllocate(&font->masks[i],(x-px)*font->height+2); camAllocateRGBImage(&font->letters[i],x-px,font->height); roi.xOffset=px; roi.yOffset=1; roi.width=x-px; roi.height=font->height; camCopy(&bitmap,&font->letters[i]); camRLEEncodeColor(&bitmap,&temp,&clusters); camRLEApplyLUT(&temp,&font->masks[i],&LUT); // Set the masks to all letters for (i=0;i<font->nb_chars;i++) { font->letters[i].mask=&font->masks[i]; } camDeallocateImage(&bitmap); camRLEDeallocate(&temp); return 1; }