int main(int argc, char *argv[]) { argc--; argv++; if (argc != 4) { fprintf(stderr, "pm_minimal a b ann annd\n" "Given input images a, b outputs nearest neighbor field 'ann' mapping a => b coords, and the squared L2 distance 'annd'\n" "These are stored as RGB 24-bit images, with a 24-bit int at every pixel. For the NNF we store (by<<12)|bx.\n"); exit(1); } printf("Loading input images\n"); BITMAP *a = load_bitmap(argv[0]); BITMAP *b = load_bitmap(argv[1]); BITMAP *ann = NULL, *annd = NULL; printf("Running PatchMatch\n"); patchmatch(a, b, ann, annd); printf("Saving output images\n"); save_bitmap(ann, argv[2]); save_bitmap(annd, argv[3]); return 0; }
int main(int argc,char *argv[]) { float beta; int* tab; int xmin; int xmax; int ymax; int ymin; // Loading image char* imagePath = argv[1]; cimg_library::CImg<unsigned char> image(imagePath); // Patch size int w; cout << "Please enter size of Patch (must be odd and positive): "; cin >> w; bool odd = (w-(w/2)*2!=0); assert(odd); bool positive = (w>=0); assert(positive); // Minimum offset size int tau; cout << "Please enter minimum offset value (must be positive): "; cin >>tau; positive = (tau>=0); assert(positive); // Random search parameter float alpha; cout << "Please enter alpha (random search parameter) value (must be between 0 and 1): "; cin >> alpha; positive = (alpha>=0); assert(positive); bool inferior_one = (alpha<1); assert(inferior_one); // Number of vectors to keep int nb; cout << "Please enter the number of offsets to keep: "; cin >> nb; positive = (nb>=0); assert(positive); // Loading offset field int** offsetField = patchmatch(image,w,tau,alpha); int** tabVect = occurrenceOffsets(offsetField,image,w,nb); while(true){ // Zone to complete tab = getXYImage(image); xmin = tab[0]; ymin = tab[1]; if (tab[0]==-1){return 0;} tab = getXYImage(image); xmax = tab[0]; ymax = tab[1]; if (tab[0]==-1){return 0;} cout << "Please enter Beta value: "; cin >> beta; int width = image.width(); int height = image.height(); cimg_library::CImg<unsigned char> newImage(width,height,1,3); newImage = graphcut(image,tabVect,xmin,xmax,ymin,ymax,w,beta,nb); displayImage(newImage); } return 0; }
static IplImage* fill(IplImage *a, IplImage *target, IplImage *mask) { int i, ax, ay, w = a->width, h = a->height; int aew = w - PATCH_W, aeh = h - PATCH_W; int tw = target->width, th = target->height; CvSize size = cvGetSize(a); IplImage *ann = cvCreateImage(size, IPL_DEPTH_32S, 1); IplImage *annd = cvCreateImage(size, IPL_DEPTH_32S, 1); IplImage *d = cvCreateImage(size, a->depth, a->nChannels); IplImage *acc = cvCreateImage(size, IPL_DEPTH_32F, a->nChannels+1); IplImage *imask = cvCreateImage(size, mask->depth, mask->nChannels); int32_t *data = (int32_t*)ann->imageData; int32_t *ddata = (int32_t*)annd->imageData; int stride = ann->widthStep/sizeof(int32_t); memset(acc->imageData, 0, acc->imageSize); IplImage *mask8u = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage *imask8u = cvCreateImage(size, IPL_DEPTH_8U, 1); cvConvertScale(mask, imask, -1, 1); cvConvertScale(mask, mask8u, 256, 0); cvConvertScale(mask8u, imask8u, -2, 256); printf("recursing %dx%d\n", w, h); if (w > 32 && h > 32 && tw > 32 && th > 32) { CvSize ssmall = {w/2, h/2}, tsmall = {tw/2, th/2}; IplImage *ssrc, *stgt, *smsk, *newtgt; ssrc = cvCreateImage(ssmall, a->depth, a->nChannels); stgt = cvCreateImage(tsmall, target->depth, target->nChannels); smsk = cvCreateImage(ssmall, mask->depth, mask->nChannels); cvResize(a, ssrc, CV_INTER_LINEAR); cvResize(target, stgt, CV_INTER_LINEAR); cvResize(mask, smsk, CV_INTER_LINEAR); newtgt = fill(ssrc, stgt, smsk); cvResize(newtgt, target, CV_INTER_LINEAR); cvReleaseImage(&ssrc); cvReleaseImage(&stgt); cvReleaseImage(&smsk); } for (i= 0; i< PM_ITERS; i++) { // completeness term patchmatch(a, target, ann, annd, imask); xy2img(ann, target, d); cvShowImage("completeness", d); for (ay = 0; ay < aeh; ay++) { for (ax = 0; ax < aew; ax++) { int xy = ay * stride + ax; int v = *(data + xy); int xbest = XY_TO_X(v), ybest = XY_TO_Y(v); int dbest = *(ddata + xy); accum(target, acc, xbest, ybest, ax, ay, dbest, imask); } } // coherence term patchmatch(target, a, ann, annd, mask); xy2img(ann, a, d); cvShowImage("coherence", d); for (ay = 0; ay < aeh; ay++) { for (ax = 0; ax < aew; ax++) { int xy = ay * stride + ax; int v = *(data + xy); int xbest = XY_TO_X(v), ybest = XY_TO_Y(v); int dbest = *(ddata + xy); accum(a, acc, xbest, ybest, ax, ay, dbest, mask); } } normalize(target, acc, mask); cvCopy(a, target, imask8u); } printf("finished %dx%d\n", w, h); //cvWaitKey(0); cvReleaseImage(&d); cvReleaseImage(&mask8u); cvReleaseImage(&imask); cvReleaseImage(&ann); cvReleaseImage(&annd); cvReleaseImage(&acc); return target; }