cvt.cpp
4.56 KB
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2012 The MITRE 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 <opencv2/imgproc/imgproc.hpp>
#include <openbr_plugin.h>
#include "core/opencvutils.h"
using namespace cv;
namespace br
{
/*!
* \ingroup transforms
* \brief Colorspace conversion
* \author Josh Klontz \cite jklontz
*/
class CvtTransform : public UntrainableTransform
{
Q_OBJECT
Q_ENUMS(Code)
Q_PROPERTY(Code code READ get_code WRITE set_code RESET reset_code STORED false)
Q_PROPERTY(int channel READ get_channel WRITE set_channel RESET reset_channel STORED false)
public:
/*!< */
enum Code { Gray = CV_BGR2GRAY,
RGBGray = CV_RGB2GRAY,
HLS = CV_BGR2HLS,
HSV = CV_BGR2HSV,
Lab = CV_BGR2Lab,
Luv = CV_BGR2Luv,
RGB = CV_BGR2RGB,
XYZ = CV_BGR2XYZ,
YCrCb = CV_BGR2YCrCb };
private:
BR_PROPERTY(Code, code, Gray)
BR_PROPERTY(int, channel, -1)
void project(const Template &src, Template &dst) const
{
if (src.m().channels() > 1) cvtColor(src, dst, code);
else dst = src;
if (channel != -1) {
std::vector<Mat> mv;
split(dst, mv);
dst = mv[channel % (int)mv.size()];
}
}
};
BR_REGISTER(Transform, CvtTransform)
/*!
* \ingroup transforms
* \brief Convert to floating point format.
* \author Josh Klontz \cite jklontz
*/
class CvtFloatTransform : public UntrainableTransform
{
Q_OBJECT
void project(const Template &src, Template &dst) const
{
src.m().convertTo(dst, CV_32F);
}
};
BR_REGISTER(Transform, CvtFloatTransform)
/*!
* \ingroup transforms
* \brief Convert to uchar format
* \author Josh Klontz \cite jklontz
*/
class CvtUCharTransform : public UntrainableTransform
{
Q_OBJECT
void project(const Template &src, Template &dst) const
{
OpenCVUtils::cvtUChar(src, dst);
}
};
BR_REGISTER(Transform, CvtUCharTransform)
/*!
* \ingroup transforms
* \brief Split a multi-channel matrix into several single-channel matrices.
* \author Josh Klontz \cite jklontz
*/
class SplitChannelsTransform : public UntrainableTransform
{
Q_OBJECT
void project(const Template &src, Template &dst) const
{
std::vector<Mat> mv;
split(src, mv);
foreach (const Mat &m, mv)
dst += m;
}
};
BR_REGISTER(Transform, SplitChannelsTransform)
/*!
* \ingroup transforms
* \brief Enforce the matrix has a certain number of channels by adding or removing channels.
* \author Josh Klontz \cite jklontz
*/
class EnsureChannelsTransform : public UntrainableTransform
{
Q_OBJECT
Q_PROPERTY(int n READ get_n WRITE set_n RESET reset_n STORED false)
BR_PROPERTY(int, n, 1)
void project(const Template &src, Template &dst) const
{
if (src.m().channels() == n) {
dst = src;
} else {
std::vector<Mat> mv;
split(src, mv);
// Add extra channels
while ((int)mv.size() < n) {
for (int i=0; i<src.m().channels(); i++) {
mv.push_back(mv[i]);
if ((int)mv.size() == n)
break;
}
}
// Remove extra channels
while ((int)mv.size() > n)
mv.pop_back();
merge(mv, dst);
}
}
};
BR_REGISTER(Transform, EnsureChannelsTransform)
} // namespace br
#include "cvt.moc"