Skip to content

Commit

Permalink
Add more LK notes
Browse files Browse the repository at this point in the history
  • Loading branch information
brimson committed Jul 22, 2022
1 parent c13a9fe commit b76fd1a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
29 changes: 16 additions & 13 deletions shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ namespace MotionBlur
float2 Lucas_Kanade(int Level, float2 Vectors, float4 TexCoord)
{
/*
Fetch 2x2 bilinear-filtered windows for spatial and temporal dertivatives
Fetch 2x2 bilinear-filtered windows for spatial(S) and temporal(T) dertivatives
[TexCoord.xw TexCoord.zw]
[TexCoord.xy TexCoord.zy]
*/
Expand Down Expand Up @@ -375,36 +375,39 @@ namespace MotionBlur
B2 = IyIt
*/

// Solve window equation at matrix A
// Create matrix A and solve its window sum
// A.x = A11; A.y = A22; A.z = A12/A22
float3 A = 0.0;
A += (S[0].xyx * S[0].xyy);
A += (S[1].xyx * S[1].xyy);
A += (S[2].xyx * S[2].xyy);
A += (S[3].xyx * S[3].xyy);

// Solve window equation at vector B
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Ensure Lucas-Kanade's determinant is non-zero
A.xy = max(A.xy, FP16_SMALLEST_SUBNORMAL);

// Create -IxIy (A12) for A's determinant and LK matrix
// Create -IxIy (A12) for A^-1 and its determinant
A.z = A.z * (-1.0);

// Calculate determinant
// Calculate A^-1 determinant
float D = ((A.x * A.y) - (A.z * A.z));

// Calculate A^-1
A = (1.0 / D) * A;

// Create vector B and solve its window sum
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Calculate Lucas-Kanade matrix
float2 LK = 0.0;
LK.x = dot(A.yz, -B.xy);
LK.y = dot(A.zx, -B.xy);
LK = (D != 0.0) ? (LK / D) + Vectors : 0.0;
LK = (D != 0.0) ? LK + Vectors : 0.0;
return LK;
}

Expand Down
27 changes: 15 additions & 12 deletions shaders/cOpticalFlowLK.fx
Original file line number Diff line number Diff line change
Expand Up @@ -368,36 +368,39 @@ namespace OpticalFlowLK
B2 = IyIt
*/

// Solve window equation at matrix A
// Create matrix A and solve its window sum
// A.x = A11; A.y = A22; A.z = A12/A22
float3 A = 0.0;
A += (S[0].xyx * S[0].xyy);
A += (S[1].xyx * S[1].xyy);
A += (S[2].xyx * S[2].xyy);
A += (S[3].xyx * S[3].xyy);

// Solve window equation at vector B
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Ensure Lucas-Kanade's determinant is non-zero
A.xy = max(A.xy, FP16_SMALLEST_SUBNORMAL);

// Create -IxIy (A12) for A's determinant and LK matrix
// Create -IxIy (A12) for A^-1 and its determinant
A.z = A.z * (-1.0);

// Calculate determinant
// Calculate A^-1 determinant
float D = ((A.x * A.y) - (A.z * A.z));

// Calculate A^-1
A = (1.0 / D) * A;

// Create vector B and solve its window sum
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Calculate Lucas-Kanade matrix
float2 LK = 0.0;
LK.x = dot(A.yz, -B.xy);
LK.y = dot(A.zx, -B.xy);
LK = (D != 0.0) ? (LK / D) + Vectors : 0.0;
LK = (D != 0.0) ? LK + Vectors : 0.0;
return LK;
}

Expand Down
27 changes: 15 additions & 12 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
Expand Up @@ -440,36 +440,39 @@ namespace Datamosh
B2 = IyIt
*/

// Solve window equation at matrix A
// Create matrix A and solve its window sum
// A.x = A11; A.y = A22; A.z = A12/A22
float3 A = 0.0;
A += (S[0].xyx * S[0].xyy);
A += (S[1].xyx * S[1].xyy);
A += (S[2].xyx * S[2].xyy);
A += (S[3].xyx * S[3].xyy);

// Solve window equation at vector B
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Ensure Lucas-Kanade's determinant is non-zero
A.xy = max(A.xy, FP16_SMALLEST_SUBNORMAL);

// Create -IxIy (A12) for A's determinant and LK matrix
// Create -IxIy (A12) for A^-1 and its determinant
A.z = A.z * (-1.0);

// Calculate determinant
// Calculate A^-1 determinant
float D = ((A.x * A.y) - (A.z * A.z));

// Calculate A^-1
A = (1.0 / D) * A;

// Create vector B and solve its window sum
// B.x = B1; B.y = B2
float2 B = 0.0;
B += (S[0] * T[0]);
B += (S[1] * T[1]);
B += (S[2] * T[2]);
B += (S[3] * T[3]);

// Calculate Lucas-Kanade matrix
float2 LK = 0.0;
LK.x = dot(A.yz, -B.xy);
LK.y = dot(A.zx, -B.xy);
LK = (D != 0.0) ? (LK / D) + Vectors : 0.0;
LK = (D != 0.0) ? LK + Vectors : 0.0;
return LK;
}

Expand Down

0 comments on commit b76fd1a

Please sign in to comment.