[go: up one dir, main page]

Menu

[r743]: / mia2 / mia / 2d / transform.cc  Maximize  Restore  History

Download this file

257 lines (205 with data), 7.0 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* -*- mia-c++ -*-
*
* Copyright (c) 2007 Gert Wollny <gert dot wollny at acm dot org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <mia/2d/transform.hh>
#include <mia/2d/deformer.hh>
#include <mia/2d/2dvfio.hh>
#include <boost/lambda/lambda.hpp>
NS_MIA_BEGIN
using namespace boost::lambda;
bool C2DTransformation::save(const std::string& filename, const std::string& type) const
{
return do_save(filename, type);
}
size_t C2DTransformation::degrees_of_freedom() const
{
return do_degrees_of_freedom();
}
P2DTransformation C2DTransformation::upscale(const C2DBounds& size) const
{
return do_upscale(size);
}
void C2DTransformation::add(const C2DTransformation& a)
{
do_add(a);
}
P2DImage C2DAffineTransformation::apply(const C2DImage& input) const
{
assert(0);
return P2DImage();
}
C2DFVector C2DAffineTransformation::operator() (const C2DFVector& x) const
{
return C2DFVector(
_M_t[0] * x.x + _M_t[1] * x.y + _M_t[2],
_M_t[3] * x.x + _M_t[4] * x.y + _M_t[5]
);
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size):
_M_t(9,0.0f),
_M_ops(op_all),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size, std::vector<float> transform, C2DAffineTransformation::EOps ops):
_M_t(transform),
_M_ops(ops),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size, C2DAffineTransformation::EOps ops):
_M_t(9,0.0f),
_M_ops(ops),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
bool C2DAffineTransformation::do_save(const std::string& filename, const std::string& type) const
{
return false;
}
size_t C2DAffineTransformation::do_degrees_of_freedom() const
{
size_t dgf = 0;
if (_M_ops & op_rotate) dgf += 2;
if (_M_ops & op_trans) dgf += 2;
if (_M_ops & op_shear) dgf += 2;
if (_M_ops & op_scale) dgf += 2;
return dgf;
}
void C2DAffineTransformation::scale(float x, float y)
{
_M_t[0] *= x; _M_t[1] *= x; _M_t[2] *= x;
_M_t[3] *= y; _M_t[4] *= y; _M_t[5] *= y;
}
const C2DBounds& C2DAffineTransformation::get_size() const
{
return _M_size;
}
P2DTransformation C2DAffineTransformation::do_upscale(const C2DBounds& size) const
{
float x_mult = float(size.x) / (float)get_size().x;
float y_mult = float(size.y) / (float)get_size().y;
C2DAffineTransformation *result = new C2DAffineTransformation(size, _M_t, _M_ops);
result->scale(x_mult, y_mult);
return P2DTransformation(result);
}
void C2DAffineTransformation::do_add(const C2DTransformation& other)
{
const C2DAffineTransformation& a = dynamic_cast<const C2DAffineTransformation&>(other);
// matrix multiply
vector<float> tmp(_M_t.begin(), _M_t.end());
_M_t[0] = tmp[0] * a._M_t[0] + tmp[1] * a._M_t[3] + tmp[2] * a._M_t[6];
_M_t[1] = tmp[0] * a._M_t[1] + tmp[1] * a._M_t[4] + tmp[2] * a._M_t[7];
_M_t[2] = tmp[0] * a._M_t[2] + tmp[1] * a._M_t[5] + tmp[2] * a._M_t[8];
_M_t[3] = tmp[3] * a._M_t[0] + tmp[4] * a._M_t[3] + tmp[5] * a._M_t[6];
_M_t[4] = tmp[3] * a._M_t[1] + tmp[4] * a._M_t[4] + tmp[5] * a._M_t[7];
_M_t[5] = tmp[3] * a._M_t[2] + tmp[4] * a._M_t[5] + tmp[5] * a._M_t[8];
_M_t[6] = tmp[6] * a._M_t[0] + tmp[7] * a._M_t[3] + tmp[8] * a._M_t[6];
_M_t[7] = tmp[6] * a._M_t[1] + tmp[7] * a._M_t[4] + tmp[8] * a._M_t[7];
_M_t[8] = tmp[6] * a._M_t[2] + tmp[7] * a._M_t[5] + tmp[8] * a._M_t[8];
}
/*
C2DGridTransformation& C2DGridTransformation::operator += (C2DGridTransformation& other)
{
TRACE("C2DGridTransformation::operator +=");
_M_field += other.field();
return *this;
}
C2DGridTransformation C2DGridTransformation::operator + (const C2DGridTransformation& other)
{
TRACE("C2DGridTransformation::operator +");
assert(_M_field.get_size() == other._M_field.get_size());
C2DGridTransformation tmp(_M_field.get_size(), _M_ipf);
C2DFVectorfield::iterator itmp = tmp._M_field.begin();
C2DFVectorfield::const_iterator ib = other._M_field.begin();
for (size_t y = 0; y < _M_field.get_size().y; ++y) {
for (size_t x = 0; x < _M_field.get_size().x; ++x, ++itmp, ++ib) {
C2DFVector xi = C2DFVector(x, y) - *ib;
*itmp = _M_field.get_interpol_val_at(xi) + *ib;
}
}
return tmp;
}
*/
C2DGridTransformation::C2DGridTransformation(const C2DBounds& size, P2DInterpolatorFactory ipf):
_M_field(size),
_M_ipf(ipf)
{
}
C2DFVectorfield& C2DGridTransformation::field()
{
return _M_field;
}
const C2DFVectorfield& C2DGridTransformation::field() const
{
return _M_field;
}
P2DInterpolatorFactory C2DGridTransformation::get_ipf()
{
return _M_ipf;
}
P2DImage C2DGridTransformation::apply(const C2DImage& input) const
{
TRACE("C2DGridTransformation::apply");
FDeformer2D deformer(_M_field, *_M_ipf);
return mia::filter(deformer, input);
}
P2DTransformation C2DGridTransformation::do_upscale(const C2DBounds& size) const
{
TRACE("C2DGridTransformation::upscale");
C2DGridTransformation *result = new C2DGridTransformation(size, _M_ipf);
// initial upscale
if (_M_field.get_size().x != 0 && _M_field.get_size().y != 0) {
float x_mult = float(size.x) / (float)_M_field.get_size().x;
float y_mult = float(size.y) / (float)_M_field.get_size().y;
float ix_mult = 1.0f / x_mult;
float iy_mult = 1.0f / y_mult;
C2DFVectorfield::iterator i = result->_M_field.begin();
for (unsigned int y = 0; y < size.y; y++){
for (unsigned int x = 0; x < size.x; x++,++i){
C2DFVector help(ix_mult * x, iy_mult * y);
C2DFVector val = _M_field.get_interpol_val_at(help);
*i = C2DFVector(val.x * x_mult,val.y * y_mult);
}
}
}
return P2DTransformation(result);
}
const C2DBounds& C2DGridTransformation::get_size() const
{
return _M_field.get_size();
}
bool C2DGridTransformation::do_save(const std::string& filename, const std::string& type) const
{
C2DIOVectorfield outfield(_M_field);
return C2DVFIOPluginHandler::instance().save(type, filename, outfield);
}
void C2DGridTransformation::do_add(const C2DTransformation& a)
{
const C2DGridTransformation& other = dynamic_cast<const C2DGridTransformation&>(a);
_M_field += other.field();
}
size_t C2DGridTransformation::do_degrees_of_freedom() const
{
return 3 * _M_field.size();
}
NS_MIA_END