/* -*- 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;
}