[go: up one dir, main page]

Menu

[r991]: / libmona / src / multi-histo.cc  Maximize  Restore  History

Download this file

287 lines (217 with data), 7.4 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
/* -*- 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: multi-histo.cc,v 1.12 2006-08-14 12:18:38 wollny Exp $
/*! \brief eva-2dimagefilter
\sa 3va-2dimagefilter.cc
\file mask.cc
\author G. Wollny, wollny eva.mpg.de, 2005
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <stdexcept>
#include <dlfcn.h>
#include <libmona/2DImageWrap.hh>
#include <libmona/monaException.hh>
#include <libmona/monaHistory.hh>
#include <libmona/histogram.hh>
#include <libmona/filetools.hh>
#include <boost/type_traits.hpp>
using namespace std;
using namespace mona;
class CStatistics {
public:
typedef void result_type;
CStatistics(int hist_bins);
template <typename Image2D>
CStatistics::result_type operator ()(const Image2D& image);
void save(const std::string& out_filename) const;
int propose_threshold()const;
private:
std::vector<double> _M_histogram;
};
CStatistics::CStatistics(int hist_bins):
_M_histogram(hist_bins)
{
}
struct CRangeProposer {
typedef double result_type;
template <typename Image2D>
CRangeProposer::result_type operator ()(const Image2D& image)
{
return std::numeric_limits<typename Image2D::value_type>::max();
}
};
template <bool is_supported, typename Image2D>
struct __dispatch_historeader {
static int apply(const Image2D& image, std::vector<double>& histo) {
throw invalid_argument("CStatistics: Input pixel types bit, signed, long, float, double, and composits are not supported");
return 0;
}
};
template <typename Image2D>
struct __dispatch_historeader<true, Image2D> {
static int apply(const Image2D& image, std::vector<double>& histo) {
std::vector<double> lhisto(histo.size());
for (typename Image2D::const_iterator i = image.begin();
i != image.end(); ++i) {
#ifdef SUPPORT_SIGNED
if (*i < 0)
++lhisto[0];
else
#endif
if (*i < lhisto.size())
++lhisto[*i];
else
++lhisto[lhisto.size()-1];
}
transform(lhisto.begin(), lhisto.end(), histo.begin(), histo.begin(), plus<double>());
return 0;
}
};
template <typename Image2D>
CStatistics::result_type CStatistics::operator ()(const Image2D& image)
{
typedef typename Image2D::value_type pixel;
const bool is_supported = ::boost::type_traits::ice_or<
#ifdef SUPPORT_SIGNED
::boost::is_same<signed char, pixel>::value,
::boost::is_same<signed short, pixel>::value,
#endif
::boost::is_same<unsigned char, pixel>::value,
::boost::is_same<unsigned short, pixel>::value >::value;
__dispatch_historeader<is_supported, Image2D>::apply(image, _M_histogram);
}
void CStatistics::save(const std::string& out_filename)const
{
COutputFile f(out_filename);
for (std::vector<double>::const_iterator i = _M_histogram.begin();
i != _M_histogram.end(); ++i) {
fprintf(f, "%f\n", *i);
}
}
int CStatistics::propose_threshold()const
{
int pos = _M_histogram.size() / 10;
int max_pos = _M_histogram.size() / 6;
double val = _M_histogram[pos];
for (int i = pos; i > 10; --i)
if (_M_histogram[i] < val) {
pos = i;
val = _M_histogram[i];
}
for (int i = pos; i < max_pos; ++i)
if (_M_histogram[i] < val) {
pos = i;
val = _M_histogram[i];
}
return pos;
}
/* Revision string */
const char revision[] = "not specified";
int main( int argc, const char *argv[] )
{
string out_filename;
vector<string> extra_options;
bool oneshot = false;
string in_filename;
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i',
"input file name template, e.g. image0000.tif", ""));
options.push_back(popt::option(oneshot, "single", 's', "single file histogram"));
options.push_back(popt::option( out_filename, "out-file", 'o', "histogram output file", ""));
try {
C2DImageIOPluginHandler imageio;
popt::parse_options(argc, argv, options, extra_options);
if (!extra_options.empty())
throw invalid_argument("unknown options given");
char endline = cverb.show_debug() ? '\n' : '\r';
CHistory::instance().append(argv[0], revision, options);
// read the first image and initialise the filter
double range = 256;
vector<string> input_files;
if (oneshot)
input_files.push_back(in_filename);
else
input_files = get_consecutive_numbered_files(in_filename);
if (input_files.empty()) {
throw invalid_argument(string("no file match input pattern ") + in_filename);
}
std::vector<string>::const_iterator i = input_files.begin();
auto_ptr<C2DImageList> first_image(imageio.load(*i));
if (!first_image.get() || !first_image->size()) {
throw invalid_argument("no images in input file");
}
CRangeProposer rp;
range = wrap_filter(rp, *first_image->begin());
if (range > std::numeric_limits<unsigned int>::max())
throw invalid_argument("only byte and short valued image supported");
cvdebug() << "setting range to " << range << "\n";
CStatistics statistics((int)range);
cvmsg() << "processing " << *i << " "<<endline;
for (C2DImageList::const_iterator img = first_image->begin();
img != first_image->end(); ++img)
wrap_filter(statistics, *img);
++i;
first_image.reset(NULL);
for (; i != input_files.end(); ++i) {
cvmsg() << "processing " << *i << " "<<endline;
// read image
auto_ptr<C2DImageList> in_image_list(imageio.load(*i));
if (in_image_list.get() && in_image_list->size()) {
for (C2DImageList::const_iterator img = in_image_list->begin();
img != in_image_list->end(); ++img)
wrap_filter(statistics, *img);
}
}
cvmsg() << "\n";
if (!out_filename.empty())
statistics.save(out_filename);
if (out_filename != "-")
cout << statistics.propose_threshold() << "\n";
return EXIT_SUCCESS;
}
catch (const mona_runtime_error& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << argv[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const runtime_error &e){
cerr << argv[0] << " runtime: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << argv[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}