-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImgProcSet.cpp
239 lines (224 loc) · 7.1 KB
/
ImgProcSet.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "ImgProcSet.h"
#include <typeinfo>
#include <time.h>
#if !defined(_MSC_VER)
#include <stdexcept>
#endif
using namespace std;
namespace cvImagePipeline {
namespace Filter {
///////////////////////////////////////////////////////////////////////////
// class ImgProcSet
IMPLEMENT_CVFILTER(ImgProcSet);
ImgProcSet::ImgProcSet()
: lastAutoProcessor(0)
{
}
ImgProcSet::~ImgProcSet()
{
std::vector<ImageProcessor*>::iterator dyn_filter = dyn_filters.begin();
while(dyn_filter != dyn_filters.end()) {
#ifdef _DEBUG
std::cout << "delete filter "
<< typeid(**dyn_filter).name() << std::endl;
#endif
delete *dyn_filter;
*dyn_filter = 0;
dyn_filter++;
}
}
ImageProcessor& ImgProcSet::add(
std::string filter_class_name,
std::string filter_instance_name,
bool autoBind)
{
ImageProcessor* filter = ImageProcessor::createFilter(filter_class_name);
if(filter != 0) {
dyn_filters.push_back(filter);
name_to_filter[filter_instance_name] = filter;
filter->setName(getName() + "." + filter_instance_name);
add(*filter, autoBind);
} else {
std::cerr << "ImageProcessor \"" << filter_class_name << "\" is not exist in "
<< getName() << " instance of class " << std::string(typeid(*this).name()) << "."
<< std::endl;
#if defined(_MSC_VER)
throw new std::exception("PropertyNotFound");
#else
throw new std::runtime_error("PropertyNotFound");
#endif
}
return *filter;
}
ImageProcessor& ImgProcSet::add(ImageProcessor& proc, bool autoBind)
{
if(autoBind) {
std::list<ImageProcessor*>::reverse_iterator back = procs.rbegin();
if(back != procs.rend()) {
ImageProcessor* last = *back;
*last >> proc;
} else {
proc.setInputMat(getInputMat());
}
refOutputMat() = proc.getOutputMat();
lastAutoProcessor = &proc;
}
procs.push_back(&proc);
return proc;
}
void ImgProcSet::setParam(
std::string filter_instance_name,
std::string parameter_name, int value)
{
(*this)[filter_instance_name].property(parameter_name, value);
}
ImageProcessor& ImgProcSet::operator[](
const std::string& filter_instance_name)
{
ImageProcessor* filter = 0;
if(name_to_filter.count(filter_instance_name) > 0) {
filter = name_to_filter[filter_instance_name];
} else {
std::cerr << "ImageProcessor named \"" << filter_instance_name << "\" is not exist in "
<< getName() << " instance of class " << std::string(typeid(*this).name()) << "."
<< std::endl;
#if defined(_MSC_VER)
throw new std::exception("ProcessorInstanceNotFound");
#else
throw new std::runtime_error("ProcessorInstanceNotFound");
#endif
}
return *filter;
}
ImageProcessor& ImgProcSet::getLastProc() {
std::list<ImageProcessor*>::reverse_iterator last = procs.rbegin();
return *(*last);
}
void ImgProcSet::execute()
{
if (isEnable()) {
std::list<ImageProcessor*>::iterator proc = procs.begin();
while (proc != procs.end()) {
if ((*proc)->isEnable()) {
try {
(*proc)->execute();
}
catch (...) {
cerr
<< "exception caught in " << getName()
<< "from " << (*proc)->getName() << "."
<< endl;
}
}
proc++;
}
if (lastAutoProcessor != 0) {
setOutputMat(lastAutoProcessor->getOutputMat());
}
}
}
void ImgProcSet::putMarkdown(std::ostream& stream) {
ImageProcessor::putMarkdown(stream);
stream << "----" << std::endl;
stream << std::endl;
std::list<ImageProcessor*>::iterator proc = procs.begin();
for(; proc != procs.end(); proc++) {
(*proc)->putMarkdown(stream);
}
}
using namespace pugi;
/**
* create procesor pipeline from xml file.
*/
bool ImgProcSet::loadXml(const std::string& filename) {
xml_document document;
xml_parse_result result = document.load_file(filename.c_str());
if(result.status != status_ok) {
return false;
}
xml_node root_node = document.document_element();
#ifdef _DEBUG
cerr << "[ImgProcSet::loadXml]" << "root node name = " << root_node.name() << endl;
#endif
pugi::xml_attribute root_name_attr = root_node.attribute("name");
setName(root_name_attr.as_string());
xpath_node_set processor_node_set = root_node.select_nodes("Processor");
xpath_node_set::const_iterator processor = processor_node_set.begin();
while(processor != processor_node_set.end()) {
addProcessorByXmlNode(processor->node());
processor++;
}
return true;
}
void ImgProcSet::addProcessorByXmlNode(pugi::xml_node processor)
{
// Attr processor class name
xml_attribute attr_proc_class = processor.attribute("class");
// Attr instance name
xml_attribute attr_proc_name = processor.attribute("name");
#ifdef _DEBUG
cerr << "[ImgProcSet::addProcessorByXmlNode] " << "Processor "
<< attr_proc_class.name() << " = " << attr_proc_class.as_string() << ", "
<< attr_proc_name.name() << " = " << attr_proc_name.as_string() << endl;
#endif
// Attr autoBind
xml_attribute attr_bind = processor.attribute("autoBind");
string autoBindAttrName = string(attr_bind.name());
bool autoBind = true;
if(autoBindAttrName == "") {
#ifdef _DEBUG
cerr << "property autoBind not specified. default=true" << std::endl;
#endif
} else {
autoBind = attr_bind.as_bool();
}
#ifdef _DEBUG
cerr << "property autoBind = \"" << autoBind << "\"" << std::endl;
#endif
// create and add.
ImageProcessor& proc = add(
attr_proc_class.as_string(),
attr_proc_name.as_string(),
autoBind);
//
// Property
//
xpath_node_set property_node_set = processor.select_nodes("Property");
xpath_node_set::const_iterator property = property_node_set.begin();
while(property != property_node_set.end()) {
proc.setPropertyByXmlNode(property->node());
property++;
}
//
// Input
//
xpath_node_set input_node_set = processor.select_nodes("Input");
xpath_node_set::const_iterator input = input_node_set.begin();
while(input != input_node_set.end()) {
setInputMatByXmlNode(proc, input->node());
input++;
}
}
void ImgProcSet::setInputMatByXmlNode(ImageProcessor& imageProcessor, pugi::xml_node input) {
string srcProcName = string(input.attribute("from").as_string());
string dstInMatName = string(input.attribute("to").as_string());
#ifdef _DEBUG
cerr << "[ImageProcessor::setInputMatByXmlNode] " << "Input "
<< "from \"" << srcProcName << "\", to " << dstInMatName << endl;
#endif
FilterInput fi = imageProcessor.input(dstInMatName);
(*this)[srcProcName] >> fi;
//(*this)[srcProcName] >> imageProcessor.input(dstInMatName);
}
void ImageProcessor::setPropertyByXmlNode(xml_node property) {
xml_attribute attr_prop_name = property.attribute("name");
xml_attribute attr_prop_value = property.attribute("value");
#ifdef _DEBUG
cerr << "[ImageProcessor::setPropertyByXmlNode] " << "Property "
<< attr_prop_name.name() << " = " << attr_prop_name.as_string() << ", "
<< attr_prop_value.name() << " = " << attr_prop_value.as_string() << endl;
#endif
setPropertyAsString(attr_prop_name.as_string(), attr_prop_value.as_string());
}
}
}