Commit a2c71854966903e1aaaacba6d20ed8ecafbf4607

Authored by Charles Otto
1 parent c3e880e6

Unify BEE::writeSimmat/writeMask

Similar to the read methods, cv::mat contains type information, so there is no
particular need for separate methods for masks and similarity matrices.

Also, fix an apparent bug where .mat files read by openbr were transposed
(compared to their dimensions when writing the files).
openbr/core/bee.cpp
... ... @@ -180,16 +180,17 @@ Mat BEE::readMat(const br::File &matrix, QString *targetSigset, QString *querySi
180 180 return result;
181 181 }
182 182  
183   -template <typename T>
184   -void writeMatrix(const Mat &m, const QString &matrix, const QString &targetSigset, const QString &querySigset)
  183 +void BEE::writeMat(const Mat &m, const QString &matrix, const QString &targetSigset, const QString &querySigset)
185 184 {
186   - if (m.type() != OpenCVType<T,1>::make()) qFatal("Invalid matrix type.");
  185 + bool isMask = false;
  186 + if (m.type() == OpenCVType<BEE::Mask_t,1>::make())
  187 + isMask = true;
  188 + else if (m.type() != OpenCVType<BEE::Simmat_t,1>::make())
  189 + qFatal("Invalid matrix type, .mtx files can only contain single channel float or uchar matrices.");
187 190  
188   - int elemSize = sizeof(T);
189   - QString matrixType;
190   - if (elemSize == 1) matrixType = "B";
191   - else if (elemSize == 4) matrixType = "F";
192   - else qFatal("Invalid element size.");
  191 + int elemSize = isMask ? sizeof(BEE::Mask_t) : sizeof(BEE::Simmat_t);
  192 +
  193 + QString matrixType = isMask ? "B" : "F";
193 194  
194 195 char buff[4];
195 196 QFile file(matrix);
... ... @@ -215,16 +216,6 @@ void writeMatrix(const Mat &amp;m, const QString &amp;matrix, const QString &amp;targetSigse
215 216 file.close();
216 217 }
217 218  
218   -void BEE::writeSimmat(const Mat &m, const QString &simmat, const QString &targetSigset, const QString &querySigset)
219   -{
220   - writeMatrix<Simmat_t>(m, simmat, targetSigset, querySigset);
221   -}
222   -
223   -void BEE::writeMask(const Mat &m, const QString &mask, const QString &targetSigset, const QString &querySigset)
224   -{
225   - writeMatrix<Mask_t>(m, mask, targetSigset, querySigset);
226   -}
227   -
228 219 void BEE::readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset)
229 220 {
230 221 qDebug("Reading %s header.", qPrintable(matrix));
... ... @@ -235,8 +226,7 @@ void BEE::writeMatrixHeader(const QString &amp;matrix, const QString &amp;targetSigset,
235 226 {
236 227 qDebug("Writing %s header to %s %s.", qPrintable(matrix), qPrintable(targetSigset), qPrintable(querySigset));
237 228  
238   - if (matrix.endsWith("mask")) writeMatrix< Mask_t>(readMat(matrix), matrix, targetSigset, querySigset);
239   - else writeMatrix<Simmat_t>(readMat(matrix), matrix, targetSigset, querySigset);
  229 + writeMat(readMat(matrix), matrix, targetSigset, querySigset);
240 230 }
241 231  
242 232 void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask)
... ... @@ -245,11 +235,11 @@ void BEE::makeMask(const QString &amp;targetInput, const QString &amp;queryInput, const
245 235 FileList targets = TemplateList::fromGallery(targetInput).files();
246 236 FileList queries = (queryInput == ".") ? targets : TemplateList::fromGallery(queryInput).files();
247 237 int partitions = targets.first().get<int>("crossValidate");
248   - if (partitions == 0) writeMask(makeMask(targets, queries), mask, targetInput, queryInput);
  238 + if (partitions == 0) writeMat(makeMask(targets, queries), mask, targetInput, queryInput);
249 239 else {
250 240 if (!mask.contains("%1")) qFatal("Mask file name missing partition number place marker (%%1)");
251 241 for (int i=0; i<partitions; i++) {
252   - writeMask(makeMask(targets, queries, i), mask.arg(i), targetInput, queryInput);
  242 + writeMat(makeMask(targets, queries, i), mask.arg(i), targetInput, queryInput);
253 243 }
254 244 }
255 245 }
... ... @@ -259,11 +249,11 @@ void BEE::makePairwiseMask(const QString &amp;targetInput, const QString &amp;queryInput
259 249 FileList targets = TemplateList::fromGallery(targetInput).files();
260 250 FileList queries = (queryInput == ".") ? targets : TemplateList::fromGallery(queryInput).files();
261 251 int partitions = targets.first().get<int>("crossValidate");
262   - if (partitions == 0) writeMask(makePairwiseMask(targets, queries), mask, targetInput, queryInput);
  252 + if (partitions == 0) writeMat(makePairwiseMask(targets, queries), mask, targetInput, queryInput);
263 253 else {
264 254 if (!mask.contains("%1")) qFatal("Mask file name missing partition number place marker (%%1)");
265 255 for (int i=0; i<partitions; i++) {
266   - writeMask(makePairwiseMask(targets, queries, i), mask.arg(i), targetInput, queryInput);
  256 + writeMat(makePairwiseMask(targets, queries, i), mask.arg(i), targetInput, queryInput);
267 257 }
268 258 }
269 259 }
... ... @@ -387,5 +377,5 @@ void BEE::combineMasks(const QStringList &amp;inputMasks, const QString &amp;outputMask,
387 377 }
388 378 }
389 379  
390   - writeMask(combinedMask, outputMask, "Combined_Targets", "Combined_Queries");
  380 + writeMat(combinedMask, outputMask, "Combined_Targets", "Combined_Queries");
391 381 }
... ...
openbr/core/bee.h
... ... @@ -40,8 +40,7 @@ namespace BEE
40 40  
41 41 // Matrix
42 42 cv::Mat readMat(const br::File & mat, QString * targetSigset = NULL, QString * querySigset = NULL);
43   - void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query");
44   - void writeMask(const cv::Mat &m, const QString &mask, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query");
  43 + void writeMat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query");
45 44 void readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset);
46 45 void writeMatrixHeader(const QString &matrix, const QString &targetSigset, const QString &querySigset);
47 46  
... ...
openbr/core/core.cpp
... ... @@ -235,15 +235,21 @@ struct AlgorithmCore
235 235 TemplateList targets = t->read();
236 236  
237 237 // Use a single file for one of the dimensions so that the output makes the right size file
238   - FileList dummyQuery;
239   - dummyQuery.append(targets[0]);
240   - QScopedPointer<Output> realOutput(Output::make(output, targetFiles, dummyQuery));
241   -
  238 + FileList dummyTarget;
  239 + dummyTarget.append(targets[0]);
  240 + QScopedPointer<Output> realOutput(Output::make(output, dummyTarget, queryFiles));
  241 +
  242 + // Some outputs assume Globals->blockSize is a real thing, of course we have no interest in it.
  243 + int old_block_size = Globals->blockSize;
  244 + Globals->blockSize = INT_MAX;
  245 + realOutput->setBlock(0,0);
242 246 for (int i=0; i < queries.length(); i++)
243 247 {
244 248 float res = distance->compare(queries[i], targets[i]);
245 249 realOutput->setRelative(res, 0,i);
246 250 }
  251 +
  252 + Globals->blockSize = old_block_size;
247 253 }
248 254  
249 255 void compare(File targetGallery, File queryGallery, File output)
... ...
openbr/core/fuse.cpp
... ... @@ -160,5 +160,5 @@ void br::Fuse(const QStringList &amp;inputSimmats, const QString &amp;normalization, con
160 160  
161 161 } while (partition < crossValidate);
162 162  
163   - BEE::writeSimmat(buffer, outputSimmat);
  163 + BEE::writeMat(buffer, outputSimmat);
164 164 }
... ...
openbr/plugins/format.cpp
... ... @@ -285,7 +285,7 @@ class mtxFormat : public Format
285 285  
286 286 void write(const Template &t) const
287 287 {
288   - BEE::writeSimmat(t, file);
  288 + BEE::writeMat(t, file);
289 289 }
290 290 };
291 291  
... ... @@ -307,7 +307,7 @@ class maskFormat : public Format
307 307  
308 308 void write(const Template &t) const
309 309 {
310   - BEE::writeMask(t, file);
  310 + BEE::writeMat(t, file);
311 311 }
312 312 };
313 313  
... ... @@ -418,9 +418,7 @@ class matFormat : public Format
418 418 }
419 419  
420 420 if ((rows > 0) && (columns > 0) && (matrixType != 0) && !matrixData.isEmpty()) {
421   - Mat transposed;
422   - transpose(Mat(rows, columns, matrixType, matrixData.data()), transposed);
423   - t.append(transposed);
  421 + t.append(Mat(rows, columns, matrixType, matrixData.data()));
424 422 }
425 423 }
426 424 }
... ...