Commit 992fb540c5847dac33f662319bde65fdebc45f1c

Authored by Brendan K
2 parents 6ff74e33 04361b90

Merge pull request #385 from biometrics/matlabGallery1

Output to gallery that can be ingested into matlab
openbr/plugins/gallery/matlab.cpp 0 → 100644
  1 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2 + * Copyright 2015 Rank One Corporation *
  3 + * *
  4 + * Licensed under the Apache License, Version 2.0 (the "License"); *
  5 + * you may not use this file except in compliance with the License. *
  6 + * You may obtain a copy of the License at *
  7 + * *
  8 + * http://www.apache.org/licenses/LICENSE-2.0 *
  9 + * *
  10 + * Unless required by applicable law or agreed to in writing, software *
  11 + * distributed under the License is distributed on an "AS IS" BASIS, *
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13 + * See the License for the specific language governing permissions and *
  14 + * limitations under the License. *
  15 + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  16 +
  17 +#include <openbr/plugins/openbr_internal.h>
  18 +
  19 +namespace br
  20 +{
  21 +/*!
  22 + * \ingroup galleries
  23 + * \brief A simple matrix container format that is easily read into matlab.
  24 + * Templates are concatenated into column vectors, and output into a single matrix.
  25 + * Note that this is not intended to read in .matlab files, as this is simply
  26 + * a quick and dirty for analyzing data in a more interactive environment.
  27 + * Use the loadOpenBR.m script to ingest the resultant file into Matlab
  28 + * \author Brendan Klare \cite bklare
  29 + */
  30 +class matlabGallery : public FileGallery
  31 +{
  32 + Q_OBJECT
  33 +
  34 + TemplateList templates;
  35 +
  36 + ~matlabGallery()
  37 + {
  38 + if (!f.open(QFile::WriteOnly))
  39 + qFatal("Failed to open %s for writting.", qPrintable(file));
  40 +
  41 + int r = templates.first().m().rows;
  42 + int c = templates.first().m().cols;
  43 +
  44 + for (int i = templates.size() - 1; i >= 0; i--) {
  45 + if (templates[i].m().rows != r || templates[i].m().cols != c) {
  46 + qDebug() << templates[i].file.name << " : template not appended to gallery b/c rows and col dimensions differ.";
  47 + templates.removeAt(i);
  48 + }
  49 + }
  50 +
  51 + cv::Mat m(r * c, templates.size(), CV_32FC1);
  52 + for (int i = 0; i < templates.size(); i++) {
  53 + cv::Mat temp;
  54 + temp = templates[i].m().reshape(1, r * c);
  55 + temp.copyTo(m.col(i));
  56 + }
  57 +
  58 + f.write((const char *) &m.rows, 4);
  59 + f.write((const char *) &m.cols, 4);
  60 + qint64 rowSize = m.cols * sizeof(float);
  61 + for (int i = 0; i < m.rows; i++)
  62 + f.write((const char *) m.row(i).data, rowSize);
  63 +
  64 + f.close();
  65 + }
  66 +
  67 + TemplateList readBlock(bool *done)
  68 + {
  69 + qDebug() << "matlabGallery is not intended to be read. This gallery is meant for export only.";
  70 + TemplateList t;
  71 + *done = false;
  72 + return t;
  73 + }
  74 +
  75 + void write(const Template &t)
  76 + {
  77 + if (t.m().type() != CV_32FC1)
  78 + qDebug() << t.file.name << ": template not appended to gallery b/c it must be type CV_32FC1.";
  79 + else
  80 + templates.append(t);
  81 + }
  82 +
  83 + void init()
  84 + {
  85 + FileGallery::init();
  86 + }
  87 +};
  88 +
  89 +BR_REGISTER(Gallery, matlabGallery)
  90 +
  91 +}
  92 +
  93 +#include "gallery/matlab.moc"
... ...
scripts/matlab/loadOpenBR.m 0 → 100644
  1 +% This script will read a matrix container format where the first two entires are 32-bit ints
  2 +% specifying the number of rows and columns (respectively). The remaining buffer is 32-bit floats
  3 +% with all the matrix contents, in row-major order.
  4 +function [x] = loadOpenBR(filename,reverse)
  5 +
  6 + if nargin < 2,
  7 + % Use this option if the data was serlialized in column-major order.
  8 + reverse = false;
  9 + end
  10 +
  11 + fileStream = fopen(filename,'r');
  12 + if fileStream == -1,
  13 + fprintf('Error opening file %s\n',filename);
  14 + x = 0;
  15 + return
  16 + end
  17 +
  18 + r = fread(fileStream,1,'int32');
  19 + c = fread(fileStream,1,'int32');
  20 + x = fread(fileStream,[c r],'float32');
  21 + x = x'; % Matlab reads the file in column-major order
  22 + fclose(fileStream);
  23 +
  24 + if reverse,
  25 + sz = size(x);
  26 + x = x';
  27 + x = reshape(x,sz);
  28 + end
... ...