In short I'm doing the same thing that an optical mouse does.
I'm taking two 2D-arrays of gray-scale and right now am comparing equal values to see what the difference is.
Example:
Array1:
1 1 0 0
0 1 0 0
0 0 0 0
0 0 0 0
Array2:
0 0 0 0
0 1 1 0
0 0 1 0
0 0 0 0
Here is the code I have right now to test it. I'm only checking for 1's right now as if it were the actual image. Changing that isn't hard.
int[][] t1 = new int[][]{
{1,1,0,0},
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}
};
int[][] t2 = new int[][]{
{0,0,0,0},
{0,1,1,0},
{0,0,1,0},
{0,0,0,0}
};
double mag = 0.0;
double angle = 0.0;
int num = 0;
for (int i = 0; i < t2.length; i++){
for (int j = 0; j < t2[i].length; j++){
if(t2[i][j] == 0) continue;
//scan through and calculate average magnitude/angle
if(t2[i][j] == 1){
for (int k = 0; k < t1.length; k++){
for (int l = 0; l < t1[k].length; l++){
if(t1[k][l] == 1){
mag += calculateMagnitude(l, k, j, i);
angle -= calculateAngle(l, k, j, i);
num++;
}
}
}
}
}
}
double fMag = mag/num;
double fAngle = angle/num;
System.out.println(fMag);
System.out.println(fAngle);
public static double calculateAngle(int x1, int y1, int x2, int y2){
if(y2 == y1){
if(x2 > x1) return 90.0;
else if(x2 < x1) return -90.0;
else return 0.0;
} else if(x2 == x1){
if(y2 > y1) return 0.0;
else if(y2 < y1) return -180.0;
}
return Math.toDegrees(Math.atan( ((double)(y2-y1))/(x2-x1) ));
}
public static double calculateMagnitude(int x1, int y1, int x2, int y2){
double d1 = Math.pow((x2 - x1),2);
double d2 = Math.pow((y2 - y1), 2);
return Math.sqrt(d1 + d2);
}
However this is quite taxing as it's O(n^4) and I'm sure there are more efficient ways to do this. I've done quite a bit of research but as of now have not been able to figure out how to do it. Also right now the exact answer should be 1.414 and -45 which means I'm off by roughly 6%. This is okay, but I'd like to be more exact.
If anyone knows a way or can figure out a way to do this more efficiently and/or precisely please post. Not to sound like an ass, but linking me to a PhD research paper and saying it should work isn't what I'm looking for. I've done a fair amount of research and those papers mostly refer if the image is still displayed completely on the screen.
I'm looking for a way to calculate the image displacement even if a portion of the image goes of screen.