From b57870af61202a09bbf22c457c2e92a11b513982 Mon Sep 17 00:00:00 2001 From: bklare Date: Mon, 15 Jun 2015 21:08:41 +0000 Subject: [PATCH] Output to gallery that can be ingested into matlab --- openbr/plugins/gallery/matlab.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/matlab/loadOpenBR.m | 28 ++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 0 deletions(-) create mode 100644 openbr/plugins/gallery/matlab.cpp create mode 100644 scripts/matlab/loadOpenBR.m diff --git a/openbr/plugins/gallery/matlab.cpp b/openbr/plugins/gallery/matlab.cpp new file mode 100644 index 0000000..ba9e296 --- /dev/null +++ b/openbr/plugins/gallery/matlab.cpp @@ -0,0 +1,92 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright 2015 Rank One Corporation * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +namespace br +{ +/*! + * \ingroup galleries + * \brief A simple matrix container format that is easily read into matlab. + * Templates are concatenated into column vectors, and output into a single matrix. + * Note that this is not intended to read in .matlab files, as this is simply + * a quick and dirty for analyzing data in a more interactive environment. + * Use the loadOpenBR.m script to ingest the resultant file into Matlab + * \author Brendan Klare \cite bklare + */ +class matlabGallery : public FileGallery +{ + Q_OBJECT + + TemplateList templates; + + ~matlabGallery() + { + if (!f.open(QFile::WriteOnly)) + qFatal("Failed to open %s for writting.", qPrintable(file)); + + int r = templates.first().m().rows; + int c = templates.first().m().cols; + + for (int i = templates.size() - 1; i >= 0; i--) { + if (templates[i].m().rows != r || templates[i].m().cols != c) { + qDebug() << templates[i].file.name << " : template not appended to gallery b/c rows and col dimensions differ."; + templates.removeAt(i); + } + } + + cv::Mat m(r * c, templates.size(), CV_32FC1); + for (int i = 0; i < templates.size(); i++) { + cv::Mat temp; + templates[i].m().copyTo(temp); + temp.reshape(1, 1); + m.col(i) = temp; + } + + f.write((const char *) &m.rows, 4); + f.write((const char *) &m.cols, 4); + qint64 rowSize = m.cols * sizeof(float); + for (int i = 0; i < m.rows; i++) + f.write((const char *) m.row(i).data, rowSize); + + f.close(); + } + + TemplateList readBlock(bool *done) + { + qDebug() << "matlabGallery is not intended to be read. This gallery is meant for export only."; + TemplateList t; + *done = false; + return t; + } + + void write(const Template &t) + { + if (t.m().type() != CV_32FC1) + qDebug() << t.file.name << ": template not appended to gallery b/c it must be type CV_32FC1."; + else + templates.append(t); + } + + void init() + { + FileGallery::init(); + } +}; + +BR_REGISTER(Gallery, matlabGallery) + +#include "gallery/matlab.moc" diff --git a/scripts/matlab/loadOpenBR.m b/scripts/matlab/loadOpenBR.m new file mode 100644 index 0000000..45ffbe2 --- /dev/null +++ b/scripts/matlab/loadOpenBR.m @@ -0,0 +1,28 @@ +% This script will read a matrix container format where the first two entires are 32-bit ints +% specifying the number of rows and columns (respectively). The remaining buffer is 32-bit floats +% with all the matrix contents, in row-major order. +function [x] = loadBin(filename,reverse) + + if nargin < 2, + % Use this option if the data was serlialized in column-major order. + reverse = false; + end + + fileStream = fopen(filename,'r'); + if fileStream == -1, + fprintf('Error opening file %s\n',filename); + x = 0; + return + end + + r = fread(fileStream,1,'int32'); + c = fread(fileStream,1,'int32'); + x = fread(fileStream,[c r],'float32'); + x = x'; % Matlab reads the file in column-major order + fclose(fileStream); + + if reverse, + sz = size(x); + x = x'; + x = reshape(x,sz); + end -- libgit2 0.21.4