Commit f63a47d95f731d95bc8694ba802e84ffbf9f3d1d
1 parent
2288768b
added some additional functionality to turkGallery
Showing
2 changed files
with
46 additions
and
7 deletions
openbr/core/common.h
| @@ -116,15 +116,24 @@ T Max(const QList<T> &vals) | @@ -116,15 +116,24 @@ T Max(const QList<T> &vals) | ||
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | /*! | 118 | /*! |
| 119 | + * \brief Returns the sum of a vector of values. | ||
| 120 | + */ | ||
| 121 | +template <template<class> class V, typename T> | ||
| 122 | +double Sum(const V<T> &vals) | ||
| 123 | +{ | ||
| 124 | + double sum = 0; | ||
| 125 | + foreach (T val, vals) sum += val; | ||
| 126 | + return sum; | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +/*! | ||
| 119 | * \brief Returns the mean and standard deviation of a vector of values. | 130 | * \brief Returns the mean and standard deviation of a vector of values. |
| 120 | */ | 131 | */ |
| 121 | template <template<class> class V, typename T> | 132 | template <template<class> class V, typename T> |
| 122 | double Mean(const V<T> &vals) | 133 | double Mean(const V<T> &vals) |
| 123 | { | 134 | { |
| 124 | if (vals.isEmpty()) return 0; | 135 | if (vals.isEmpty()) return 0; |
| 125 | - double sum = 0; | ||
| 126 | - foreach (T val, vals) sum += val; | ||
| 127 | - return sum / vals.size(); | 136 | + return Sum(vals) / vals.size(); |
| 128 | } | 137 | } |
| 129 | 138 | ||
| 130 | /*! | 139 | /*! |
openbr/plugins/turk.cpp
| 1 | #include "openbr_internal.h" | 1 | #include "openbr_internal.h" |
| 2 | +#include "openbr/core/common.h" | ||
| 2 | #include "openbr/core/qtutils.h" | 3 | #include "openbr/core/qtutils.h" |
| 3 | 4 | ||
| 4 | namespace br | 5 | namespace br |
| @@ -12,6 +13,10 @@ namespace br | @@ -12,6 +13,10 @@ namespace br | ||
| 12 | class turkGallery : public Gallery | 13 | class turkGallery : public Gallery |
| 13 | { | 14 | { |
| 14 | Q_OBJECT | 15 | Q_OBJECT |
| 16 | + Q_PROPERTY(bool flat READ get_flat WRITE set_flat RESET reset_flat STORED false) | ||
| 17 | + Q_PROPERTY(bool normalize READ get_normalize WRITE set_normalize RESET reset_normalize STORED false) | ||
| 18 | + BR_PROPERTY(bool, flat, false) | ||
| 19 | + BR_PROPERTY(bool, normalize, false) | ||
| 15 | 20 | ||
| 16 | struct Attribute : public QStringList | 21 | struct Attribute : public QStringList |
| 17 | { | 22 | { |
| @@ -23,6 +28,23 @@ class turkGallery : public Gallery | @@ -23,6 +28,23 @@ class turkGallery : public Gallery | ||
| 23 | if (i != -1) | 28 | if (i != -1) |
| 24 | append(str.mid(i+1, str.length()-i-2).split(",")); | 29 | append(str.mid(i+1, str.length()-i-2).split(",")); |
| 25 | } | 30 | } |
| 31 | + | ||
| 32 | + Attribute normalized() const | ||
| 33 | + { | ||
| 34 | + bool ok; | ||
| 35 | + QList<float> values; | ||
| 36 | + foreach (const QString &value, *this) { | ||
| 37 | + values.append(value.toFloat(&ok)); | ||
| 38 | + if (!ok) | ||
| 39 | + qFatal("Can't normalize non-numeric vector!"); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + Attribute normal(name); | ||
| 43 | + const float sum = Common::Sum(values); | ||
| 44 | + for (int i=0; i<values.size(); i++) | ||
| 45 | + normal.append(QString::number(values[i] / sum)); | ||
| 46 | + return normal; | ||
| 47 | + } | ||
| 26 | }; | 48 | }; |
| 27 | 49 | ||
| 28 | TemplateList readBlock(bool *done) | 50 | TemplateList readBlock(bool *done) |
| @@ -48,10 +70,18 @@ class turkGallery : public Gallery | @@ -48,10 +70,18 @@ class turkGallery : public Gallery | ||
| 48 | if (type.size() != rating.size()) | 70 | if (type.size() != rating.size()) |
| 49 | qFatal(".turk Gallery incorrect ratings count."); | 71 | qFatal(".turk Gallery incorrect ratings count."); |
| 50 | 72 | ||
| 51 | - QMap<QString,QVariant> categoryMap; | ||
| 52 | - for (int j=0; j<type.size(); j++) | ||
| 53 | - categoryMap.insert(type[j], rating[j]); | ||
| 54 | - f.set(type.name, categoryMap); | 73 | + if (normalize) |
| 74 | + rating = rating.normalized(); | ||
| 75 | + | ||
| 76 | + if (flat) { | ||
| 77 | + for (int j=0; j<type.size(); j++) | ||
| 78 | + f.set(type.name + "_" + type[j], rating[j]); | ||
| 79 | + } else { | ||
| 80 | + QMap<QString,QVariant> categoryMap; | ||
| 81 | + for (int j=0; j<type.size(); j++) | ||
| 82 | + categoryMap.insert(type[j], rating[j]); | ||
| 83 | + f.set(type.name, categoryMap); | ||
| 84 | + } | ||
| 55 | } | 85 | } |
| 56 | templates.append(f); | 86 | templates.append(f); |
| 57 | } | 87 | } |