forked from hbristow/cvmatio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
100 lines (88 loc) · 3.36 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2012, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* File: main.cpp
* Author: Hilton Bristow
* Created: Jun 27, 2012
*/
/*! \mainpage Matlab Mat File Parser for C++ OpenCV
*
* MatlabIO provides the ability to read .Mat files generated by Matlab
* into C++ programs. MatlabIO stores matrices in the OpenCV cv::Mat format
* and is intrinsically designed for computer vision research and applications,
* though other fields may find use in the features it provides. It has a simple,
* easy to use interface for parsing files, searching for particular variables
* and extracting the data in OpenCV native formats.
*/
#include <string>
#include <cassert>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "MatlabIO.hpp"
#include "MatlabIOContainer.hpp"
using namespace cv;
using namespace std;
int main(int argc, char **argv) {
assert(argc > 1);
// get the Matlab .Mat file from the command line
string filename(argv[1]);
namedWindow("image");
// create a new reader
MatlabIO matio;
bool ok = matio.open(filename, "r");
if (!ok) return -1;
// read all of the variables in the file
vector<MatlabIOContainer> variables;
variables = matio.read();
// close the file
matio.close();
// display the file info
matio.whos(variables);
waitKey();
// search for a variable named "im" and "gray"
Mat gray;
Mat im;
for (unsigned int n = 0; n < variables.size(); ++n) {
if (variables[n].name().compare("im") == 0) {
im = variables[n].data<Mat>();
cvtColor(im, im, CV_RGB2BGR);
}
if (variables[n].name().compare("gray") == 0) {
gray = variables[n].data<Mat>();
}
}
imshow("image", im);
waitKey(0);
return 0;
}