Commit c3e880e6c15be09cd4dae8f067aa3cdbb2d02f06

Authored by Charles Otto
1 parent 0d62d619

Refactor BEE::readMask and readSimmat to a common readMat function

Since .mtx files include a matrix type in their header, we don't really need
separate functions to read similarity matrices and masks.

This drops special case support for creating identity matrices via readSimmat
and readMask (since in that case, there is no actual header to read to get a
matrix type).
openbr/core/bee.cpp
@@ -135,43 +135,8 @@ void BEE::writeSigset(const QString &sigset, const br::FileList &files, bool ign @@ -135,43 +135,8 @@ void BEE::writeSigset(const QString &sigset, const br::FileList &files, bool ign
135 QtUtils::writeFile(sigset, lines); 135 QtUtils::writeFile(sigset, lines);
136 } 136 }
137 137
138 -template <typename T>  
139 -Mat readMatrix(const br::File &matrix, QString *targetSigset = NULL, QString *querySigset = NULL) 138 +Mat BEE::readMat(const br::File &matrix, QString *targetSigset, QString *querySigset)
140 { 139 {
141 - // Special case matrix construction  
142 - if (matrix == "Identity") {  
143 - int rows = matrix.get<int>("rows", -1);  
144 - int columns = matrix.get<int>("columns", -1);  
145 - const int size = matrix.get<int>("size", -1);  
146 - if (size != -1) {  
147 - if (rows == -1) rows = size;  
148 - if (columns == -1) columns = size;  
149 - }  
150 - const int step = matrix.get<int>("step", 1);  
151 - if (rows % step != 0) qFatal("Step does not divide rows evenly.");  
152 - if (columns % step != 0) qFatal("Step does not divide columns evenly.");  
153 -  
154 - if (sizeof(T) == sizeof(BEE::Mask_t)) {  
155 - const bool selfSimilar = matrix.get<bool>("selfSimilar", false);  
156 -  
157 - Mat m(rows, columns, CV_8UC1);  
158 - m.setTo(BEE::NonMatch);  
159 - for (int i=0; i<std::min(rows, columns); i+=step)  
160 - for (int j=0; j<step; j++)  
161 - for (int k=0; k<step; k++)  
162 - m.at<BEE::Mask_t>(i+j,i+k) = ((selfSimilar && (j == k)) ? BEE::DontCare : BEE::Match);  
163 - return m;  
164 - } else if (sizeof(T) == sizeof(BEE::Simmat_t)) {  
165 - Mat m(rows, columns, CV_32FC1);  
166 - m.setTo(0);  
167 - for (int i=0; i<std::min(rows, columns); i+=step)  
168 - for (int j=0; j<step; j++)  
169 - for (int k=0; k<step; k++)  
170 - m.at<BEE::Simmat_t>(i+j,i+k) = 1;  
171 - return m;  
172 - }  
173 - }  
174 -  
175 QFile file(matrix); 140 QFile file(matrix);
176 bool success = file.open(QFile::ReadOnly); 141 bool success = file.open(QFile::ReadOnly);
177 if (!success) qFatal("Unable to open %s for reading.", qPrintable(matrix.name)); 142 if (!success) qFatal("Unable to open %s for reading.", qPrintable(matrix.name));
@@ -181,6 +146,7 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu @@ -181,6 +146,7 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu
181 bool isDistance = (format[0] == 'D'); 146 bool isDistance = (format[0] == 'D');
182 if (format[1] != '2') qFatal("Invalid matrix header."); 147 if (format[1] != '2') qFatal("Invalid matrix header.");
183 148
  149 +
184 // Read sigsets 150 // Read sigsets
185 if (targetSigset != NULL) *targetSigset = file.readLine().simplified(); 151 if (targetSigset != NULL) *targetSigset = file.readLine().simplified();
186 else file.readLine(); 152 else file.readLine();
@@ -192,9 +158,17 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu @@ -192,9 +158,17 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu
192 int rows = words[1].toInt(); 158 int rows = words[1].toInt();
193 int cols = words[2].toInt(); 159 int cols = words[2].toInt();
194 160
  161 + bool isMask = words[0][1] == 'B';
  162 + int typeSize = isMask ? sizeof(BEE::Mask_t) : sizeof(BEE::Simmat_t);
  163 +
195 // Get matrix data 164 // Get matrix data
196 - qint64 bytesExpected = (qint64)rows*(qint64)cols*(qint64)sizeof(T);  
197 - Mat m(rows, cols, OpenCVType<T,1>::make()); 165 + qint64 bytesExpected = (qint64)rows*(qint64)cols*(qint64)typeSize;
  166 + Mat m;
  167 + if (isMask)
  168 + m.create(rows, cols, OpenCVType<BEE::Mask_t,1>::make());
  169 + else
  170 + m.create(rows, cols, OpenCVType<BEE::Simmat_t,1>::make());
  171 +
198 qint64 read = file.read((char*)m.data, bytesExpected); 172 qint64 read = file.read((char*)m.data, bytesExpected);
199 if (read != bytesExpected) 173 if (read != bytesExpected)
200 qFatal("Invalid matrix size."); 174 qFatal("Invalid matrix size.");
@@ -206,16 +180,6 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu @@ -206,16 +180,6 @@ Mat readMatrix(const br::File &amp;matrix, QString *targetSigset = NULL, QString *qu
206 return result; 180 return result;
207 } 181 }
208 182
209 -Mat BEE::readSimmat(const br::File &simmat, QString *targetSigset, QString *querySigset)  
210 -{  
211 - return readMatrix<Simmat_t>(simmat, targetSigset, querySigset);  
212 -}  
213 -  
214 -Mat BEE::readMask(const br::File &mask)  
215 -{  
216 - return readMatrix<Mask_t>(mask);  
217 -}  
218 -  
219 template <typename T> 183 template <typename T>
220 void writeMatrix(const Mat &m, const QString &matrix, const QString &targetSigset, const QString &querySigset) 184 void writeMatrix(const Mat &m, const QString &matrix, const QString &targetSigset, const QString &querySigset)
221 { 185 {
@@ -264,15 +228,15 @@ void BEE::writeMask(const Mat &amp;m, const QString &amp;mask, const QString &amp;targetSigs @@ -264,15 +228,15 @@ void BEE::writeMask(const Mat &amp;m, const QString &amp;mask, const QString &amp;targetSigs
264 void BEE::readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset) 228 void BEE::readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset)
265 { 229 {
266 qDebug("Reading %s header.", qPrintable(matrix)); 230 qDebug("Reading %s header.", qPrintable(matrix));
267 - if (matrix.endsWith("mask")) readMatrix< Mask_t>(matrix, targetSigset, querySigset);  
268 - else readMatrix<Simmat_t>(matrix, targetSigset, querySigset); 231 + readMat(matrix, targetSigset, querySigset);
269 } 232 }
270 233
271 void BEE::writeMatrixHeader(const QString &matrix, const QString &targetSigset, const QString &querySigset) 234 void BEE::writeMatrixHeader(const QString &matrix, const QString &targetSigset, const QString &querySigset)
272 { 235 {
273 qDebug("Writing %s header to %s %s.", qPrintable(matrix), qPrintable(targetSigset), qPrintable(querySigset)); 236 qDebug("Writing %s header to %s %s.", qPrintable(matrix), qPrintable(targetSigset), qPrintable(querySigset));
274 - if (matrix.endsWith("mask")) writeMatrix< Mask_t>(readMatrix< Mask_t>(matrix), matrix, targetSigset, querySigset);  
275 - else writeMatrix<Simmat_t>(readMatrix<Simmat_t>(matrix), matrix, targetSigset, querySigset); 237 +
  238 + if (matrix.endsWith("mask")) writeMatrix< Mask_t>(readMat(matrix), matrix, targetSigset, querySigset);
  239 + else writeMatrix<Simmat_t>(readMat(matrix), matrix, targetSigset, querySigset);
276 } 240 }
277 241
278 void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask) 242 void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask)
@@ -387,7 +351,7 @@ void BEE::combineMasks(const QStringList &amp;inputMasks, const QString &amp;outputMask, @@ -387,7 +351,7 @@ void BEE::combineMasks(const QStringList &amp;inputMasks, const QString &amp;outputMask,
387 351
388 QList<Mat> masks; 352 QList<Mat> masks;
389 foreach (const QString &inputMask, inputMasks) 353 foreach (const QString &inputMask, inputMasks)
390 - masks.append(readMask(inputMask)); 354 + masks.append(readMat(inputMask));
391 if (masks.size() < 2) qFatal("Expected at least two masks."); 355 if (masks.size() < 2) qFatal("Expected at least two masks.");
392 356
393 const int rows = masks.first().rows; 357 const int rows = masks.first().rows;
openbr/core/bee.h
@@ -39,8 +39,7 @@ namespace BEE @@ -39,8 +39,7 @@ namespace BEE
39 void writeSigset(const QString &sigset, const br::FileList &files, bool ignoreMetadata = false); 39 void writeSigset(const QString &sigset, const br::FileList &files, bool ignoreMetadata = false);
40 40
41 // Matrix 41 // Matrix
42 - cv::Mat readSimmat(const br::File &simmat, QString *targetSigset = NULL, QString *querySigset = NULL);  
43 - cv::Mat readMask(const br::File &mask); 42 + cv::Mat readMat(const br::File & mat, QString * targetSigset = NULL, QString * querySigset = NULL);
44 void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); 43 void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query");
45 void writeMask(const cv::Mat &m, const QString &mask, 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");
46 void readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset); 45 void readMatrixHeader(const QString &matrix, QString *targetSigset, QString *querySigset);
openbr/core/cluster.cpp
@@ -99,7 +99,7 @@ Neighborhood getNeighborhood(const QStringList &amp;simmats) @@ -99,7 +99,7 @@ Neighborhood getNeighborhood(const QStringList &amp;simmats)
99 int currentRows = -1; 99 int currentRows = -1;
100 int columnOffset = 0; 100 int columnOffset = 0;
101 for (int j=0; j<numGalleries; j++) { 101 for (int j=0; j<numGalleries; j++) {
102 - cv::Mat m = BEE::readSimmat(simmats[i*numGalleries+j]); 102 + cv::Mat m = BEE::readMat(simmats[i*numGalleries+j]);
103 if (j==0) { 103 if (j==0) {
104 currentRows = m.rows; 104 currentRows = m.rows;
105 allNeighbors.resize(currentRows); 105 allNeighbors.resize(currentRows);
openbr/core/core.cpp
@@ -443,7 +443,7 @@ void br::Convert(const File &amp;fileType, const File &amp;inputFile, const File &amp;output @@ -443,7 +443,7 @@ void br::Convert(const File &amp;fileType, const File &amp;inputFile, const File &amp;output
443 while (!done) after->writeBlock(before->readBlock(&done)); 443 while (!done) after->writeBlock(before->readBlock(&done));
444 } else if (fileType == "Output") { 444 } else if (fileType == "Output") {
445 QString target, query; 445 QString target, query;
446 - cv::Mat m = BEE::readSimmat(inputFile, &target, &query); 446 + cv::Mat m = BEE::readMat(inputFile, &target, &query);
447 const FileList targetFiles = TemplateList::fromGallery(target).files(); 447 const FileList targetFiles = TemplateList::fromGallery(target).files();
448 const FileList queryFiles = TemplateList::fromGallery(query).files(); 448 const FileList queryFiles = TemplateList::fromGallery(query).files();
449 449
openbr/core/eval.cpp
@@ -81,7 +81,7 @@ float Evaluate(const QString &amp;simmat, const QString &amp;mask, const QString &amp;csv) @@ -81,7 +81,7 @@ float Evaluate(const QString &amp;simmat, const QString &amp;mask, const QString &amp;csv)
81 QString target, query; 81 QString target, query;
82 Mat scores; 82 Mat scores;
83 if (simmat.endsWith(".mtx")) { 83 if (simmat.endsWith(".mtx")) {
84 - scores = BEE::readSimmat(simmat, &target, &query); 84 + scores = BEE::readMat(simmat, &target, &query);
85 } else { 85 } else {
86 QScopedPointer<Format> format(Factory<Format>::make(simmat)); 86 QScopedPointer<Format> format(Factory<Format>::make(simmat));
87 scores = format->read(); 87 scores = format->read();
@@ -99,12 +99,8 @@ float Evaluate(const QString &amp;simmat, const QString &amp;mask, const QString &amp;csv) @@ -99,12 +99,8 @@ float Evaluate(const QString &amp;simmat, const QString &amp;mask, const QString &amp;csv)
99 File maskFile(mask); 99 File maskFile(mask);
100 maskFile.set("rows", scores.rows); 100 maskFile.set("rows", scores.rows);
101 maskFile.set("columns", scores.cols); 101 maskFile.set("columns", scores.cols);
102 - if (mask.endsWith(".mtx"))  
103 - truth = BEE::readMask(mask);  
104 - else {  
105 - QScopedPointer<Format> format(Factory<Format>::make(maskFile));  
106 - truth = format->read();  
107 - } 102 + QScopedPointer<Format> format(Factory<Format>::make(maskFile));
  103 + truth = format->read();
108 } 104 }
109 105
110 return Evaluate(scores, truth, csv); 106 return Evaluate(scores, truth, csv);
openbr/core/fuse.cpp
@@ -82,7 +82,7 @@ void br::Fuse(const QStringList &amp;inputSimmats, const QString &amp;normalization, con @@ -82,7 +82,7 @@ void br::Fuse(const QStringList &amp;inputSimmats, const QString &amp;normalization, con
82 QString target, query, previousTarget, previousQuery; 82 QString target, query, previousTarget, previousQuery;
83 QList<Mat> originalMatrices; 83 QList<Mat> originalMatrices;
84 foreach (const QString &simmat, inputSimmats) { 84 foreach (const QString &simmat, inputSimmats) {
85 - originalMatrices.append(BEE::readSimmat(simmat,&target,&query)); 85 + originalMatrices.append(BEE::readMat(simmat,&target,&query));
86 // Make we're fusing score matrices for the same set of targets and querys 86 // Make we're fusing score matrices for the same set of targets and querys
87 if (!previousTarget.isEmpty() && !previousQuery.isEmpty() && (previousTarget != target || previousQuery != query)) 87 if (!previousTarget.isEmpty() && !previousQuery.isEmpty() && (previousTarget != target || previousQuery != query))
88 qFatal("Target or query files are not the same across fused matrices."); 88 qFatal("Target or query files are not the same across fused matrices.");
openbr/plugins/format.cpp
@@ -280,7 +280,7 @@ class mtxFormat : public Format @@ -280,7 +280,7 @@ class mtxFormat : public Format
280 280
281 Template read() const 281 Template read() const
282 { 282 {
283 - return BEE::readSimmat(file); 283 + return BEE::readMat(file);
284 } 284 }
285 285
286 void write(const Template &t) const 286 void write(const Template &t) const
@@ -302,7 +302,7 @@ class maskFormat : public Format @@ -302,7 +302,7 @@ class maskFormat : public Format
302 302
303 Template read() const 303 Template read() const
304 { 304 {
305 - return BEE::readMask(file); 305 + return BEE::readMat(file);
306 } 306 }
307 307
308 void write(const Template &t) const 308 void write(const Template &t) const