regions.cpp
4.48 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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>
using namespace cv;
using namespace br;
/*!
* \ingroup transforms
* \brief Subdivide matrix into rectangular subregions.
* \author Josh Klontz \cite jklontz
*/
class RectRegions : public UntrainableTransform
{
Q_OBJECT
Q_PROPERTY(int width READ get_width WRITE set_width RESET reset_width STORED false)
Q_PROPERTY(int height READ get_height WRITE set_height RESET reset_height STORED false)
Q_PROPERTY(int widthStep READ get_widthStep WRITE set_widthStep RESET reset_widthStep STORED false)
Q_PROPERTY(int heightStep READ get_heightStep WRITE set_heightStep RESET reset_heightStep STORED false)
BR_PROPERTY(int, width, 8)
BR_PROPERTY(int, height, 8)
BR_PROPERTY(int, widthStep, -1)
BR_PROPERTY(int, heightStep, -1)
void project(const Template &src, Template &dst) const
{
const int widthStep = this->widthStep == -1 ? width : this->widthStep;
const int heightStep = this->heightStep == -1 ? height : this->heightStep;
const Mat &m = src;
const int xMax = m.cols - width;
const int yMax = m.rows - height;
for (int x=0; x <= xMax; x += widthStep)
for (int y=0; y <= yMax; y += heightStep)
dst += m(Rect(x, y, width, height));
}
};
BR_REGISTER(Transform, RectRegions)
/*!
* \ingroup transforms
* \brief Turns each row into its own matrix.
* \author Josh Klontz \cite jklontz
*/
class ByRow : public UntrainableTransform
{
Q_OBJECT
void project(const Template &src, Template &dst) const
{
for (int i=0; i<src.m().rows; i++)
dst += src.m().row(i);
}
};
BR_REGISTER(Transform, ByRow)
/*!
* \ingroup transforms
* \brief Concatenates all input matrices into a single floating point matrix.
* No requirements are placed on input matrices size and type.
* \author Josh Klontz \cite jklontz
*/
class CatTransform : public UntrainableMetaTransform
{
Q_OBJECT
Q_PROPERTY(int partitions READ get_partitions WRITE set_partitions RESET reset_partitions)
BR_PROPERTY(int, partitions, 1)
void project(const Template &src, Template &dst) const
{
dst.file = src.file;
if (src.size() % partitions != 0)
qFatal("Cat %d partitions does not evenly divide %d matrices.", partitions, src.size());
QVector<int> sizes(partitions, 0);
for (int i=0; i<src.size(); i++)
sizes[i%partitions] += src[i].total() * src[i].channels();
foreach (int size, sizes)
dst.append(Mat(1, size, CV_32FC1));
QVector<int> offsets(partitions, 0);
for (int i=0; i<src.size(); i++) {
size_t size = src[i].total() * src[i].elemSize();
int j = i%partitions;
memcpy(&dst[j].data[offsets[j]], src[i].ptr(), size);
offsets[j] += size;
}
}
};
BR_REGISTER(Transform, CatTransform)
/*!
* \ingroup transforms
* \brief Duplicates the template data.
* \author Josh Klontz \cite jklontz
*/
class Dup : public UntrainableMetaTransform
{
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
{
for (int i=0; i<n; i++)
dst.merge(src);
}
};
BR_REGISTER(Transform, Dup)
#include "regions.moc"