[a34150]: / contrib / brl / bpro / core / vpgl_pro / processes / vpgl_compute_affine_from_rational_process.cxx  Maximize  Restore  History

Download this file

90 lines (81 with data), 4.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This is brl/bpro/core/vpgl_pro/processes/vpgl_compute_affine_from_rational_process.cxx
//:
// \file
// \brief A process for computing an affine camera approximation of a "local" rational camera.
// i.e. use case: create a bvxm/boxm2 scene, crop a satellite image using the scene and create a "local" rational camera (cropped camera) using its local bounding box in 3d
// The process samples random points from the input bounding box
// projects these points into the image using local rational camera to find image points of these 3d points
// then uses vpgl_affine_camera_compute to fit an affine camera to these projections
// the output affine camera is in the local vertical coordinte system given by the LVCS of the local rational camera
// \author Ozge C. Ozcanli
// \date Nov 26, 2013
#include <bprb/bprb_func_process.h>
#include <bprb/bprb_parameters.h>
#include <brdb/brdb_value.h>
#include <vpgl/algo/vpgl_camera_compute.h>
#include <vpgl/algo/vpgl_affine_rectification.h>
#include <vpgl/vpgl_local_rational_camera.h>
#include <vnl/vnl_random.h>
bool vpgl_compute_affine_from_rat_process_cons(bprb_func_process& pro)
{
//set output types
vcl_vector<vcl_string> input_types_(8);
int i=0;
input_types_[i++] = "vpgl_camera_double_sptr"; // camera -- pass local rational camera
input_types_[i++] = "double"; // min point x (e.g. lower left corner of a scene bbox)
input_types_[i++] = "double"; // min point y
input_types_[i++] = "double"; // min point z
input_types_[i++] = "double"; // max point x (e.g. upper right corner of a scene bbox)
input_types_[i++] = "double"; // max point y
input_types_[i++] = "double"; // max point z
input_types_[i++] = "unsigned"; // n_points -- randomly sample this many points form the voxel volume, e.g. 100
if (!pro.set_input_types(input_types_))
return false;
vcl_vector<vcl_string> output_types_(1);
output_types_[0] = "vpgl_camera_double_sptr"; // output affine camera
return pro.set_output_types(output_types_);
}
bool vpgl_compute_affine_from_rat_process(bprb_func_process& pro)
{
unsigned i = 0;
vpgl_camera_double_sptr camera = pro.get_input<vpgl_camera_double_sptr>(i++);
double min_x = pro.get_input<double>(i++);
double min_y = pro.get_input<double>(i++);
double min_z = pro.get_input<double>(i++);
double max_x = pro.get_input<double>(i++);
double max_y = pro.get_input<double>(i++);
double max_z = pro.get_input<double>(i++);
unsigned n_points = pro.get_input<unsigned>(i++);
if (n_points <= 3)
n_points = 10; // make it minimum 10 points
if (!camera) {
vcl_cout << pro.name() <<" :-- Input 0 is not valid!\n";
return false;
}
vpgl_local_rational_camera<double>* rat_camera = dynamic_cast<vpgl_local_rational_camera<double>*> (camera.as_pointer());
if (!rat_camera) {
vcl_cout << pro.name() <<" :-- Input camera is not a local rational camera!\n";
return false;
}
double width = max_x - min_x;
double depth = max_y - min_y;
double height = max_z - min_z;
vcl_cout << " Using: " << n_points << " to find the affine camera!\n";
vcl_cout << " w: " << width << " d: " << depth << " h: " << height << '\n';
vcl_vector< vgl_point_2d<double> > image_pts;
vcl_vector< vgl_point_3d<double> > world_pts;
vnl_random rng;
for (unsigned i = 0; i < n_points; i++) {
vgl_point_3d<float> corner_world;
double x = rng.drand64()*width + min_x; // sample in local coords
double y = rng.drand64()*depth + min_y;
double z = rng.drand64()*height + min_z;
world_pts.push_back(vgl_point_3d<double>(x,y,z));
double u, v;
camera->project(x,y,z,u,v); // local rational camera has an lvcs, so it handles, local coord to global to image point projection internally
image_pts.push_back(vgl_point_2d<double>(u,v));
}
vpgl_affine_camera<double>* out_camera = vpgl_affine_rectification::compute_affine_cam(image_pts, world_pts);
pro.set_output_val<vpgl_camera_double_sptr>(0, out_camera);
return true;
}