sauvola_ms.cc 3.08 KB
Newer Older
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
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena 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, version 2 of the License.
//
// Olena 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 Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.

26
#include <mln/core/image/image2d.hh>
27
#include <mln/value/int_u8.hh>
28
29
#include <mln/io/pgm/load.hh>
#include <mln/io/pbm/save.hh>
30

31
#include <scribo/binarization/sauvola_ms.hh>
32
33
#include <scribo/debug/usage.hh>

34
bool check_args(int argc, char * argv[])
35
{
36
37
  if (argc < 5 || argc > 6)
    return false;
38

39
  int s = atoi(argv[3]);
40

41
  if (s < 1 || s > 3)
42
  {
43
44
45
    std::cout << "s must be set to 2 or 3."
	      << std::endl;
    return false;
46
47
  }

48
49
  return true;
}
50
51
52
53
54


const char *args_desc[][2] =
{
  { "input.pgm", "A graylevel image." },
55
  { "w", "Window size at scale 1. (Common value: 101)" },
56
57
  { "s", "First subsampling ratio (Common value: 3)." },
  { "min_area",    "Minimum object area at scale 1 (Common value: 67)" },
58
59
60
61
62
63
64
65
66
67
68
69
70
  {0, 0}
};




int main(int argc, char *argv[])
{
  using namespace mln;
  using namespace scribo;

  if (!check_args(argc, argv))
    return scribo::debug::usage(argv,
71
				"Multi-Scale Binarization of a color image based on Sauvola's algorithm.",
72
				"input.pgm w s area_threshold output.pbm",
73
74
75
76
				args_desc, "A binary image.");

  trace::entering("main");

77
78
  // Window size
  unsigned w_1 = atoi(argv[2]);  // Scale 1
79
80

  // First subsampling scale.
81
  unsigned s = atoi(argv[3]);
82

83
84
  // Lambda value
  unsigned lambda_min_1 = atoi(argv[4]);
85
86


87
88
  image2d<value::int_u8> input_1;
  io::pgm::load(input_1, argv[1]);
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
//   {
//     unsigned max_dim = math::min(input_1.ncols() / s,
// 				 input_1.nrows() / s);
//     if ((w_1 / s * 4) > max_dim)
//     {
//       std::cout << "------------------" << std::endl;
//       std::cout << "The window is too large! Image size is only "
// 		<< input_1.nrows() << "x" << input_1.ncols()
// 		<< std::endl
// 		<< "Window size must not exceed " << max_dim * s / 4
// 		<< std::endl;
//       return 1;
//     }
//   }
104

105
106
  image2d<bool>
    output = scribo::binarization::sauvola_ms(input_1, w_1, s, lambda_min_1);
107

108
  io::pbm::save(output, argv[5]);
109
}
110

111