From c3e880e6c15be09cd4dae8f067aa3cdbb2d02f06 Mon Sep 17 00:00:00 2001 From: Charles Otto Date: Sun, 17 Nov 2013 14:39:58 -0500 Subject: [PATCH] Refactor BEE::readMask and readSimmat to a common readMat function --- openbr/core/bee.cpp | 70 +++++++++++++++++----------------------------------------------------- openbr/core/bee.h | 3 +-- openbr/core/cluster.cpp | 2 +- openbr/core/core.cpp | 2 +- openbr/core/eval.cpp | 10 +++------- openbr/core/fuse.cpp | 2 +- openbr/plugins/format.cpp | 4 ++-- 7 files changed, 26 insertions(+), 67 deletions(-) diff --git a/openbr/core/bee.cpp b/openbr/core/bee.cpp index 2695dbc..8682215 100644 --- a/openbr/core/bee.cpp +++ b/openbr/core/bee.cpp @@ -135,43 +135,8 @@ void BEE::writeSigset(const QString &sigset, const br::FileList &files, bool ign QtUtils::writeFile(sigset, lines); } -template -Mat readMatrix(const br::File &matrix, QString *targetSigset = NULL, QString *querySigset = NULL) +Mat BEE::readMat(const br::File &matrix, QString *targetSigset, QString *querySigset) { - // Special case matrix construction - if (matrix == "Identity") { - int rows = matrix.get("rows", -1); - int columns = matrix.get("columns", -1); - const int size = matrix.get("size", -1); - if (size != -1) { - if (rows == -1) rows = size; - if (columns == -1) columns = size; - } - const int step = matrix.get("step", 1); - if (rows % step != 0) qFatal("Step does not divide rows evenly."); - if (columns % step != 0) qFatal("Step does not divide columns evenly."); - - if (sizeof(T) == sizeof(BEE::Mask_t)) { - const bool selfSimilar = matrix.get("selfSimilar", false); - - Mat m(rows, columns, CV_8UC1); - m.setTo(BEE::NonMatch); - for (int i=0; i(i+j,i+k) = ((selfSimilar && (j == k)) ? BEE::DontCare : BEE::Match); - return m; - } else if (sizeof(T) == sizeof(BEE::Simmat_t)) { - Mat m(rows, columns, CV_32FC1); - m.setTo(0); - for (int i=0; i(i+j,i+k) = 1; - return m; - } - } - QFile file(matrix); bool success = file.open(QFile::ReadOnly); if (!success) qFatal("Unable to open %s for reading.", qPrintable(matrix.name)); @@ -181,6 +146,7 @@ Mat readMatrix(const br::File &matrix, QString *targetSigset = NULL, QString *qu bool isDistance = (format[0] == 'D'); if (format[1] != '2') qFatal("Invalid matrix header."); + // Read sigsets if (targetSigset != NULL) *targetSigset = file.readLine().simplified(); else file.readLine(); @@ -192,9 +158,17 @@ Mat readMatrix(const br::File &matrix, QString *targetSigset = NULL, QString *qu int rows = words[1].toInt(); int cols = words[2].toInt(); + bool isMask = words[0][1] == 'B'; + int typeSize = isMask ? sizeof(BEE::Mask_t) : sizeof(BEE::Simmat_t); + // Get matrix data - qint64 bytesExpected = (qint64)rows*(qint64)cols*(qint64)sizeof(T); - Mat m(rows, cols, OpenCVType::make()); + qint64 bytesExpected = (qint64)rows*(qint64)cols*(qint64)typeSize; + Mat m; + if (isMask) + m.create(rows, cols, OpenCVType::make()); + else + m.create(rows, cols, OpenCVType::make()); + qint64 read = file.read((char*)m.data, bytesExpected); if (read != bytesExpected) qFatal("Invalid matrix size."); @@ -206,16 +180,6 @@ Mat readMatrix(const br::File &matrix, QString *targetSigset = NULL, QString *qu return result; } -Mat BEE::readSimmat(const br::File &simmat, QString *targetSigset, QString *querySigset) -{ - return readMatrix(simmat, targetSigset, querySigset); -} - -Mat BEE::readMask(const br::File &mask) -{ - return readMatrix(mask); -} - template void writeMatrix(const Mat &m, const QString &matrix, const QString &targetSigset, const QString &querySigset) { @@ -264,15 +228,15 @@ void BEE::writeMask(const Mat &m, const QString &mask, const QString &targetSigs void BEE::readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset) { qDebug("Reading %s header.", qPrintable(matrix)); - if (matrix.endsWith("mask")) readMatrix< Mask_t>(matrix, targetSigset, querySigset); - else readMatrix(matrix, targetSigset, querySigset); + readMat(matrix, targetSigset, querySigset); } void BEE::writeMatrixHeader(const QString &matrix, const QString &targetSigset, const QString &querySigset) { qDebug("Writing %s header to %s %s.", qPrintable(matrix), qPrintable(targetSigset), qPrintable(querySigset)); - if (matrix.endsWith("mask")) writeMatrix< Mask_t>(readMatrix< Mask_t>(matrix), matrix, targetSigset, querySigset); - else writeMatrix(readMatrix(matrix), matrix, targetSigset, querySigset); + + if (matrix.endsWith("mask")) writeMatrix< Mask_t>(readMat(matrix), matrix, targetSigset, querySigset); + else writeMatrix(readMat(matrix), matrix, targetSigset, querySigset); } void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask) @@ -387,7 +351,7 @@ void BEE::combineMasks(const QStringList &inputMasks, const QString &outputMask, QList masks; foreach (const QString &inputMask, inputMasks) - masks.append(readMask(inputMask)); + masks.append(readMat(inputMask)); if (masks.size() < 2) qFatal("Expected at least two masks."); const int rows = masks.first().rows; diff --git a/openbr/core/bee.h b/openbr/core/bee.h index 094de9f..ba561db 100644 --- a/openbr/core/bee.h +++ b/openbr/core/bee.h @@ -39,8 +39,7 @@ namespace BEE void writeSigset(const QString &sigset, const br::FileList &files, bool ignoreMetadata = false); // Matrix - cv::Mat readSimmat(const br::File &simmat, QString *targetSigset = NULL, QString *querySigset = NULL); - cv::Mat readMask(const br::File &mask); + cv::Mat readMat(const br::File & mat, QString * targetSigset = NULL, QString * querySigset = NULL); void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); void writeMask(const cv::Mat &m, const QString &mask, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); void readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset); diff --git a/openbr/core/cluster.cpp b/openbr/core/cluster.cpp index 8c880d9..9657a70 100644 --- a/openbr/core/cluster.cpp +++ b/openbr/core/cluster.cpp @@ -99,7 +99,7 @@ Neighborhood getNeighborhood(const QStringList &simmats) int currentRows = -1; int columnOffset = 0; for (int j=0; jwriteBlock(before->readBlock(&done)); } else if (fileType == "Output") { QString target, query; - cv::Mat m = BEE::readSimmat(inputFile, &target, &query); + cv::Mat m = BEE::readMat(inputFile, &target, &query); const FileList targetFiles = TemplateList::fromGallery(target).files(); const FileList queryFiles = TemplateList::fromGallery(query).files(); diff --git a/openbr/core/eval.cpp b/openbr/core/eval.cpp index 85b9b35..2406a01 100644 --- a/openbr/core/eval.cpp +++ b/openbr/core/eval.cpp @@ -81,7 +81,7 @@ float Evaluate(const QString &simmat, const QString &mask, const QString &csv) QString target, query; Mat scores; if (simmat.endsWith(".mtx")) { - scores = BEE::readSimmat(simmat, &target, &query); + scores = BEE::readMat(simmat, &target, &query); } else { QScopedPointer format(Factory::make(simmat)); scores = format->read(); @@ -99,12 +99,8 @@ float Evaluate(const QString &simmat, const QString &mask, const QString &csv) File maskFile(mask); maskFile.set("rows", scores.rows); maskFile.set("columns", scores.cols); - if (mask.endsWith(".mtx")) - truth = BEE::readMask(mask); - else { - QScopedPointer format(Factory::make(maskFile)); - truth = format->read(); - } + QScopedPointer format(Factory::make(maskFile)); + truth = format->read(); } return Evaluate(scores, truth, csv); diff --git a/openbr/core/fuse.cpp b/openbr/core/fuse.cpp index 8f42dcd..89306c3 100644 --- a/openbr/core/fuse.cpp +++ b/openbr/core/fuse.cpp @@ -82,7 +82,7 @@ void br::Fuse(const QStringList &inputSimmats, const QString &normalization, con QString target, query, previousTarget, previousQuery; QList originalMatrices; foreach (const QString &simmat, inputSimmats) { - originalMatrices.append(BEE::readSimmat(simmat,&target,&query)); + originalMatrices.append(BEE::readMat(simmat,&target,&query)); // Make we're fusing score matrices for the same set of targets and querys if (!previousTarget.isEmpty() && !previousQuery.isEmpty() && (previousTarget != target || previousQuery != query)) qFatal("Target or query files are not the same across fused matrices."); diff --git a/openbr/plugins/format.cpp b/openbr/plugins/format.cpp index 9d33e19..258a06d 100644 --- a/openbr/plugins/format.cpp +++ b/openbr/plugins/format.cpp @@ -280,7 +280,7 @@ class mtxFormat : public Format Template read() const { - return BEE::readSimmat(file); + return BEE::readMat(file); } void write(const Template &t) const @@ -302,7 +302,7 @@ class maskFormat : public Format Template read() const { - return BEE::readMask(file); + return BEE::readMat(file); } void write(const Template &t) const -- libgit2 0.21.4