[go: up one dir, main page]

Menu

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

Download this file

156 lines (127 with data), 4.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
/* -*- 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>
using namespace mona;
typedef std::pair<int, int> CClassRange;
typedef std::map<double, CClassRange> CClassMap;
class CBinarizeImage {
public:
typedef C2DImageWrap result_type;
CBinarizeImage(unsigned int min, unsigned int max);
template <typename Image2D>
CBinarizeImage::result_type operator ()(const Image2D& image)const;
private:
int _M_min;
int _M_max;
};
CBinarizeImage::CBinarizeImage(unsigned int min, unsigned int max):
_M_min(min),
_M_max(max)
{
}
template <typename Image2D>
CBinarizeImage::result_type CBinarizeImage::operator ()(const Image2D& image) const
{
typename Image2D::const_iterator ii = image.begin();
typename Image2D::const_iterator ie = image.end();
C2DBitImage * result = new C2DBitImage(image.get_size());
if (!result)
throw std::runtime_error("Out of memory");
cvdebug() << "filter with " << _M_min << ", " << _M_max << "\n";
C2DBitImage::iterator oi = result->begin();
while (ii != ie) {
*oi = ((int)*ii <= _M_max && (int)*ii >= _M_min);
++ii;
++oi;
}
return C2DImageWrap(result);
}
int main(int argc, const char *args[])
{
std::string in_filename;
std::string out_filename;
std::string map_filename;
int min, max;
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( min, "min", 0, "minimum value", "0"));
options.push_back(popt::option( max, "max", 0, "minimum value", "65534"));
try {
std::vector<std::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 (in_filename.empty())
throw mona_fatal_error("required argument --in-image missing");
if (out_filename.empty())
throw mona_fatal_error("required argument --out-image missing");
C2DImageIOPluginHandler imageio;
std::auto_ptr<C2DImageList> in_image_list(imageio.load(in_filename));
CBinarizeImage binarizer(min,max);
if (in_image_list.get() && in_image_list->size()) {
for (C2DImageList::iterator img = in_image_list->begin();
img != in_image_list->end(); ++img)
*img = wrap_filter(binarizer, *img);
}else{
std::string no_image = ("no images found in ") + in_filename;
throw mona_runtime_error(no_image);
}
if ( !imageio.save(in_image_list->get_sourceformat(), *in_image_list, out_filename) ){
std::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){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const mona_fatal_error& e){
std::cerr << args[0] << " fatal: " << e.what() << "\n";
}
catch (const mona_exception& e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const std::runtime_error &e){
std::cerr << args[0] << " runtime: " << e.what() << "\n";
}
catch (const std::invalid_argument &e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const std::exception& e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (...){
std::cerr << args[0] << " unknown exception" << "\n";
}
return EXIT_FAILURE;
}