Commit b5f4124c6302f93e33c3a52fee5bd9bf45feec39
1 parent
1c82d1ec
finished draft of WordWiseTransform
Showing
1 changed file
with
31 additions
and
16 deletions
sdk/plugins/integral.cpp
| ... | ... | @@ -133,38 +133,53 @@ class WordWiseTransform : public Transform |
| 133 | 133 | Q_OBJECT |
| 134 | 134 | Q_PROPERTY(br::Transform* getWords READ get_getWords WRITE set_getWords RESET reset_getWords) |
| 135 | 135 | Q_PROPERTY(br::Transform* byWord READ get_byWord WRITE set_byWord RESET reset_byWord) |
| 136 | + Q_PROPERTY(int numWords READ get_numWords WRITE set_numWords RESET reset_numWords) | |
| 136 | 137 | BR_PROPERTY(br::Transform*, getWords, NULL) |
| 137 | 138 | BR_PROPERTY(br::Transform*, byWord, NULL) |
| 139 | + BR_PROPERTY(int, numWords, 0) | |
| 138 | 140 | |
| 139 | 141 | void train(const TemplateList &data) |
| 140 | 142 | { |
| 141 | 143 | getWords->train(data); |
| 142 | - TemplateList words; | |
| 143 | - getWords->project(data, words); | |
| 144 | + TemplateList bins; | |
| 145 | + getWords->project(data, bins); | |
| 144 | 146 | |
| 145 | - const int columns = data.first().m().cols; | |
| 146 | - int numWords = 0; | |
| 147 | - foreach (const Template &t, words) { | |
| 147 | + numWords = 0; | |
| 148 | + foreach (const Template &t, bins) { | |
| 148 | 149 | double minVal, maxVal; |
| 149 | 150 | minMaxLoc(t, &minVal, &maxVal); |
| 150 | 151 | numWords = max(numWords, int(maxVal)+1); |
| 151 | 152 | } |
| 152 | 153 | |
| 153 | - QVector<int> wordCounts(numWords, 0); | |
| 154 | - foreach (const Template &t, words) { | |
| 155 | - const Mat &m = t.m(); | |
| 156 | - for (int i=0; i<m.rows; i++) | |
| 157 | - wordCounts[m.at<uchar>(i,0)]++; | |
| 158 | - } | |
| 159 | - | |
| 160 | - QVector<Mat> trainingWords(numWords); | |
| 161 | - for (int i=0; i<numWords; i++) | |
| 162 | - trainingWords[i] = Mat(wordCounts[i], columns, CV_8UC1); | |
| 154 | + TemplateList reworded; reworded.reserve(data.size()); | |
| 155 | + foreach (const Template &t, data) | |
| 156 | + reworded.append(reword(t)); | |
| 157 | + byWord->train(reworded); | |
| 163 | 158 | } |
| 164 | 159 | |
| 165 | 160 | void project(const Template &src, Template &dst) const |
| 166 | 161 | { |
| 167 | - (void) src; (void) dst; | |
| 162 | + Template reworded; | |
| 163 | + getWords->project(src, reworded); | |
| 164 | + byWord->project(reworded, dst); | |
| 165 | + } | |
| 166 | + | |
| 167 | + Template reword(const Template &src) const | |
| 168 | + { | |
| 169 | + Template words; | |
| 170 | + getWords->project(src, words); | |
| 171 | + QVector<int> wordCounts(numWords, 0); | |
| 172 | + for (int i=0; i<words.m().rows; i++) | |
| 173 | + wordCounts[words.m().at<uchar>(i,0)]++; | |
| 174 | + Template reworded(src.file); reworded.reserve(numWords); | |
| 175 | + for (int i=0; i<numWords; i++) | |
| 176 | + reworded.append(Mat(wordCounts[i], src.m().cols, src.m().type())); | |
| 177 | + QVector<int> indicies(numWords, 0); | |
| 178 | + for (int i=0; i<src.m().rows; i++) { | |
| 179 | + const int word = words.m().at<uchar>(i,0); | |
| 180 | + reworded[word].row(indicies[word]++) = src.m().row(i); | |
| 181 | + } | |
| 182 | + return reworded; | |
| 168 | 183 | } |
| 169 | 184 | }; |
| 170 | 185 | ... | ... |