Commit 5e6b30e94bf625b4ff4cd6e48114a6ddfce27aad
1 parent
1d5bad33
Added Sum and Attribute Distances
Showing
1 changed file
with
58 additions
and
0 deletions
openbr/plugins/distance.cpp
| ... | ... | @@ -388,5 +388,63 @@ class OnlineDistance : public Distance |
| 388 | 388 | |
| 389 | 389 | BR_REGISTER(Distance, OnlineDistance) |
| 390 | 390 | |
| 391 | +/*! | |
| 392 | + */ | |
| 393 | +class AttributeDistance : public Distance | |
| 394 | +{ | |
| 395 | + Q_OBJECT | |
| 396 | + Q_PROPERTY(QString attribute READ get_attribute WRITE set_attribute RESET reset_attribute STORED false) | |
| 397 | + BR_PROPERTY(QString, attribute, QString()) | |
| 398 | + | |
| 399 | + float compare(const Template &target, const Template &query) const | |
| 400 | + { | |
| 401 | + // Make gaussian attenuation distribution around query attribute value | |
| 402 | + float queryValue = query.file.get<float>(attribute); | |
| 403 | + float targetValue = target.file.get<float>(attribute); | |
| 404 | + | |
| 405 | + float stddev = 1; | |
| 406 | + | |
| 407 | + if (queryValue == targetValue) return 1; | |
| 408 | + else return 1/(stddev*sqrt(2*CV_PI))*exp(-0.5*pow((targetValue-queryValue)/stddev, 2)); | |
| 409 | + } | |
| 410 | +}; | |
| 411 | + | |
| 412 | +BR_REGISTER(Distance, AttributeDistance) | |
| 413 | + | |
| 414 | +/*! | |
| 415 | + */ | |
| 416 | +class SumDistance : public Distance | |
| 417 | +{ | |
| 418 | + Q_OBJECT | |
| 419 | + Q_PROPERTY(QList<br::Distance*> distances READ get_distances WRITE set_distances RESET reset_distances) | |
| 420 | + Q_PROPERTY(QString attribute READ get_attribute WRITE set_attribute RESET reset_attribute STORED false) | |
| 421 | + BR_PROPERTY(QList<br::Distance*>, distances, QList<br::Distance*>()) | |
| 422 | + BR_PROPERTY(QString, attribute, QString()) | |
| 423 | + | |
| 424 | + void train(const TemplateList &data) | |
| 425 | + { | |
| 426 | + QFutureSynchronizer<void> futures; | |
| 427 | + foreach (br::Distance *distance, distances) | |
| 428 | + futures.addFuture(QtConcurrent::run(distance, &Distance::train, data)); | |
| 429 | + futures.waitForFinished(); | |
| 430 | + } | |
| 431 | + | |
| 432 | + float compare(const Template &target, const Template &query) const | |
| 433 | + { | |
| 434 | + float result = 0; | |
| 435 | + | |
| 436 | + foreach (br::Distance *distance, distances) { | |
| 437 | + result += distance->compare(target, query); | |
| 438 | + | |
| 439 | + if (result == -std::numeric_limits<float>::max()) | |
| 440 | + return result; | |
| 441 | + } | |
| 442 | + | |
| 443 | + return result; | |
| 444 | + } | |
| 445 | +}; | |
| 446 | + | |
| 447 | +BR_REGISTER(Distance, SumDistance) | |
| 448 | + | |
| 391 | 449 | } // namespace br |
| 392 | 450 | #include "distance.moc" | ... | ... |