-
-
Save dflemstr/391021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Linear interpolation of a 2D double array | |
private double[,] Interpolate(double[,] matrix, int dim) | |
{ | |
double[,] result = new double[dim, dim]; | |
double sf = (double)(matrix.GetLength(0) - 1) / (double)(dim - 1d); | |
for (int x = 0; x < dim; x++) | |
{ | |
for (int y = 0; y < dim; y++) | |
{ | |
double oldX = sf * x; | |
double oldY = sf * y; | |
double q12 = matrix[(int)Math.Floor(oldX), (int)Math.Floor(oldY)]; //Oben links | |
double q22 = matrix[(int)Math.Ceiling(oldX), (int)Math.Floor(oldY)]; //Oben rechts | |
double q11 = matrix[(int)Math.Floor(oldX), (int)Math.Ceiling(oldY)]; //Unten links | |
double q21 = matrix[(int)Math.Ceiling(oldX), (int)Math.Ceiling(oldY)];//Unten rechts | |
double x1 = Math.Floor(oldX); //Links | |
double x2 = Math.Ceiling(oldX); //Rechts | |
double y2 = Math.Floor(oldY); //Oben | |
double y1 = Math.Ceiling(oldY); //Unten | |
double x0 = (x2 - oldX); | |
double y0 = (y1 - oldY); | |
double newValue = q11 * (1d - x0) * (1d - y0) + | |
q21 * x0 * (1d - y0) + | |
q12 * y0 * (1d - x0) + | |
q22 * x0 * y0; | |
result[x, y] = newValue; | |
} | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.purview.core.transforms | |
import org.purview.core.data.Matrix | |
import org.purview.core.data.MutableArrayMatrix | |
import scala.math._ | |
case class Interpolate(dimX: Int, dimY: Int) extends Function1[Matrix[Float], Matrix[Float]] { | |
def apply(matrix: Matrix[Float]): Matrix[Float] = { | |
val result = new MutableArrayMatrix[Float](dimX, dimY) | |
val sx = matrix.width / dimX.toFloat | |
val sy = matrix.height / dimY.toFloat | |
var x = 0 | |
while(x < dimX) { | |
var y = 0 | |
while(y < dimY) { | |
val oldX = sx * x; | |
val oldY = sy * y; | |
val x1 = oldX.toInt | |
val x2 = x1 + 1 | |
val y1 = oldY.toInt | |
val y2 = y1 + 1 | |
val q11 = matrix(x1, y1) | |
val q21 = matrix(x2, y1) | |
val q12 = matrix(x1, y2) | |
val q22 = matrix(x2, y2) | |
val dx = oldX - x1 | |
val dy = oldY - y1 | |
result(x, y) = q11 * dx * dy + | |
q21 * (1 - dx) * dy + | |
q12 * dx * (1 - dy) + | |
q22 * (1 - dx) * (1 - dy) | |
y += 1 | |
} | |
x += 1 | |
} | |
result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment