Some ransac fixes and cleanups Change-Id: I119a6833cf6f71f02d5a40093f7d3bea97b7a7b8
diff --git a/av1/common/warped_motion.c b/av1/common/warped_motion.c index f2cbad1..f726fc6 100644 --- a/av1/common/warped_motion.c +++ b/av1/common/warped_motion.c
@@ -917,7 +917,6 @@ } static void normalize_homography(double *pts, int n, double *T) { - // Assume the points are 2d coordinates with scale = 1 double *p = pts; double mean[2] = { 0, 0 }; double msqe = 0; @@ -974,7 +973,23 @@ multiply_mat(iT2, params2, params, 3, 3, 3); } -static void denormalize_affine(double *params, double *T1, double *T2) { +static void denormalize_homography_reorder(double *params, double *T1, + double *T2) { + double params_denorm[MAX_PARAMDIM]; + memcpy(params_denorm, params, sizeof(*params) * 8); + params_denorm[8] = 1.0; + denormalize_homography(params_denorm, T1, T2); + params[0] = params_denorm[2]; + params[1] = params_denorm[5]; + params[2] = params_denorm[0]; + params[3] = params_denorm[1]; + params[4] = params_denorm[3]; + params[5] = params_denorm[4]; + params[6] = params_denorm[6]; + params[7] = params_denorm[7]; +} + +static void denormalize_affine_reorder(double *params, double *T1, double *T2) { double params_denorm[MAX_PARAMDIM]; params_denorm[0] = params[0]; params_denorm[1] = params[1]; @@ -994,7 +1009,8 @@ params[6] = params[7] = 0; } -static void denormalize_rotzoom(double *params, double *T1, double *T2) { +static void denormalize_rotzoom_reorder(double *params, double *T1, + double *T2) { double params_denorm[MAX_PARAMDIM]; params_denorm[0] = params[0]; params_denorm[1] = params[1]; @@ -1014,7 +1030,8 @@ params[6] = params[7] = 0; } -static void denormalize_translation(double *params, double *T1, double *T2) { +static void denormalize_translation_reorder(double *params, double *T1, + double *T2) { double params_denorm[MAX_PARAMDIM]; params_denorm[0] = 1; params_denorm[1] = 0; @@ -1054,7 +1071,7 @@ } mat[0] = sumx / np; mat[1] = sumy / np; - denormalize_translation(mat, T1, T2); + denormalize_translation_reorder(mat, T1, T2); return 0; } @@ -1093,7 +1110,7 @@ return 1; } multiply_mat(temp, b, mat, 4, np2, 1); - denormalize_rotzoom(mat, T1, T2); + denormalize_rotzoom_reorder(mat, T1, T2); aom_free(a); return 0; } @@ -1137,7 +1154,7 @@ return 1; } multiply_mat(temp, b, mat, 6, np2, 1); - denormalize_affine(mat, T1, T2); + denormalize_affine_reorder(mat, T1, T2); aom_free(a); return 0; } @@ -1147,7 +1164,7 @@ const int np3 = np * 3; double *a = (double *)aom_malloc(sizeof(*a) * np3 * 18); double *U = a + np3 * 9; - double S[9], V[9 * 9]; + double S[9], V[9 * 9], H[9]; int i, mini; double sx, sy, dx, dy; double T1[9], T2[9]; @@ -1202,11 +1219,15 @@ } } - for (i = 0; i < 9; i++) mat[i] = V[i * 9 + mini]; - denormalize_homography(mat, T1, T2); + for (i = 0; i < 9; i++) H[i] = V[i * 9 + mini]; + denormalize_homography_reorder(H, T1, T2); aom_free(a); - if (mat[8] == 0.0) { + if (H[8] == 0.0) { return 1; + } else { + // normalize + double f = 1.0 / H[8]; + for (i = 0; i < 8; i++) mat[i] = f * H[i]; } return 0; }
diff --git a/av1/encoder/ransac.c b/av1/encoder/ransac.c index d01ca7f..33b45ca 100644 --- a/av1/encoder/ransac.c +++ b/av1/encoder/ransac.c
@@ -87,8 +87,8 @@ Z_inv = mat[6] * x + mat[7] * y + 1; assert(fabs(Z_inv) > 0.000001); Z = 1. / Z_inv; - *(proj++) = (mat[0] * x + mat[1] * y + mat[2]) * Z; - *(proj++) = (mat[3] * x + mat[4] * y + mat[5]) * Z; + *(proj++) = (mat[2] * x + mat[3] * y + mat[0]) * Z; + *(proj++) = (mat[4] * x + mat[5] * y + mat[1]) * Z; points += stride_points - 2; proj += stride_proj - 2; } @@ -119,18 +119,13 @@ static int ransac(double *matched_points, int npoints, int *number_of_inliers, int *best_inlier_mask, double *best_params, const int minpts, const int paramdim, IsDegenerateFunc is_degenerate, - NormalizeFunc normalize, DenormalizeFunc denormalize, FindTransformationFunc find_transformation, ProjectPointsDoubleFunc projectpoints) { - static const double INLIER_THRESHOLD_NORMALIZED = 0.1; - static const double INLIER_THRESHOLD_UNNORMALIZED = 1.0; + static const double inlier_threshold = 1.0; static const double PROBABILITY_REQUIRED = 0.9; static const double EPS = 1e-12; static const int MIN_TRIALS = 20; - const double inlier_threshold = - (normalize && denormalize ? INLIER_THRESHOLD_NORMALIZED - : INLIER_THRESHOLD_UNNORMALIZED); int N = 10000, trial_count = 0; int i; int ret_val = 0; @@ -154,7 +149,6 @@ int *inlier_mask; double *cnp1, *cnp2; - double T1[9], T2[9]; *number_of_inliers = 0; if (npoints < minpts * MINPTS_MULTIPLIER || npoints == 0) { @@ -188,11 +182,6 @@ } matched_points -= 4 * npoints; - if (normalize && denormalize) { - normalize(corners1, npoints, T1); - normalize(corners2, npoints, T2); - } - while (N > trial_count) { int num_inliers = 0; double sum_distance = 0.0; @@ -284,9 +273,6 @@ } find_transformation(max_inliers, best_inlier_set1, best_inlier_set2, best_params); - if (normalize && denormalize) { - denormalize(best_params, T1, T2); - } *number_of_inliers = max_inliers; finish_ransac: aom_free(best_inlier_set1); @@ -324,54 +310,28 @@ int *number_of_inliers, int *best_inlier_mask, double *best_params) { return ransac(matched_points, npoints, number_of_inliers, best_inlier_mask, - best_params, 3, 2, is_degenerate_translation, - NULL, // normalize_homography, - NULL, // denormalize_rotzoom, - find_translation, project_points_double_translation); + best_params, 3, 2, is_degenerate_translation, find_translation, + project_points_double_translation); } int ransac_rotzoom(double *matched_points, int npoints, int *number_of_inliers, int *best_inlier_mask, double *best_params) { return ransac(matched_points, npoints, number_of_inliers, best_inlier_mask, - best_params, 3, 4, is_degenerate_affine, - NULL, // normalize_homography, - NULL, // denormalize_rotzoom, - find_rotzoom, project_points_double_rotzoom); + best_params, 3, 4, is_degenerate_affine, find_rotzoom, + project_points_double_rotzoom); } int ransac_affine(double *matched_points, int npoints, int *number_of_inliers, int *best_inlier_mask, double *best_params) { return ransac(matched_points, npoints, number_of_inliers, best_inlier_mask, - best_params, 3, 6, is_degenerate_affine, - NULL, // normalize_homography, - NULL, // denormalize_affine, - find_affine, project_points_double_affine); + best_params, 3, 6, is_degenerate_affine, find_affine, + project_points_double_affine); } int ransac_homography(double *matched_points, int npoints, int *number_of_inliers, int *best_inlier_mask, double *best_params) { - const int result = - ransac(matched_points, npoints, number_of_inliers, best_inlier_mask, - best_params, 4, 8, is_degenerate_homography, - NULL, // normalize_homography, - NULL, // denormalize_homography, - find_homography, project_points_double_homography); - if (!result) { - // normalize so that H33 = 1 - int i; - double params[8]; - const double m = 1.0 / best_params[8]; - assert(fabs(best_params[8]) > 0.00001); - for (i = 0; i < 8; ++i) params[i] = best_params[i] * m; - best_params[0] = params[2]; - best_params[1] = params[5]; - best_params[2] = params[0]; - best_params[3] = params[1]; - best_params[4] = params[3]; - best_params[5] = params[4]; - best_params[6] = params[6]; - best_params[7] = params[7]; - } - return result; + return ransac(matched_points, npoints, number_of_inliers, best_inlier_mask, + best_params, 4, 8, is_degenerate_homography, find_homography, + project_points_double_homography); }