All Downloads are FREE. Search and download functionalities are using the official Maven repository.

CopyMove.NNF_NoMask Maven / Gradle / Ivy

The newest version!
package CopyMove;

import java.util.Random;

/**
 * Nearest-Neighbor Field (see PatchMatch algorithm) 
 * 
 * @author Xavier Philippeau
 *
 */
public class NNF_NoMask {

	// image
	ImageMap input, output;

	//  patch size
	int S;

	// Nearest-Neighbor Field 1 pixel = { x_target, y_target, distance_scaled }
	int[][][] field;

	// random generator
	Random random = new Random(0);

	// constructor
	public NNF_NoMask(ImageMap input, ImageMap output, int patchsize) {
		this.input = input;
		this.output= output;
		this.S = patchsize;	
	}
	
	// initialize field with random values
	public void randomize() {
		// field
		this.field = new int[input.W][input.H][3];
		
		for(int y=0;y "+input.W+","+input.H);
		for(int y=0;y0) minimizeLink(x,y,+1);

			// reverse scanline order
			for(int y=max_y;y>=min_y;y--)
				for(int x=max_x;x>=min_x;x--)
					if (field[x][y][2]>0) minimizeLink(x,y,-1);
		}
	}


	// multi-pass NN-field minimization for matches above a distance threshold (modification by Markos Zampoglou)
	public void minimizeDistance(int pass, int minDist) {

		double ImageDiagonal=Math.sqrt(input.W*input.W+input.H*input.H);
		int MinPixelDistance=(int) Math.ceil(ImageDiagonal/minDist);

		int min_x=0, min_y=0, max_x=input.W-1, max_y=input.H-1;

		// multi-pass minimization
		for(int i=0;i0) minimizeLinkDistance(x, y, +1, MinPixelDistance);

			// reverse scanline order
			for(int y=max_y;y>=min_y;y--)
				for(int x=max_x;x>=min_x;x--)
					if (field[x][y][2]>0) minimizeLinkDistance(x, y, -1, MinPixelDistance);
		}
	}


	// minimize a single link (see "PatchMatch" - page 4)
	public void minimizeLink(int x, int y, int dir) {
		int xp,yp,dp;
		
		//Propagation Left/Right
		if (x-dir>0 && x-dir0 && y-dir0) {
			xp = xpi + random.nextInt(2*wi)-wi;
			yp = ypi + random.nextInt(2*wi)-wi;
			xp = Math.max(0, Math.min(output.W-1, xp ));
			yp = Math.max(0, Math.min(output.H-1, yp ));
			
			dp = distance(x,y, xp,yp);
			if (dp0 && x-dirminDist*minDist) & (dp < field[x][y][2])) {
					field[x][y][0] = xp;
					field[x][y][1] = yp;
					field[x][y][2] = dp;
				}
			}


		//Propagation Up/Down
		if (y-dir>0 && y-dirminDist*minDist) & (dp0) {
			xp = xpi + random.nextInt(2*wi)-wi;
			yp = ypi + random.nextInt(2*wi)-wi;
			xp = Math.max(0, Math.min(output.W-1, xp ));
			yp = Math.max(0, Math.min(output.H-1, yp ));

			dp = distance(x,y, xp,yp);
			if (((xp-x)*(xp-x)+(yp-y)*(yp-y)>minDist*minDist) & (dp




© 2015 - 2024 Weber Informatics LLC | Privacy Policy