9 #include "../lib/args.h" 10 #include "../lib/rotation.h" 11 #include "../lib/misc.h" 12 #include "../lib/json.h" 13 #include "../lib/intrinsics.h" 14 #include "../lib/assert.h" 15 #include "../lib/opencv.h" 16 #include "../lib/eigen.h" 33 std::vector<Eigen_vec3> v_points;
34 v_points.reserve(feature.
points.size());
36 for(
const auto& kv : feature.
points) {
38 if(fpoint.
depth == 0.0)
continue;
42 v_points.push_back(v);
45 std::size_t n = v_points.size();
48 v_mean = v_mean /
real(n);
52 for(std::ptrdiff_t i = 0; i < n; ++i) {
54 A(0, i) = v[0] - v_mean[0];
55 A(1, i) = v[1] - v_mean[1];
56 A(2, i) = v[2] - v_mean[2];
59 Eigen::JacobiSVD<decltype(A)> svd(A, Eigen::ComputeThinU);
60 auto U = svd.matrixU();
62 if(normal[2] < 0.0) normal = -normal;
64 return from_eigen<3>(normal);
69 vec3 z(0.0, 0.0, 1.0);
70 vec3 v = normal.cross(z);
76 real c = normal.dot(z);
77 real f = 1.0 / (1.0 + c);
78 mat33 R = mat33::eye() + v_mat + f*v_mat*v_mat;
85 std::vector<cv::Vec2f> horizontal_v_points, vertical_v_points;
86 for(
const auto& kv : feature.
points) {
89 if(fpoint.
depth == 0.0)
continue;
92 vec3 v = R_xy * K_inv * i_h;
94 if(idx.
y == feature.
reference_view.
y) horizontal_v_points.emplace_back(v[0], v[1]);
95 if(idx.
x == feature.
reference_view.
x) vertical_v_points.emplace_back(v[0], v[1]);
98 real horizontal_tan = NAN, vertical_tan = NAN;
102 cv::Vec4f line_parameters;
103 cv::fitLine(horizontal_v_points, line_parameters, CV_DIST_L2, 0.0, 0.01, 0.01);
104 horizontal_tan = line_parameters[1] / line_parameters[0];
108 cv::Vec4f line_parameters;
109 cv::fitLine(vertical_v_points, line_parameters, CV_DIST_L2, 0.0, 0.01, 0.01);
110 vertical_tan = -line_parameters[0] / line_parameters[1];
113 if(std::isfinite(horizontal_tan) && std::isfinite(vertical_tan)) {
114 return (horizontal_tan + vertical_tan) / 2.0;
115 }
else if(std::isfinite(horizontal_tan))
116 return horizontal_tan;
117 else if(std::isfinite(vertical_tan))
124 int main(
int argc,
const char* argv[]) {
125 get_args(argc, argv,
"cors.json intrinsics.json out_rotation.json");
130 vec3 xy_normal_sum = 0.0;
131 for(
const auto& kv : cors.
features) {
138 vec3 xy_normal = xy_normal_sum / cv::norm(xy_normal_sum);
142 real angle_mean = 0.0;
144 for(
const auto& kv : cors.
features) {
147 if(std::isnan(tan))
continue;
148 angle_mean += std::atan(tan);
151 real angle = angle_mean / angle_count;
154 std::cos(angle), -std::sin(angle), 0.0,
155 std::sin(angle), std::cos(angle), 0.0,
159 mat33 R = R_xy.t() * R_z;
162 std::cout << R << std::endl;
164 std::cout <<
"x = " << euler[0] *
deg_per_rad <<
"\n";
165 std::cout <<
"y = " << euler[1] * deg_per_rad <<
"\n";
166 std::cout <<
"z = " << euler[2] * deg_per_rad << std::endl;
168 std::cout <<
"saving rotation matrix" << std::endl;
real additional_z_rotation(const mat33 &K_inv, const mat33 &R_xy, const image_correspondence_feature &feature)
constexpr real deg_per_rad
int main(int argc, const char *argv[])
constexpr int depth_min_views
Set of features, each on set of views.
constexpr int vslope_min_views
Eigen_vec< 3 > Eigen_vec3
Eigen_mat< Rows, Cols > to_eigen_mat(const cv::Matx< real, Rows, Cols > &cv_mat)
Eigen::Matrix< real, Rows, Eigen::Dynamic > Eigen_matnX
std::map< std::string, image_correspondence_feature > features
intrinsics intrinsics_arg()
mat33 xy_rotation_matrix(const xy_rotation &normal)
cv::Matx< real, 3, 3 > mat33
json encode_mat(const Mat &mat)
void export_json_file(const json &j, const std::string &filename, bool compact)
view_index reference_view
image_correspondences image_correspondences_arg()
constexpr int hslope_min_views
std::string out_filename_arg()
vec3 to_euler(const mat33 &R_)
Feature on set of views. Optionally one view is "reference".
std::map< view_index, feature_point > points
xy_rotation estimate_xy_rotation(const mat33 &K_inv, const image_correspondence_feature &feature)
const vec3 null_vec3(0.0, 0.0, 0.0)
void get_args(int argc, const char *argv[], const std::string &usage)