[go: up one dir, main page]

Menu

[r994]: / libmona / src / remap.cc  Maximize  Restore  History

Download this file

316 lines (251 with data), 8.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* 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 <libmona/monaIOStream.hh>
#include <libmona/monaException.hh>
#include <libmona/2DImageWrap.hh>
#include <map>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/type_traits.hpp>
using namespace mona;
using namespace std;
typedef pair<int, int> CClassRange;
typedef map<double, CClassRange> CClassMap;
class CMapImage {
public:
typedef C2DImageWrap result_type;
CMapImage(const vector<int>& map, int nclasses);
template <typename Image2D>
CMapImage::result_type operator ()(const Image2D& image)const;
private:
const vector<int>& _M_map;
int _M_nclasses;
};
class CSMapImage {
public:
typedef C2DImageList result_type;
CSMapImage(const vector<int>& map, int nclasses);
template <typename Image2D>
CSMapImage::result_type operator ()(const Image2D& image)const;
private:
const vector<int>& _M_map;
int _M_nclasses;
};
template <typename Image2D>
C2DImageWrap map_to_bit_image(const Image2D& image, const vector<int>& map)
{
typename Image2D::const_iterator ii = image.begin();
typename Image2D::const_iterator ie = image.end();
C2DBitImage *result = new C2DBitImage(image.get_size());
C2DBitImage::iterator t = result->begin();
while (ii != ie)
*t++ = map[(int)*ii++] == 1 ? true : false;
return C2DImageWrap(result);
}
template <typename OutImage, typename Image2D>
C2DImageWrap map_classes(const Image2D& image, const vector<int>& map)
{
typename Image2D::const_iterator ii = image.begin();
typename Image2D::const_iterator ie = image.end();
OutImage *result = new OutImage(image.get_size());
typename OutImage::iterator t = result->begin();
while (ii != ie)
*t++ = map[(int)*ii++];
return C2DImageWrap(result);
}
CMapImage::CMapImage(const vector<int>& map, int nclasses):
_M_map(map),
_M_nclasses(nclasses)
{
}
template <typename Image2D>
CMapImage::result_type CMapImage::operator ()(const Image2D& image) const
{
if (numeric_limits<typename Image2D::value_type>::max() > (typename Image2D::value_type)_M_map.size()) {
stringstream errmsg;
errmsg << "input image range larger then map span: " << (int)numeric_limits<typename Image2D::value_type>::max()
<<" vs." << _M_map.size();
throw mona_runtime_error(errmsg.str());
}
if (_M_nclasses < 3)
return map_to_bit_image(image, _M_map);
else if (_M_nclasses < 256)
return map_classes<C2DUBImage>(image, _M_map);
else
return map_classes<C2DUSImage>(image, _M_map);
}
CSMapImage::CSMapImage(const vector<int>& map, int nclasses):
_M_map(map),
_M_nclasses(nclasses)
{
}
template <typename Image2D, bool integral>
struct __dispatcher {
static CSMapImage::result_type apply(const Image2D& image, int nclasses, const vector<int>& map)
{
throw invalid_argument("unsupported input image type");
return CSMapImage::result_type();
}
};
template <typename Image2D>
struct __dispatcher<Image2D, true> {
static CSMapImage::result_type apply(const Image2D& image, int nclasses, const vector<int>& map)
{
vector<C2DBitImage*> r(nclasses);
vector<C2DBitImage::iterator> ov(nclasses);
for (int i = 0; i < nclasses; ++i) {
r[i] = new C2DBitImage(image.get_size());
ov[i] = r[i]->begin();
}
typename Image2D::const_iterator ii = image.begin();
typename Image2D::const_iterator ie = image.end();
while (ii != ie) {
*ov[map[*ii++]] = true;
for (int i = 0; i < nclasses; ++i)
++ov[i];
}
C2DImageList result;
for (int i = 0; i < nclasses; ++i)
result.push_back(C2DImageWrap(r[i]));
return result;
}
};
template <typename Image2D>
CSMapImage::result_type CSMapImage::operator ()(const Image2D& image)const
{
const bool is_integral = ::boost::is_integral<typename Image2D::value_type>::value;
return __dispatcher<Image2D, is_integral>::apply(image, _M_nclasses, _M_map);
}
CClassMap read_map(const string& filename)
{
ifstream inp(filename.c_str());
if (!inp.good()) {
stringstream errmsg;
errmsg << "unable to open " << filename << " for reading";
throw mona_fatal_error(errmsg.str());
}
int nclasses;
inp >> nclasses;
CClassMap result;
while (inp.good() && nclasses--) {
CClassRange range;
double center;
inp >> center >> range.first >> range.second;
result[center] = range;
}
if (nclasses>0) {
stringstream errmsg;
errmsg << "Could not read all classes from " << filename;
throw mona_fatal_error(errmsg.str());
}
return result;
}
vector<int> get_mapper(const CClassMap& map)
{
vector<int> result;
int iclass = 0;
int pos = 0;
CClassMap::const_iterator im = map.begin();
CClassMap::const_iterator em = map.end();
while (im != em) {
while (pos < im->second.second) {
result.push_back(iclass);
++pos;
}
++iclass;
++im;
}
return result;
}
int main(int argc, const char *args[])
{
string in_filename;
string out_filename;
string map_filename;
bool split;
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i', "input image(s) to be remapped", NULL));
options.push_back(popt::option( out_filename, "out-file", 'o', "output image(s) to be remapped", NULL));
options.push_back(popt::option( map_filename, "map", 'm', "kmeans map file", NULL));
options.push_back(popt::option( split, "split", 's', "split the input file into a bit-file per class"));
try {
vector<string> non_args;
popt::parse_options(argc, args, options, non_args);
if (!non_args.empty()) {
throw mona_fatal_error("unrecognised arguments detected, try 'eva-remap --help'");
}
if (map_filename.empty())
throw mona_fatal_error("required argument --map missing");
if (in_filename.empty())
throw mona_fatal_error("required argument --in-file missing");
if (out_filename.empty())
throw mona_fatal_error("required argument --out-file missing");
CClassMap cmap = read_map(map_filename);
vector<int> map = get_mapper(cmap);
C2DImageIOPluginHandler imageio;
auto_ptr<C2DImageList> in_image_list(imageio.load(in_filename));
if (!in_image_list.get() || !in_image_list->size())
throw mona_runtime_error(string("no images found in ") + in_filename);
if (split) {
CSMapImage mapper(map, cmap.size());
C2DImageList out_list = wrap_filter(mapper, *in_image_list->begin());
if ( !imageio.save(in_image_list->get_sourceformat(), out_list, out_filename) ){
string not_save = ("unable to save result to ") + out_filename;
throw mona_runtime_error(not_save);
};
}else{
CMapImage mapper(map, cmap.size());
for (C2DImageList::iterator img = in_image_list->begin();
img != in_image_list->end(); ++img)
*img = wrap_filter(mapper, *img);
if ( !imageio.save(in_image_list->get_sourceformat(), *in_image_list, out_filename) ){
string not_save = ("unable to save result to ") + out_filename;
throw mona_runtime_error(not_save);
};
}
return EXIT_SUCCESS;
}
catch (const mona_runtime_error& e){
cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const mona_fatal_error& e){
cerr << args[0] << " fatal: " << e.what() << "\n";
}
catch (const mona_exception& e){
cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const runtime_error &e){
cerr << args[0] << " runtime: " << e.what() << "\n";
}
catch (const invalid_argument &e){
cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const exception& e){
cerr << args[0] << " error: " << e.what() << "\n";
}
catch (...){
cerr << args[0] << " unknown exception" << "\n";
}
return EXIT_FAILURE;
}