From 973ce6bfcd6fcbce54dd437765154032e63835da Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Mon, 14 Jan 2013 17:28:00 -0500 Subject: [PATCH] generalized Convert --- sdk/core/bee.cpp | 58 ---------------------------------------------------------- sdk/core/bee.h | 6 ------ sdk/core/core.cpp | 7 +++++++ sdk/openbr.cpp | 14 +++----------- sdk/openbr.h | 6 ++---- sdk/openbr_plugin.h | 7 +++++++ sdk/plugins/format.cpp | 6 +++++- 7 files changed, 24 insertions(+), 80 deletions(-) diff --git a/sdk/core/bee.cpp b/sdk/core/bee.cpp index d5f70d6..fe65fa2 100644 --- a/sdk/core/bee.cpp +++ b/sdk/core/bee.cpp @@ -225,64 +225,6 @@ void BEE::writeMask(const Mat &m, const QString &mask, const QString &targetSigs writeMatrix(m, mask, targetSigset, querySigset); } -template -void matrixToCSV(const QString &matrix, const QString &csv) -{ - qDebug("Converting %s to %s", qPrintable(matrix), qPrintable(csv)); - - QFile out(csv); - out.open(QFile::WriteOnly); - - Mat m = readMatrix(matrix); - for (int i=0; i(i,j)))); - out.write(","); - } - out.write("\n"); - } -} - -void BEE::simmatToCSV(const QString &simmat, const QString &csv) -{ - matrixToCSV(simmat, csv); -} - -void BEE::maskToCSV(const QString &mask, const QString &csv) -{ - matrixToCSV(mask, csv); -} - -template -void CSVToMatrix(const QString &csv, const QString &matrix) -{ - qDebug("Converting %s to %s", qPrintable(csv), qPrintable(matrix)); - - QStringList lines = QtUtils::readLines(csv); - Mat m(lines.size(), lines.first().split(",", QString::SkipEmptyParts).size(), OpenCVType::make()); - - for (int i=0; i(i, j) = words[j].toFloat(&ok); - if (!ok) qFatal("bee.cpp::CSVToMatrix failed to convert %s to floating point format.", qPrintable(words[j])); - } - } - - writeMatrix(m, matrix, "Unknown_Target", "Unknown_Query"); -} - -void BEE::CSVToSimmat(const QString &csv, const QString &simmat) -{ - CSVToMatrix(csv, simmat); -} - -void BEE::CSVToMask(const QString &csv, const QString &mask) -{ - CSVToMatrix(csv, mask); -} - void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask) { qDebug("Making mask from %s and %s to %s", qPrintable(targetInput), qPrintable(queryInput), qPrintable(mask)); diff --git a/sdk/core/bee.h b/sdk/core/bee.h index b16c9c9..f51c1c3 100644 --- a/sdk/core/bee.h +++ b/sdk/core/bee.h @@ -44,12 +44,6 @@ namespace BEE 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"); - // CSV IO - void simmatToCSV(const QString &simmat, const QString &csv); - void maskToCSV(const QString &mask, const QString &csv); - void CSVToSimmat(const QString &csv, const QString &simmat); - void CSVToMask(const QString &csv, const QString &mask); - // Write BEE files void makeMask(const QString &targetInput, const QString &queryInput, const QString &mask); void combineMasks(const QStringList &inputMasks, const QString &outputMask, const QString &method); diff --git a/sdk/core/core.cpp b/sdk/core/core.cpp index 48dc432..d26c8e8 100644 --- a/sdk/core/core.cpp +++ b/sdk/core/core.cpp @@ -305,6 +305,13 @@ void br::Compare(const File &targetGallery, const File &queryGallery, const File AlgorithmManager::getAlgorithm(output.getString("algorithm"))->compare(targetGallery, queryGallery, output); } +void br::Convert(const File &src, const File &dst) +{ + QScopedPointer before(Factory::make(src)); + QScopedPointer after(Factory::make(dst)); + after->write(before->read()); +} + QSharedPointer br::Transform::fromAlgorithm(const QString &algorithm) { return AlgorithmManager::getAlgorithm(algorithm)->transform; diff --git a/sdk/openbr.cpp b/sdk/openbr.cpp index 1dac486..b0922d8 100644 --- a/sdk/openbr.cpp +++ b/sdk/openbr.cpp @@ -51,17 +51,9 @@ void br_confusion(const char *file, float score, int *true_positives, int *false return Confusion(file, score, *true_positives, *false_positives, *true_negatives, *false_negatives); } -void br_convert(const char *input_matrix, const char *output_matrix) -{ - QString inputSuffix = QFileInfo(input_matrix).suffix(); - QString outputSuffix = QFileInfo(output_matrix).suffix(); - if (inputSuffix == "csv") { - if (outputSuffix == "mtx") BEE::CSVToSimmat(input_matrix, output_matrix); - else BEE::CSVToMask(input_matrix, output_matrix); - } else { - if (inputSuffix == "mtx") BEE::simmatToCSV(input_matrix, output_matrix); - else BEE::maskToCSV(input_matrix, output_matrix); - } +void br_convert(const char *input, const char *output) +{ + Convert(File(input), File(output)); } void br_enroll(const char *input, const char *gallery) diff --git a/sdk/openbr.h b/sdk/openbr.h index 8cc9af4..d653bd5 100644 --- a/sdk/openbr.h +++ b/sdk/openbr.h @@ -123,11 +123,9 @@ BR_EXPORT void br_confusion(const char *file, float score, int *true_positives, int *false_positives, int *true_negatives, int *false_negatives); /*! - * \brief Converts a .csv file to/from a \ref simmat or \ref mask. - * \param input_matrix The input matrix. - * \param output_matrix The output matrix. + * \brief Wraps br::Convert() */ -BR_EXPORT void br_convert(const char *input_matrix, const char *output_matrix); +BR_EXPORT void br_convert(const char *input, const char *output); /*! * \brief Constructs template(s) from an input. diff --git a/sdk/openbr_plugin.h b/sdk/openbr_plugin.h index ca49a32..b46a31e 100644 --- a/sdk/openbr_plugin.h +++ b/sdk/openbr_plugin.h @@ -1080,6 +1080,13 @@ BR_EXPORT FileList Enroll(const File &input, const File &gallery = File()); */ BR_EXPORT void Compare(const File &targetGallery, const File &queryGallery, const File &output); +/*! + * \brief To convert between matrix/template formats. + * \param input The input matrix or template. + * \param output The output matrix or template. + */ +BR_EXPORT void Convert(const File &input, const File &output); + /*! @}*/ } // namespace br diff --git a/sdk/plugins/format.cpp b/sdk/plugins/format.cpp index d3d4075..7533575 100644 --- a/sdk/plugins/format.cpp +++ b/sdk/plugins/format.cpp @@ -46,12 +46,15 @@ class csvFormat : public Format QStringList lines(QString(f.readAll()).split('\n')); f.close(); + bool isUChar = true; QList< QList > valsList; foreach (const QString &line, lines) { QList vals; foreach (const QString &word, line.split(QRegExp(" *, *"))) { bool ok; - vals.append(word.toFloat(&ok)); assert(ok); + const float val = word.toFloat(&ok); + vals.append(val); + isUChar = isUChar && (val == float(uchar(val))); } valsList.append(vals); } @@ -65,6 +68,7 @@ class csvFormat : public Format } } + if (isUChar) m.convertTo(m, CV_8U); return Template(m); } -- libgit2 0.21.4