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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
#include "Surface_mesh_item_classification.h"
#include "Color_ramp.h"
#include <CGAL/Timer.h>
#include <CGAL/Memory_sizer.h>
#include <CGAL/Three/Viewer_interface.h>
#include <QLineEdit>
#include <set>
#include <stack>
#include <algorithm>
Surface_mesh_item_classification::Surface_mesh_item_classification(Scene_surface_mesh_item* mesh)
: m_mesh (mesh),
m_selection (nullptr),
m_generator (nullptr)
{
m_index_color = 1;
backup_existing_colors_and_add_new();
m_training = m_mesh->polyhedron()->add_property_map<face_descriptor, std::size_t>("f:training", std::size_t(-1)).first;
m_classif = m_mesh->polyhedron()->add_property_map<face_descriptor, std::size_t>("f:label", std::size_t(-1)).first;
m_labels.add("ground");
m_labels.add("vegetation");
m_labels.add("roof");
m_labels.add("facade");
m_sowf = new Sum_of_weighted_features (m_labels, m_features);
m_ethz = nullptr;
#ifdef CGAL_LINKED_WITH_OPENCV
m_random_forest = nullptr;
#endif
}
Surface_mesh_item_classification::~Surface_mesh_item_classification()
{
if (m_sowf != nullptr)
delete m_sowf;
if (m_ethz != nullptr)
delete m_ethz;
#ifdef CGAL_LINKED_WITH_OPENCV
if (m_random_forest != nullptr)
delete m_random_forest;
#endif
#ifdef CGAL_LINKED_WITH_TENSORFLOW
if (m_neural_network != nullptr)
delete m_neural_network;
#endif
if (m_generator != nullptr)
delete m_generator;
}
void Surface_mesh_item_classification::backup_existing_colors_and_add_new()
{
std::optional< Mesh::Property_map<face_descriptor, CGAL::IO::Color>> color_map = m_mesh->polyhedron()->property_map<face_descriptor, CGAL::IO::Color>("f:color");
if (color_map.has_value())
{
m_color = color_map.value();
m_real_color
= m_mesh->polyhedron()->add_property_map<face_descriptor, CGAL::IO::Color>("f:real_color").first;
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
m_real_color[fd] = m_color[fd];
m_color[fd] = CGAL::IO::Color(128, 128, 128);
}
}
else
m_color =
m_mesh->polyhedron()->add_property_map<face_descriptor, CGAL::IO::Color>("f:color", CGAL::IO::Color(128,128,128)).first;
}
void Surface_mesh_item_classification::change_color (int index, float* vmin, float* vmax)
{
m_index_color = index;
int index_color = index;
if (index == 0 && m_real_color == Mesh::Property_map<face_descriptor, CGAL::IO::Color>())
index_color = -1;
static Color_ramp ramp;
ramp.build_rainbow();
if (index_color == -1) // item color
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
m_color[fd] = CGAL::IO::Color(128,128,128);
}
else if (index_color == 0) // real colors
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
m_color[fd] = m_real_color[fd];
}
else if (index_color == 1) // classif
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
QColor color (128, 128, 128);
std::size_t c = m_classif[fd];
if (c != std::size_t(-1))
color = label_qcolor (m_labels[c]);
m_color[fd] = CGAL::IO::Color(color.red(), color.green(), color.blue());
}
}
else if (index_color == 2) // training
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
QColor color (128, 128, 128);
std::size_t c = m_training[fd];
std::size_t c2 = m_classif[fd];
if (c != std::size_t(-1))
color = label_qcolor(m_labels[c]);
float div = 1;
if (c != c2)
div = 2;
m_color[fd] = CGAL::IO::Color(color.red() / div,
color.green() / div,
color.blue() / div);
}
}
else
{
std::size_t corrected_index = index_color - 3;
if (corrected_index < m_labels.size()) // Display label probabilities
{
if (m_label_probabilities.size() <= corrected_index ||
m_label_probabilities[corrected_index].size() != num_faces(*(m_mesh->polyhedron())))
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
m_color[fd] = CGAL::IO::Color((unsigned char)(128),
(unsigned char)(128),
(unsigned char)(128));
}
}
else
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
float v = (std::max) (0.f, (std::min)(1.f, m_label_probabilities[corrected_index][fd]));
m_color[fd] = CGAL::IO::Color((unsigned char)(ramp.r(v) * 255),
(unsigned char)(ramp.g(v) * 255),
(unsigned char)(ramp.b(v) * 255));
}
}
}
else
{
corrected_index -= m_labels.size();
if (corrected_index >= m_features.size())
{
std::cerr << "Error: trying to access feature " << corrected_index << " out of " << m_features.size() << std::endl;
return;
}
Feature_handle feature = m_features[corrected_index];
float min = (std::numeric_limits<float>::max)();
float max = -(std::numeric_limits<float>::max)();
if (vmin != nullptr && vmax != nullptr
&& *vmin != std::numeric_limits<float>::infinity()
&& *vmax != std::numeric_limits<float>::infinity())
{
min = *vmin;
max = *vmax;
}
else
{
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
if (feature->value(fd) > max)
max = feature->value(fd);
if (feature->value(fd) < min)
min = feature->value(fd);
}
}
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
float v = (feature->value(fd) - min) / (max - min);
if (v < 0.f) v = 0.f;
if (v > 1.f) v = 1.f;
m_color[fd] = CGAL::IO::Color((unsigned char)(ramp.r(v) * 255),
(unsigned char)(ramp.g(v) * 255),
(unsigned char)(ramp.b(v) * 255));
}
if (vmin != nullptr && vmax != nullptr)
{
*vmin = min;
*vmax = max;
}
}
}
}
void Surface_mesh_item_classification::compute_features (std::size_t nb_scales, float voxel_size)
{
std::cerr << "Computing features with " << nb_scales << " scale(s) and ";
if (voxel_size == -1)
std::cerr << "automatic voxel size" << std::endl;
else
std::cerr << "voxel size = " << voxel_size << std::endl;
m_features.clear();
if (m_generator != nullptr)
delete m_generator;
Face_center_map fc_map (m_mesh->polyhedron());
m_generator = new Generator (*(m_mesh->polyhedron()), fc_map, nb_scales, voxel_size);
#ifdef CGAL_LINKED_WITH_TBB
m_features.begin_parallel_additions();
#endif
m_generator->generate_point_based_features(m_features);
m_generator->generate_face_based_features(m_features);
#ifdef CGAL_LINKED_WITH_TBB
m_features.end_parallel_additions();
#endif
delete m_sowf;
m_sowf = new Sum_of_weighted_features (m_labels, m_features);
if (m_ethz != nullptr)
{
delete m_ethz;
m_ethz = nullptr;
}
#ifdef CGAL_LINKED_WITH_OPENCV
if (m_random_forest != nullptr)
{
delete m_random_forest;
m_random_forest = nullptr;
}
#endif
std::cerr << "Features = " << m_features.size() << std::endl;
}
void Surface_mesh_item_classification::train (int classifier, const QMultipleInputDialog& dialog)
{
if (m_features.size() == 0)
{
std::cerr << "Error: features not computed" << std::endl;
return;
}
m_label_probabilities.clear();
m_label_probabilities.resize (m_labels.size());
for (std::size_t i = 0; i < m_label_probabilities.size(); ++ i)
m_label_probabilities[i].resize (num_faces(*(m_mesh->polyhedron())));
std::vector<std::size_t> training (num_faces(*(m_mesh->polyhedron())), std::size_t(-1));
std::vector<std::size_t> indices (num_faces(*(m_mesh->polyhedron())), std::size_t(-1));
std::vector<std::size_t> nb_label (m_labels.size(), 0);
std::size_t nb_total = 0;
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
{
training[fd] = m_training[fd];
if (training[fd] != std::size_t(-1))
{
nb_label[training[fd]] ++;
++ nb_total;
}
}
std::cerr << nb_total << " face(s) used for training ("
<< 100. * (nb_total / double(m_mesh->polyhedron()->faces().size())) << "% of the total):" << std::endl;
for (std::size_t i = 0; i < m_labels.size(); ++ i)
std::cerr << " * " << m_labels[i]->name() << ": " << nb_label[i] << " face(s)" << std::endl;
if (classifier == CGAL_CLASSIFICATION_SOWF_NUMBER)
{
m_sowf->train<Concurrency_tag>(training, dialog.get<QSpinBox>("trials")->value());
CGAL::Classification::classify<Concurrency_tag> (m_mesh->polyhedron()->faces(),
m_labels, *m_sowf,
indices, m_label_probabilities);
}
else if (classifier == CGAL_CLASSIFICATION_ETHZ_NUMBER)
{
if (m_ethz != nullptr)
delete m_ethz;
m_ethz = new ETHZ_random_forest (m_labels, m_features);
m_ethz->train<Concurrency_tag>(training, true,
dialog.get<QSpinBox>("num_trees")->value(),
dialog.get<QSpinBox>("max_depth")->value());
CGAL::Classification::classify<Concurrency_tag> (m_mesh->polyhedron()->faces(),
m_labels, *m_ethz,
indices, m_label_probabilities);
}
else if (classifier == CGAL_CLASSIFICATION_OPENCV_NUMBER)
{
#ifdef CGAL_LINKED_WITH_OPENCV
if (m_random_forest != nullptr)
delete m_random_forest;
m_random_forest = new Random_forest (m_labels, m_features,
dialog.get<QSpinBox>("max_depth")->value(), 5, 15,
dialog.get<QSpinBox>("num_trees")->value());
m_random_forest->train (training);
CGAL::Classification::classify<Concurrency_tag> (m_mesh->polyhedron()->faces(),
m_labels, *m_random_forest,
indices, m_label_probabilities);
#endif
}
for(face_descriptor fd : faces(*(m_mesh->polyhedron())))
m_classif[fd] = indices[fd];
if (m_index_color == 1 || m_index_color == 2)
change_color (m_index_color);
}
bool Surface_mesh_item_classification::run (int method, int classifier,
std::size_t subdivisions, double smoothing)
{
if (m_features.size() == 0)
{
std::cerr << "Error: features not computed" << std::endl;
return false;
}
if (classifier == CGAL_CLASSIFICATION_SOWF_NUMBER)
run (method, *m_sowf, subdivisions, smoothing);
else if (classifier == CGAL_CLASSIFICATION_ETHZ_NUMBER)
{
if (m_ethz == nullptr)
{
std::cerr << "Error: ETHZ Random Forest must be trained or have a configuration loaded first" << std::endl;
return false;
}
run (method, *m_ethz, subdivisions, smoothing);
}
else if (classifier == CGAL_CLASSIFICATION_OPENCV_NUMBER)
{
#ifdef CGAL_LINKED_WITH_OPENCV
if (m_random_forest == nullptr)
{
std::cerr << "Error: OpenCV Random Forest must be trained or have a configuration loaded first" << std::endl;
return false;
}
run (method, *m_random_forest, subdivisions, smoothing);
#endif
}
return true;
}
|