void DifferenceCut::rankImages (std::vector<RowSpan>& spans, std::priority_queue<Ranker>& ordered) { assert(ordered.empty()); float score, dist; const unsigned char *Itest, *Iavoid; Vec3i Vtest, Vavoid; int x, y, index; for (unsigned int i=0; i<_images.size(); ++i) { score = 0; ImageAbs* im = _images[i]; for (unsigned int j=0; j<spans.size(); ++j) { x = spans[j]._x; y = spans[j]._y; for (int s=0; s<spans[j]._num; ++s) { index = y*_w + x + s; Itest = im->data(x+s,y); Iavoid = _imptr(_labels[index], Coord(x+s,y)); Vtest.Set(Itest[0], Itest[1], Itest[2]); Vavoid.Set(Iavoid[0], Iavoid[1], Iavoid[2]); dist = sqrt(Vtest.distanceTo2(Vavoid)); //printf("%f\n",dist); if (dist > 25) score += 25.; else score += dist; } } printf("Image %d, score %f\n",i, score); ordered.push(Ranker(i,score)); } }
float DifferenceCut::BVZ_data_penalty(Coord p, ushort d) { const unsigned char* col = _imptr(d,p); Vec3i c(col[0], col[1], col[2]); float dist = sqrt(_avoidColor.distanceTo2(c)); if (dist < 25.) return _bright * ( 25. - dist); else return 0; }
void PortraitCut::saveComp(char* name) { RGBImage im = (RGBImage) imNew(IMAGE_RGB, _w, _h); int i, j,index=0; for (j=0; j<_h; j++) for (i=0; i<_w; i++, ++index) { Coord p(i,j); unsigned char* cref = _imptr(_labels[index], p); IMREF(im,p).r = cref[0]; IMREF(im,p).g = cref[1]; IMREF(im,p).b = cref[2]; } int res = imSave(im,name); assert(res==0); imFree(im); }
float IntrinsicCut::BVZ_data_penalty(Coord p, ushort d) { //return 0; int index = p.y*_w + p.x; //return 0.005f * (float) (_medians[index].normFrom(_cgrads[d][index])); const unsigned char* col = _imptr(d,p); Vec3i c(col[0], col[1], col[2]); float cost = 0.003f * (float) sqrt(_medianColors[index].distanceTo2(c)); //fprintf(stdout, "cost is %lf\n", cost); return cost; /* //return (.001f * float(sqrt(_medianColors[index].distanceTo2(c))) + // .005f * float(_medians[index].normFrom(_cgrads[d][index]))); */ }
void IntrinsicCut::computeMedians() { int med = _n/2; _medians = new CGrad[_w*_h]; double* workspace = new double[_n]; _medianColors = new Vec3i[_w*_h]; int* cworkspace = new int[_n]; int c,i,j,l, index; for (c=0; c<6; ++c) { for (j=0,index=0; j<_h; ++j) for (i=0; i<_w; ++i, ++index) { for (l=0; l<_n; ++l) { workspace[l] = _cgrads[l][index].val(c); } // end iterate over images nth_element(workspace, workspace+med, workspace+_n); _medians[index].setVal(c,workspace[med]); } // end iterate over pixels } // iterate over channels, directions for (c=0; c<3; ++c) { for (j=0,index=0; j<_h; ++j) for (i=0; i<_w; ++i, ++index) { for (l=0; l<_n; ++l) { Coord p(i,j); const unsigned char* col = _imptr(l, p); cworkspace[l] = col[c]; } nth_element(cworkspace, cworkspace+med, cworkspace+_n); _medianColors[index].data()[c] = cworkspace[med]; } } printf("Finished computing medians\n"); delete[] workspace; delete[] cworkspace; }
unsigned char* PortraitCut::comp() { //RGBImage im = (RGBImage) imNew(IMAGE_RGB, _w, _h); unsigned char* im = new unsigned char[3*_w*_h]; int i, j,index=0, cindex=0; for (j=0; j<_h; j++) for (i=0; i<_w; i++, ++index, cindex+=3) { Coord p(i,j); unsigned char* cref = _imptr(_labels[index], p); //IMREF(im,p).r = cref[0]; //IMREF(im,p).g = cref[1]; //IMREF(im,p).b = cref[2]; im[cindex] = cref[0]; im[cindex+1] = cref[1]; im[cindex+2] = cref[2]; } return im; //(unsigned char* ) im->data; }
float PortraitCut::BVZ_interaction_penalty(Coord p, Coord np, ushort l, ushort nl) { int c, k; float a,M=0; assert(l<_n); assert(l<_n); if (l==nl) return 0; unsigned char *Il, *Inl; if (_cuttype == C_NORMAL || _cuttype == C_GRAD) { // difference at p pixel a=0; Il = _imptr(l,p); Inl = _imptr(nl,p); for (c=0; c<3; ++c) { k = Il[c] - Inl[c]; a += k*k; } M = sqrt(a); // difference at np pixel a=0; Il = _imptr(l,np); Inl = _imptr(nl,np); for (c=0; c<3; ++c) { k = Il[c] - Inl[c]; a += k*k; } M += sqrt(a); M /=6.f; // gradient denominator if (_cuttype == C_GRAD) { float G; if (p.x!=np.x) { // vertical cut, vertical Sobel filter Coord minp(min(p.x,np.x), p.y); if (p.y>0 && p.y<_h-1) { G = .5f*(_idata->vertGradMagLookup(l,minp) + _idata->vertGradMagLookup(nl,minp)); //G = MIN(vertGradMagLookup(l,minp), vertGradMagLookup(nl,minp)); } else G = 1.f; } else { // horizontal cut, horizontal Sobel filter Coord minp(p.x, min(p.y,np.y)); if (p.x>0 && p.x<_w-1) { G = .5f*(_idata->horizGradMagLookup(l,minp) + _idata->horizGradMagLookup(nl,minp)); //G = MIN(horizGradMagLookup(l,minp), horizGradMagLookup(nl,minp)); } else G = 1.f; } if (G==0) M = A_INFINITY; else M /= G; } } else if (_cuttype == C_GRAD2) { // difference at p pixel const CGrad& acgrad = _idata->cgradLookup(l,p); const CGrad& bcgrad = _idata->cgradLookup(nl,p); M = acgrad.normFrom(bcgrad); // difference at np pixel const CGrad& acgrad2 = _idata->cgradLookup(l,np); const CGrad& bcgrad2 = _idata->cgradLookup(nl,np); M += acgrad2.normFrom(bcgrad2); } else if (_cuttype == C_BOTH) { // difference at p pixel const CGrad& acgrad = _idata->cgradLookup(l,p); const CGrad& bcgrad = _idata->cgradLookup(nl,p); M = acgrad.normFrom(bcgrad); // difference at np pixel const CGrad& acgrad2 = _idata->cgradLookup(l,np); const CGrad& bcgrad2 = _idata->cgradLookup(nl,np); M += acgrad2.normFrom(bcgrad2); // difference at p pixel a=0; Il = _imptr(l,p); Inl = _imptr(nl,p); for (c=0; c<3; ++c) { k = Il[c] - Inl[c]; a += k*k; } M += sqrt(a); // difference at np pixel a=0; Il = _imptr(l,np); Inl = _imptr(nl,np); for (c=0; c<3; ++c) { k = Il[c] - Inl[c]; a += k*k; } M += sqrt(a); M *= .1; } if (M>A_INFINITY) M = A_INFINITY; //printf("%d ",d); //assert(_finite(M) && !_isnan(M)); return M; }
float BrightCut::BVZ_data_penalty(Coord p, ushort d) { unsigned char* I = _imptr(d,p); float lum = .3086f * (float)I[0] + .6094f * (float)I[1] + .082f * (float)I[2]; return .01f * (255.f -lum); }