1 // This is contrib/brl/bbas/bpgl/algo/bpgl_heightmap_from_disparity.h
2 #ifndef bpgl_heightmap_from_disparity_h_
3 #define bpgl_heightmap_from_disparity_h_
4 //:
5 // \file
6 // \brief Methods related to z(x,y) heightmap generation
7 // \author Dan Crispell
8 // \date Nov 15, 2018
9 //
10 
11 #include <vgl/vgl_box_3d.h>
12 #include <vgl/vgl_pointset_3d.h>
13 #include <vil/vil_image_view.h>
14 
15 /**
16  * Main convenience function
17  * Given cameras for a stereo pair and a disparity map, return a 3-plane (x,y,z)
18  * image containing the corresponding triangulated 3D points in the form z(x,y)
19  * disparity should contain pixel offsets such that (img1(u, v) <--> img2(u + disparity, v)
20  * for example, given the pixel location (100, 200) in image1, and a disparity value of -20 at that location
21  * in the disparity image,x then the corresponding pixel location in image2 is (80, 200).
22  */
23 template<class T, class CAM_T>
24 vil_image_view<T> bpgl_heightmap_from_disparity(
25     CAM_T const& cam1,
26     CAM_T const& cam2,
27     vil_image_view<T> const& disparity,
28     vgl_box_3d<T> heightmap_bounds,
29     T ground_sample_distance);
30 
31 
32 /**
33  * Helper class, separating each step for fine grained use
34  * Given a triangulated image returned from "bpgl_3d_from_disparity",
35  * convert to pointsets and/or heightmaps as appropriate.
36  **/
37 template<class T>
38 class bpgl_heightmap
39 {
40   public:
41 
42     //: constructor
43     bpgl_heightmap() = default;
44 
bpgl_heightmap(vgl_box_3d<T> heightmap_bounds,T ground_sample_distance)45     bpgl_heightmap(
46         vgl_box_3d<T> heightmap_bounds,
47         T ground_sample_distance) :
48       heightmap_bounds_(heightmap_bounds),
49       ground_sample_distance_(ground_sample_distance)
50     {}
51 
52     //: destructor
53     ~bpgl_heightmap() = default;
54 
55     // Accessors
ground_sample_distance()56     T ground_sample_distance() const { return ground_sample_distance_; }
ground_sample_distance(T x)57     void ground_sample_distance(T x) { ground_sample_distance_ = x; }
58 
heightmap_bounds()59     vgl_box_3d<T> heightmap_bounds() const { return heightmap_bounds_; }
heightmap_bounds(vgl_box_3d<T> x)60     void heightmap_bounds(vgl_box_3d<T> x) { heightmap_bounds_ = x; }
61 
neighbor_dist_factor()62     T neighbor_dist_factor() const { return neighbor_dist_factor_; }
neighbor_dist_factor(T x)63     void neighbor_dist_factor(T x) { neighbor_dist_factor_ = x; }
64 
min_neighbors()65     unsigned min_neighbors() const { return min_neighbors_; }
min_neighbors(unsigned x)66     void min_neighbors(unsigned x) { min_neighbors_ = x; }
67 
max_neighbors()68     unsigned max_neighbors() const { return max_neighbors_; }
max_neighbors(unsigned x)69     void max_neighbors(unsigned x) { max_neighbors_ = x; }
70 
71     //: compute pointset from triangulated image
72     void pointset_from_tri(
73         const vil_image_view<T>& tri_3d,
74         vgl_pointset_3d<T>& ptset_output);
75 
76     //: compute pointset from triangulated image & scalar input
77     void pointset_from_tri(
78         const vil_image_view<T>& tri_3d,
79         const vil_image_view<T>& scalar,
80         vgl_pointset_3d<T>& ptset_output);
81 
82     //: compute heightmap from pointset input
83     void heightmap_from_pointset(
84         const vgl_pointset_3d<T>& ptset,
85         vil_image_view<T>& heightmap_output);
86 
87     //: compute heightmap & scalar field from pointset input
88     void heightmap_from_pointset(
89         const vgl_pointset_3d<T>& ptset,
90         vil_image_view<T>& heightmap_output,
91         vil_image_view<T>& scalar_output);
92 
93     //: compute heightmap, scalar field and radial standard deviation image
94     void heightmap_from_pointset(
95         const vgl_pointset_3d<T>& ptset,
96         vil_image_view<T>& heightmap_output,
97         vil_image_view<T>& scalar_output,
98         vil_image_view<T>& radial_std_dev);
99 
100     //: compute heightmap from triangulated image
101     void heightmap_from_tri(
102         const vil_image_view<T>& tri_3d,
103         vil_image_view<T>& heightmap_output);
104 
105     //: compute heightmap & scalar field from triangulated image & scalar input
106     void heightmap_from_tri(
107         const vil_image_view<T>& tri_3d,
108         const vil_image_view<T>& scalar,
109         vil_image_view<T>& heightmap_output,
110         vil_image_view<T>& scalar_output);
111 
112   private:
113 
114     // compute pointset from triangulated image & scalar input
115     // scalar usage controlled by "ignore_scalar" flag
116     void _pointset_from_tri(
117         const vil_image_view<T>& tri_3d,
118         const vil_image_view<T>& scalar,
119         vgl_pointset_3d<T>& ptset_output,
120         bool ignore_scalar);
121 
122     // compute heightmap & scalar field from pointset input
123     // scalar usage controlled by "ignore_scalar" flag
124     void _heightmap_from_pointset(
125         const vgl_pointset_3d<T>& ptset,
126         vil_image_view<T>& heightmap_output,
127         vil_image_view<T>& scalar_output,
128         bool ignore_scalar);
129 
130 
131     // parameters
132     vgl_box_3d<T> heightmap_bounds_;
133     T ground_sample_distance_;
134 
135     // pointset->heightmap gridding paramters:
136     // expected number of neighbors (between min/max neighbors) within some distance
137     // (neighbor_dist_factor_ * ground_sample_distance_) of each heightmap pixel
138     unsigned min_neighbors_ = 3;
139     unsigned max_neighbors_ = 5;
140     T neighbor_dist_factor_ = 3.0;
141 };
142 
143 #endif
144