6 #include "../lib/args.h" 7 #include "../lib/misc.h" 8 #include "../lib/json.h" 9 #include "../lib/opencv.h" 10 #include "../lib/camera.h" 11 #include "../lib/image_io.h" 12 #include "../lib/random_color.h" 13 #include "../lib/intrinsics.h" 14 #include "../lib/filesystem.h" 15 #include "../lib/rotation.h" 22 int main(
int argc,
const char* argv[]) {
23 get_args(argc, argv,
"R.json intr.json out_datas_dir/ [features_count=100] [num_x=30] [num_y=30] [step_x=3.0] [step_y=3.0]");
34 real fx = K(0, 0), fy = K(1, 1), cx = K(0, 2), cy = K(1, 2);
38 int width = intr.
width;
41 std::cout <<
"generating " << features_count <<
" random 3D features" << std::endl;
42 std::vector<vec3> features;
44 std::uniform_real_distribution<real> ix_dist(0, width);
45 std::uniform_real_distribution<real> iy_dist(0, height);
46 std::uniform_real_distribution<real> vz_dist(500.0, 3000.0);
48 for(
int i = 0; i < features_count; ++i) {
49 real ix = ix_dist(gen);
50 real iy = iy_dist(gen);
51 real vz = vz_dist(gen);
53 real vx = (ix - cx)*vz/fx;
54 real vy = (iy - cy)*vz/fy;
59 features.push_back(w);
64 std::cout <<
"creating directories" << std::endl;
70 std::cout <<
"generating dataset parameters" << std::endl;
72 json j_dataset = json::object();
73 j_dataset[
"x_index_range"] = json::array({0, num_x-1});
74 j_dataset[
"y_index_range"] = json::array({0, num_y-1});
75 j_dataset[
"width"] = width;
76 j_dataset[
"height"] = height;
77 j_dataset[
"image_filename_format"] =
"image/y{y}_x{x}.png";
78 j_dataset[
"depth_filename_format"] =
"depth/y{y}_x{x}.png";
79 j_dataset[
"cameras_filename"] =
"cameras.json";
80 j_dataset[
"camera_name_format"] =
"camera_y{y}_x{x}";
85 std::cout <<
"generating correspondences by projecting features for each view" << std::endl;
88 std::map<view_index, vec3> view_camera_centers;
89 std::map<view_index, mat33> view_camera_rotations;
91 std::normal_distribution<real> pos_noise_dist(0.0, step_x/15.0), pos_outlier_noise_dist(0.0, step_x/6.0);
92 std::bernoulli_distribution pos_outlier_dist(0.01);
93 std::normal_distribution<real> rot_noise_dist(0.0, 0.007_deg), rot_roll_noise_dist(0.0, 0.004_deg);
95 for(
int y = 0; y < num_y; ++y)
for(
int x = 0; x < num_x; ++x) {
99 real px = step_x*(x - num_x/2);
100 real py = step_y*(y - num_y/2);
104 if(pos_outlier_dist(gen)) {
105 px += pos_outlier_noise_dist(gen);
106 py += pos_outlier_noise_dist(gen);
108 px += pos_noise_dist(gen);
109 py += pos_noise_dist(gen);
115 euler[0] += rot_noise_dist(gen);
116 euler[1] += rot_noise_dist(gen);
117 euler[2] += rot_roll_noise_dist(gen);
123 view_camera_centers[idx] = p;
124 view_camera_rotations[idx] = cam_R;
128 for(
int feature = 0; feature < features_count; ++feature) {
130 const vec3& w = features[feature];
131 vec3 v = cam_R*(w + p);
133 vec2 i(i_[0]/i_[2], i_[1]/i_[2]);
143 auto feature_it = cors.
features.find(feature_name);
144 if(feature_it == cors.
features.end()) {
147 auto res = cors.
features.emplace(feature_name, std::move(feature));
148 feature_it = res.first;
151 feature_it->second.points[idx] = fpoint;
156 std::cout <<
"saving correspondences" << std::endl;
160 std::cout <<
"saving cameras" << std::endl;
162 for(
const auto& kv : view_camera_centers) {
164 const vec3& p = kv.second;
170 cam.
rotation = view_camera_rotations.at(idx);
174 std::string cameras_filename = out_datas_dir +
"/cameras.json";
178 std::cout <<
"saving straight depths" << std::endl;
180 json j_straight_depths = json::object();
181 for(
int feature = 0; feature < features_count; ++feature) {
183 const vec3& wp = features.at(feature);
185 j_straight_depths[feature_name] = wp[2];
187 export_json_file(j_straight_depths, out_datas_dir +
"/straight_depths.json");
191 std::cout <<
"drawing images and depth maps" << std::endl;
193 const real small_radius = 4.0;
194 const real large_radius = 60.0;
195 const real focal = (intr.
fx() + intr.
fy()) / 2.0;
197 auto draw_circle = [](cv::Mat& mat,
real x,
real y,
real rad, cv::Scalar col,
bool smooth) {
200 int x_int = x * (1<<shift), y_int = y * (1<<shift), rad_int = rad * (1<<shift);
201 cv::circle(mat, cv::Point(x_int, y_int), rad_int, cv::Scalar(col), -1, CV_AA, shift);
203 cv::circle(mat, cv::Point(x, y), rad, cv::Scalar(col), -1, 8);
207 #pragma omp parallel for 208 for(
int y = 0; y < num_y; ++y)
for(
int x = 0; x < num_x; ++x) {
210 std::cout <<
'.' << std::flush;
213 cv::Mat_<cv::Vec3b> texture_image(height, width, background_color);
214 cv::Mat_<ushort> depth_image(height, width, ushort(0xffff));
216 for(
int feature = 0; feature < features_count; ++feature) {
222 cv::Mat_<cv::Vec3b> feature_texture_image(height, width, background_color);
223 cv::Mat_<uchar> feature_mask_image(height, width, uchar(0));
225 int small_radius_pix = focal * small_radius / depth;
226 int large_radius_pix = focal * large_radius / depth;
229 cv::Vec3b col_darker = 0.3 * col;
231 draw_circle(feature_texture_image, x, y, large_radius_pix, cv::Scalar(col_darker),
true);
232 draw_circle(feature_texture_image, x, y, small_radius_pix, cv::Scalar(col),
true);
233 draw_circle(feature_mask_image, x, y, large_radius_pix-2, 255,
true);
235 cv::Mat_<uchar> mask = feature_mask_image & (depth < depth_image);
236 feature_texture_image.copyTo(texture_image, mask);
237 depth_image.setTo(depth, mask);
239 depth_image.setTo(ushort(0), (depth_image == 0xffff));
244 cv::resize(texture_image, texture_image, cv::Size(width, height), 0.0, 0.0, cv::INTER_CUBIC);
246 cv::resize(depth_image, depth_image, cv::Size(width, height), 0.0, 0.0, cv::INTER_NEAREST);
247 save_depth(depth_image_filename, depth_image);
249 std::cout << std::endl;
252 std::cout <<
"done" << std::endl;
void save_depth(const std::string &filename, const cv::Mat_< ushort > &depth)
void export_cameras_file(const camera_array &cameras, const std::string &filename)
long int_opt_arg(long def)
void save_texture(const std::string &filename, const cv::Mat_< cv::Vec3b > &texture)
double real_opt_arg(double def)
Set of features, each on set of views.
cv::Vec3b random_color(int i)
const cv::Vec3b background_color(0, 0, 0)
int main(int argc, const char *argv[])
cv::Mat_< real > decode_mat(const json &j)
std::map< std::string, image_correspondence_feature > features
intrinsics intrinsics_arg()
cv::Matx< real, 3, 3 > mat33
vec2 distort_point(const intrinsics &intr, const vec2 &undistorted)
std::string out_dirname_arg()
void export_json_file(const json &j, const std::string &filename, bool compact)
view_index reference_view
mat33 to_rotation_matrix(const vec3 &euler)
std::string to_string(const T &)
vec3 to_euler(const mat33 &R_)
Feature on set of views. Optionally one view is "reference".
void make_directory(const std::string &dirname)
std::vector< camera > camera_array
void export_image_corresponcences(const image_correspondences &cors, const std::string &filename)
void get_args(int argc, const char *argv[], const std::string &usage)