Commit 46bec4ccfa1ad93c357237e0eb0271acf81c8729
Merge branch 'master' of https://github.com/biometrics/openbr
Showing
4 changed files
with
34 additions
and
32 deletions
sdk/plugins/compare.cpp
| ... | ... | @@ -42,7 +42,7 @@ public: |
| 42 | 42 | INF, |
| 43 | 43 | L1, |
| 44 | 44 | L2, |
| 45 | - CosineSimilarity }; | |
| 45 | + Cosine }; | |
| 46 | 46 | |
| 47 | 47 | private: |
| 48 | 48 | BR_PROPERTY(Metric, metric, L2) |
| ... | ... | @@ -76,8 +76,8 @@ private: |
| 76 | 76 | case L2: |
| 77 | 77 | result = norm(a, b, NORM_L2); |
| 78 | 78 | break; |
| 79 | - case CosineSimilarity: | |
| 80 | - result = cosineSimilarity(a, b); | |
| 79 | + case Cosine: | |
| 80 | + result = cosine(a, b); | |
| 81 | 81 | break; |
| 82 | 82 | default: |
| 83 | 83 | qFatal("Invalid metric"); |
| ... | ... | @@ -89,33 +89,29 @@ private: |
| 89 | 89 | return -log(result+1); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - static float cosineSimilarity(const Mat &a, const Mat &b) | |
| 92 | + static float cosine(const Mat &a, const Mat &b) | |
| 93 | 93 | { |
| 94 | - assert((a.type() == CV_32FC1) && (b.type() == CV_32FC1)); | |
| 95 | - assert((a.rows == b.rows) && (a.cols == b.cols)); | |
| 96 | - | |
| 97 | - float denom = 0; | |
| 98 | - float tnum = 0; | |
| 99 | - float qnum = 0; | |
| 94 | + float dot = 0; | |
| 95 | + float magA = 0; | |
| 96 | + float magB = 0; | |
| 100 | 97 | |
| 101 | 98 | for (int row=0; row<a.rows; row++) { |
| 102 | 99 | for (int col=0; col<a.cols; col++) { |
| 103 | - float target = a.at<float>(row,col); | |
| 104 | - float query = b.at<float>(row,col); | |
| 100 | + const float target = a.at<float>(row,col); | |
| 101 | + const float query = b.at<float>(row,col); | |
| 105 | 102 | |
| 106 | - denom += target * query; | |
| 107 | - tnum += target * target; | |
| 108 | - qnum += query * query; | |
| 103 | + dot += target * query; | |
| 104 | + magA += target * target; | |
| 105 | + magB += query * query; | |
| 109 | 106 | } |
| 110 | 107 | } |
| 111 | 108 | |
| 112 | - return denom / (sqrt(tnum)*sqrt(qnum)); | |
| 109 | + return dot / (sqrt(magA)*sqrt(magB)); | |
| 113 | 110 | } |
| 114 | 111 | }; |
| 115 | 112 | |
| 116 | 113 | BR_REGISTER(Distance, Dist) |
| 117 | 114 | |
| 118 | - | |
| 119 | 115 | /*! |
| 120 | 116 | * \ingroup distances |
| 121 | 117 | * \brief Fast 8-bit L1 distance | ... | ... |
sdk/plugins/regions.cpp
| ... | ... | @@ -79,23 +79,29 @@ BR_REGISTER(Transform, ByRow) |
| 79 | 79 | class Cat : public UntrainableMetaTransform |
| 80 | 80 | { |
| 81 | 81 | Q_OBJECT |
| 82 | + Q_PROPERTY(int partitions READ get_partitions WRITE set_partitions RESET reset_partitions) | |
| 83 | + BR_PROPERTY(int, partitions, 1) | |
| 82 | 84 | |
| 83 | 85 | void project(const Template &src, Template &dst) const |
| 84 | 86 | { |
| 85 | - int vals = 0; | |
| 86 | - foreach (const cv::Mat &m, src) | |
| 87 | - vals += m.total() * m.channels(); | |
| 88 | - | |
| 89 | - Mat cat(1, (int)vals, CV_32FC1); | |
| 90 | - int offset = 0; | |
| 91 | - foreach (const cv::Mat &m, src) { | |
| 92 | - size_t size = m.total() * m.elemSize(); | |
| 93 | - memcpy(&cat.data[offset], m.ptr(), size); | |
| 94 | - offset += size; | |
| 95 | - } | |
| 96 | - | |
| 97 | 87 | dst.file = src.file; |
| 98 | - dst = cat; | |
| 88 | + | |
| 89 | + if (src.size() % partitions != 0) | |
| 90 | + qFatal("Cat %d partitions does not evenly divide %d matrices.", partitions, src.size()); | |
| 91 | + QVector<int> sizes(partitions, 0); | |
| 92 | + for (int i=0; i<src.size(); i++) | |
| 93 | + sizes[i%partitions] += src[i].total() * src[i].channels(); | |
| 94 | + | |
| 95 | + foreach (int size, sizes) | |
| 96 | + dst.append(Mat(1, size, CV_32FC1)); | |
| 97 | + | |
| 98 | + QVector<int> offsets(partitions, 0); | |
| 99 | + for (int i=0; i<src.size(); i++) { | |
| 100 | + size_t size = src[i].total() * src[i].elemSize(); | |
| 101 | + int j = i%partitions; | |
| 102 | + memcpy(&dst[j].data[offsets[j]], src[i].ptr(), size); | |
| 103 | + offsets[j] += size; | |
| 104 | + } | |
| 99 | 105 | } |
| 100 | 106 | }; |
| 101 | 107 | ... | ... |
share/openbr/openbr.bib