// initialize field from an existing (possibily smaller) NNF public void initialize(NNF nnf) { // field this.field = new int[input.width,input.height,3]; this.input = nnf.input; this.output = nnf.output; this.S = nnf.S; int fx = input.width/nnf.input.width; int fy = input.height/nnf.input.height; //System.out.println("nnf upscale by "+fx+"x"+fy+" : "+nnf.input.W+","+nnf.input.H+" -> "+input.W+","+input.H); for(int y=0;y<input.height;y++) { for(int x=0;x<input.width;x++) { int xlow = Mathf.Min(x/fx, nnf.input.width-1); int ylow = Mathf.Min(y/fy, nnf.input.height-1); field[x,y,0] = nnf.field[xlow,ylow,0]*fx; field[x,y,1] = nnf.field[xlow,ylow,1]*fy; field[x,y,2] = MaskedImage.DSCALE; } } initialize(); }
//----------------------------------------------------- // Expectation Step : vote for best estimations of each pixel public void ExpectationStep(NNF nnf, bool sourceToTarget, double[,,] vote, MaskedImage source, bool upscale) { int[,,] field = nnf.getField(); int R = nnf.S; for(int y=0;y<nnf.input.height;y++) { for(int x=0;x<nnf.input.width;x++) { // x,y = center pixel of patch in input // xp,yp = center pixel of best corresponding patch in output int xp=field[x,y,0], yp=field[x,y,1], dp=field[x,y,2]; // similarity measure between the two patches double w=0; if(dp<MaskedImage.DSCALE+1) w = MaskedImage.similarity[dp]; // vote for each pixel inside the input patch for(int dy=-R;dy<=R;dy++) { for(int dx=-R;dx<=R;dx++) { // get corresponding pixel in output patch int xs,ys,xt,yt; if (sourceToTarget) { xs=x+dx; ys=y+dy; xt=xp+dx; yt=yp+dy; } else { xs=xp+dx; ys=yp+dy; xt=x+dx; yt=y+dy; } if (xs<0 || xs>=nnf.input.width) continue; if (ys<0 || ys>=nnf.input.height) continue; if (xt<0 || xt>=nnf.output.width) continue; if (yt<0 || yt>=nnf.output.height) continue; // add vote for the value if (upscale) { weightedCopy(source, 2*xs, 2*ys, vote, 2*xt, 2*yt, w); weightedCopy(source, 2*xs+1, 2*ys, vote, 2*xt+1, 2*yt, w); weightedCopy(source, 2*xs, 2*ys+1, vote, 2*xt, 2*yt+1, w); weightedCopy(source, 2*xs+1, 2*ys+1, vote, 2*xt+1, 2*yt+1, w); } else weightedCopy(source, xs, ys, vote, xt, yt, w); } } // vote } } }