| 273 | // ---------------------------------------------------------------------------------------- |
| 274 | |
| 275 | void propagate_labels( |
| 276 | const std::string& label, |
| 277 | dlib::image_dataset_metadata::dataset& data, |
| 278 | unsigned long prev, |
| 279 | unsigned long next |
| 280 | ) |
| 281 | { |
| 282 | if (prev == next || next >= data.images.size()) |
| 283 | return; |
| 284 | |
| 285 | |
| 286 | for (unsigned long i = 0; i < data.images[prev].boxes.size(); ++i) |
| 287 | { |
| 288 | if (data.images[prev].boxes[i].label != label) |
| 289 | continue; |
| 290 | |
| 291 | // figure out which box in the next image matches the current one the best |
| 292 | const rectangle cur = data.images[prev].boxes[i].rect; |
| 293 | double best_overlap = 0; |
| 294 | unsigned long best_idx = 0; |
| 295 | for (unsigned long j = 0; j < data.images[next].boxes.size(); ++j) |
| 296 | { |
| 297 | const rectangle next_box = data.images[next].boxes[j].rect; |
| 298 | const double overlap = cur.intersect(next_box).area()/(double)(cur+next_box).area(); |
| 299 | if (overlap > best_overlap) |
| 300 | { |
| 301 | best_overlap = overlap; |
| 302 | best_idx = j; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // If we found a matching rectangle in the next image and the best match doesn't |
| 307 | // already have a label. |
| 308 | if (best_overlap > 0.5 && data.images[next].boxes[best_idx].label == "") |
| 309 | { |
| 310 | data.images[next].boxes[best_idx].label = label; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | // ---------------------------------------------------------------------------------------- |
| 317 |
no test coverage detected