[go: up one dir, main page]

Menu

[r999]: / libmona / src / dist2ascii.cc  Maximize  Restore  History

Download this file

173 lines (132 with data), 4.8 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
/* -*- 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
*
*/
// $Id: dist2ascii.cc,v 1.1 2006-08-22 10:15:15 wollny Exp $
/*! \brief eva-downscale
\sa eva-downscale3d.cc
\file downscale.cc
\author G. Wollny, wollny eva.mpg.de, 2005
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <stack>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <libmona/monaHistory.hh>
#include <libmona/monaException.hh>
#include <libmona/monaPopt.hh>
#include <libmona/3DVector.hh>
#include <libmona/filter.hh>
#include <libmona/fnameana.hh>
#include <libmona/2DImageWrap.hh>
#include <libmona/filter2dstack.hh>
#include <libmona/filetools.hh>
using namespace std;
using namespace mona;
void append_data(const C2DImageWrap& image, size_t z, float scale, ostream& outfile)
{
const C2DUSImage *src = image.getC2DUSImage();
if (!src) {
stringstream msg;
msg << "input image " << z << "is not result of a distance transform";
throw runtime_error(msg.str());
}
float iscale = 1.0 / scale;
C2DUSImage::const_iterator p = src->begin();
for (size_t y = 0; y < src->get_size().y; ++y)
for (size_t x = 0; x < src->get_size().x; ++x, ++p)
if (*p > 0)
outfile << x << " " << y << " " << z << " " << *p * iscale << "\n";
}
int main(int argc, const char *args[])
{
string in_filename;
string out_filename;
float scale;
C2DImageIOPluginHandler imageio;
// define options
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i', "input file name", ""));
options.push_back(popt::option( out_filename, "out-file", 'o', "output file name", "output.txt"));
options.push_back(popt::option( scale, "scale", 's', "fixed point scale of distcance transform", "256.0"));
vector<string> unknown_args;
popt::parse_options(argc, args, options, unknown_args);
if (!unknown_args.empty()) {
cverr() << "unknown arguments given\n";
return -1;
}
if (in_filename.empty()) {
cverr() << "No input file given, run '" << args[0] << " --help' to get help\n";
return -1;
}
if (out_filename.empty()) {
cverr() << "No output filename given, run '" << args[0] << " --help' to get help\n";
return -1;
}
try {
ofstream os( out_filename.c_str(), ios::out );
C2DImageList in_imagebuffer;
size_t start_filenum = 0;
size_t end_filenum = 0;
size_t format_width = 0;
std::string src_basename = get_filename_pattern_and_range(in_filename, start_filenum, end_filenum, format_width);
if (start_filenum >= end_filenum)
throw invalid_argument(string("no files match pattern ") + src_basename);
// read all the files
for (size_t i = start_filenum; i < end_filenum; ++i) {
cvmsg() << "read " << i << " out of [" << start_filenum << ", " << end_filenum << "]\n";
string src_name = create_filename(src_basename.c_str(), i);
auto_ptr<C2DImageList> in_image_list(imageio.load(src_name));
if (!in_image_list.get() || !in_image_list->size()) {
cverr() << "expected " << end_filenum - start_filenum << " images, got only" << i - start_filenum <<"\n";
break;
}
append_data(*in_image_list->begin(), i, scale, os );
}
cvmsg() << '\n';
return EXIT_SUCCESS;
}
catch (const mona_runtime_error& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << args[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const runtime_error &e){
cerr << args[0] << " runtime: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << args[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}