[778818]: contrib / brl / bbas / volm / pro / processes / volm_generate_class_map_process.cxx Maximize Restore History

Download this file

volm_generate_class_map_process.cxx    165 lines (143 with data), 8.0 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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This is brl/bbas/volm/pro/processes/volm_generate_class_map_process.cxx
#include <bprb/bprb_func_process.h>
//:
// \file
// Take a color classification map generated by sdet classifier and generate corresponding volm land id map
//
//
#include <bprb/bprb_parameters.h>
#include <vil/vil_image_view.h>
#include <volm/volm_category_io.h>
//:
// Take a colored segmentation output and map it to volm labels
bool volm_generate_class_map_process_cons(bprb_func_process& pro)
{
vcl_vector<vcl_string> input_types;
input_types.push_back("vil_image_view_base_sptr"); // classification image
vcl_vector<vcl_string> output_types;
output_types.push_back("vil_image_view_base_sptr"); // output unsigned short image with volm_land_type ids
return pro.set_input_types(input_types)
&& pro.set_output_types(output_types);
}
//: Execute the process
bool volm_generate_class_map_process(bprb_func_process& pro)
{
if (pro.n_inputs() < 1) {
vcl_cout << "volm_map_osm_process: The number of inputs should be 1" << vcl_endl;
return false;
}
// get the inputs
vil_image_view_base_sptr img_sptr = pro.get_input<vil_image_view_base_sptr>(0);
vil_image_view<vil_rgb<vxl_byte> > img(img_sptr);
vil_image_view<unsigned short> out_img(img_sptr->ni(), img_sptr->nj(), 1);
out_img.fill(0);
vcl_map<vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >, unsigned short> sdet_color_map;
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(52, vcl_pair<vxl_byte, vxl_byte>(226, 127))] = 15; // volm_category_io::volm_land_table - "building";
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(254, vcl_pair<vxl_byte, vxl_byte>(17, 199))] = 242; // "palm_tree";
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(185, vcl_pair<vxl_byte, vxl_byte>(242, 86))] = 0; //"invalid"; // actually park/open in sdet but we're mapping to invalid in volm
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(20, vcl_pair<vxl_byte, vxl_byte>(166, 41))] = 31; // "road"; // actually parking_lot in sdet
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(191, vcl_pair<vxl_byte, vxl_byte>(184, 98))] = 31; // "road";
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(225, vcl_pair<vxl_byte, vxl_byte>(36, 147))] = 0; // "invalid"; // actually shadow in sdet
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(116, vcl_pair<vxl_byte, vxl_byte>(45, 119))] = 31; //"road"; // actually street in sdet
sdet_color_map[vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >(41, vcl_pair<vxl_byte, vxl_byte>(234, 166))] = 243; //"tree";
for (unsigned i = 0; i < img.ni(); i++)
for (unsigned j = 0; j < img.nj(); j++) {
vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> > pp(img(i,j).r, vcl_pair<vxl_byte, vxl_byte>(img(i,j).g, img(i,j).b));
vcl_map<vcl_pair<vxl_byte, vcl_pair<vxl_byte, vxl_byte> >, unsigned short>::iterator iter = sdet_color_map.find(pp);
if (iter != sdet_color_map.end())
out_img(i, j) = iter->second;
else
out_img(i, j) = 0; // map to invalid if undefined
}
vil_image_view_base_sptr out_img_sptr = new vil_image_view<unsigned short>(out_img);
pro.set_output_val<vil_image_view_base_sptr>(0, out_img_sptr);
return true;
}
//:
// a process generating color class map from volm id class map
bool volm_generate_color_class_map_process_cons(bprb_func_process& pro)
{
vcl_vector<vcl_string> input_types;
input_types.push_back("vil_image_view_base_sptr"); // classification image
vcl_vector<vcl_string> output_types;
output_types.push_back("vil_image_view_base_sptr"); // output unsigned short image with volm_land_type ids
return pro.set_input_types(input_types)
&& pro.set_output_types(output_types);
}
bool volm_generate_color_class_map_process(bprb_func_process& pro)
{
if (pro.n_inputs() < 1) {
vcl_cout << "volm_map_osm_process: The number of inputs should be 1" << vcl_endl;
return false;
}
// get the inputs
vil_image_view_base_sptr img_sptr = pro.get_input<vil_image_view_base_sptr>(0);
vil_image_view<unsigned char> img(img_sptr);
vil_image_view<vil_rgb<vxl_byte> > out_img(img_sptr->ni(), img_sptr->nj(), 1);
out_img.fill(vil_rgb<vxl_byte>(0,0,0));
vcl_map<unsigned char, vil_rgb<vxl_byte> > sdet_color_map;
sdet_color_map[0] = vil_rgb<vxl_byte>(225,36 ,147);
sdet_color_map[15] = vil_rgb<vxl_byte>(52 ,226,127);
sdet_color_map[28] = vil_rgb<vxl_byte>(185,242,86 );
sdet_color_map[31] = vil_rgb<vxl_byte>(191,184,98 );
sdet_color_map[32] = vil_rgb<vxl_byte>(20, 166,41 );
sdet_color_map[242] = vil_rgb<vxl_byte>(254,17 ,199);
sdet_color_map[243] = vil_rgb<vxl_byte>(41, 234,166);
for (unsigned i = 0; i < img.ni(); i++)
for (unsigned j = 0; j < img.nj(); j++) {
vcl_map<unsigned char, vil_rgb<vxl_byte> >::iterator mit = sdet_color_map.find(img(i,j));
if (mit != sdet_color_map.end())
out_img(i,j) = mit->second;
}
vil_image_view_base_sptr out_img_sptr = new vil_image_view<vil_rgb<vxl_byte> >(out_img);
pro.set_output_val<vil_image_view_base_sptr>(0, out_img_sptr);
return true;
}
//:
// a process that uses a source class map to update current class map.
// The source class map supposes to have more accurate tree/road land categories yet unclear parks/parking lot/building boundaries.
// Therefore the new source class map will overwrite current class map in terms of non-building region while keep building pixels which
// reside on open space
bool volm_update_class_map_process_cons(bprb_func_process& pro)
{
vcl_vector<vcl_string> input_types_;
input_types_.push_back("vil_image_view_base_sptr"); // current classification map
input_types_.push_back("vil_image_view_base_sptr"); // source classification map used to update the class map
vcl_vector<vcl_string> output_types_;
return pro.set_input_types(input_types_) && pro.set_output_types(output_types_);
}
bool volm_update_class_map_process(bprb_func_process& pro)
{
if (pro.n_inputs() < 2) {
vcl_cout << pro.name() << ": The number of inputs should be 2" << vcl_endl;
return false;
}
// get the inputs
vil_image_view_base_sptr curr_img_sptr = pro.get_input<vil_image_view_base_sptr>(0);
vil_image_view_base_sptr srce_img_sptr = pro.get_input<vil_image_view_base_sptr>(1);
vil_image_view<unsigned char>* curr_img = dynamic_cast<vil_image_view<unsigned char>* >(curr_img_sptr.ptr());
vil_image_view<unsigned char>* srce_img = dynamic_cast<vil_image_view<unsigned char>* >(srce_img_sptr.ptr());
unsigned ni = curr_img->ni();
unsigned nj = curr_img->nj();
if (srce_img->ni() != ni || srce_img->nj() != nj) {
vcl_cout << pro.name() << ": The class map and source class map have difference in size (" << ni << 'x' << nj << "), and (" << srce_img->ni() << 'x' << srce_img->nj() << ')' << vcl_endl;
return false;
}
unsigned cnt = 0;
for (unsigned i = 0; i < ni; i++) {
for (unsigned j = 0; j < nj; j++) {
bool is_parks_or_lot = (*srce_img)(i,j) == volm_osm_category_io::volm_land_table_name["parks"].id_ ||
(*srce_img)(i,j) == volm_osm_category_io::volm_land_table_name["parking"].id_ ||
(*srce_img)(i,j) == volm_osm_category_io::volm_land_table_name["invalid"].id_;
if ( (*curr_img)(i,j) == volm_osm_category_io::volm_land_table_name["building"].id_ && is_parks_or_lot) { // keep previous building if source is open space, parking lot or invalid
//vcl_cout << " pixel (" << i << "," << j << ") has current value " << (unsigned)(*curr_img)(i,j) << " and source value " << (unsigned)(*srce_img)(i,j) << vcl_endl;
cnt++;
continue;
}
// overwrite the class map with new source
(*curr_img)(i,j) = (*srce_img)(i,j);
}
}
vcl_cout << " there are " << cnt << " building pixels kept during update" << vcl_endl;
return true;
}