Commit b57870af61202a09bbf22c457c2e92a11b513982
1 parent
6ff74e33
Output to gallery that can be ingested into matlab
Showing
2 changed files
with
120 additions
and
0 deletions
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 | + templates[i].m().copyTo(temp); | |
| 55 | + temp.reshape(1, 1); | |
| 56 | + m.col(i) = temp; | |
| 57 | + } | |
| 58 | + | |
| 59 | + f.write((const char *) &m.rows, 4); | |
| 60 | + f.write((const char *) &m.cols, 4); | |
| 61 | + qint64 rowSize = m.cols * sizeof(float); | |
| 62 | + for (int i = 0; i < m.rows; i++) | |
| 63 | + f.write((const char *) m.row(i).data, rowSize); | |
| 64 | + | |
| 65 | + f.close(); | |
| 66 | + } | |
| 67 | + | |
| 68 | + TemplateList readBlock(bool *done) | |
| 69 | + { | |
| 70 | + qDebug() << "matlabGallery is not intended to be read. This gallery is meant for export only."; | |
| 71 | + TemplateList t; | |
| 72 | + *done = false; | |
| 73 | + return t; | |
| 74 | + } | |
| 75 | + | |
| 76 | + void write(const Template &t) | |
| 77 | + { | |
| 78 | + if (t.m().type() != CV_32FC1) | |
| 79 | + qDebug() << t.file.name << ": template not appended to gallery b/c it must be type CV_32FC1."; | |
| 80 | + else | |
| 81 | + templates.append(t); | |
| 82 | + } | |
| 83 | + | |
| 84 | + void init() | |
| 85 | + { | |
| 86 | + FileGallery::init(); | |
| 87 | + } | |
| 88 | +}; | |
| 89 | + | |
| 90 | +BR_REGISTER(Gallery, matlabGallery) | |
| 91 | + | |
| 92 | +#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] = loadBin(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 | ... | ... |