libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNMatrixInt.cpp
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNMatrixInt.cpp
19  * \author Yann LEYDIER, Jean DUONG
20  */
21 
22 #include <CRNMath/CRNMatrixInt.h>
23 #include <CRNMath/CRNMath.h>
24 #include <CRNException.h>
25 #include <CRNStringUTF8.h>
26 #include <CRNProtocols.h>
27 #include <CRNi18n.h>
28 
29 using namespace crn;
30 
31 // Special matrix constructors
32 #define MAX_Gauss_W 40
33 #define MULT 10
34 
45 {
46  if (sigma < 0)
47  throw ExceptionDomain(StringUTF8("MatrixInt::NewGaussian(double sigma): ") +
48  _("Negative standard deviation"));
49  if (sigma <= 0.001)
50  return MatrixInt(1, 1, 1);
51  size_t d = (int)ceil(Thrice(sigma));
52 
53  MatrixInt mat(d + d + 1, d + d + 1);
54 
55  // fill central column
56  for (size_t r = 0; r < d; r++)
57  mat[r][d] = (int)(MULT * MeanGauss(double(d) - double(r), sigma));
58  for (size_t r = d; r < mat.rows; r++)
59  mat[r][d] = (int)(MULT * MeanGauss(double(r) - double(d), sigma));
60  // fill central row
61  for (size_t c = 0; c < d; c++)
62  mat[d][c] = (int)(MULT * MeanGauss(double(d) - double(c), sigma));
63  for (size_t c = d; c < mat.cols; c++)
64  mat[d][c] = (int)(MULT * MeanGauss(double(c) - double(d), sigma));
65  // fill first quadrant
66  for (size_t r = 0; r < d; r++)
67  for (size_t c = d + 1; c < mat.cols; c++)
68  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
69  // fill second quadrant
70  for (size_t r = 0; r < d; r++)
71  for (size_t c = 0; c < d; c++)
72  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
73  // fill third quadrant
74  for (size_t r = d + 1; r < mat.rows; r++)
75  for (size_t c = 0; c < d; c++)
76  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
77  // fill fourth quadrant
78  for (size_t r = d + 1; r < mat.rows; r++)
79  for (size_t c = d + 1; c < mat.cols; c++)
80  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
81 
82  return mat;
83 }
84 
91 {
92  MatrixInt mat(3, 3, 0);
93 
94  mat[0][0] = 1;
95  mat[1][0] = 2;
96  mat[2][0] = 1;
97  mat[0][1] = 2;
98  mat[1][1] = 4;
99  mat[2][1] = 2;
100  mat[0][2] = 1;
101  mat[1][2] = 2;
102  mat[2][2] = 1;
103 
104  return mat;
105 }
106 
117 {
118  if (sigma < 0)
119  throw ExceptionDomain(StringUTF8("MatrixInt::NewGaussianSobelX(double sigma): ") +
120  _("Negative standard deviation"));
121  if (sigma <= 0.001)
122  return NewSobelX();
123  size_t d = (int)ceil(Thrice(sigma));
124 
125  MatrixInt mat(d + d + 1, d + d + 1);
126 
127  // fill central column
128  for (size_t r = 0; r < d; r++)
129  mat[r][d] = (int)(MULT * MeanGauss(double(d) - double(r), sigma));
130  for (size_t r = d; r < mat.rows; r++)
131  mat[r][d] = (int)(MULT * MeanGauss(double(r) - double(d), sigma));
132  // fill central row
133  for (size_t c = 0; c < d; c++)
134  mat[d][c] = (int)(MULT * MeanGauss(double(d) - double(c), sigma));
135  for (size_t c = d; c < mat.cols; c++)
136  mat[d][c] = (int)(MULT * MeanGauss(double(c) - double(d), sigma));
137  // fill first quadrant
138  for (size_t r = 0; r < d; r++)
139  for (size_t c = d + 1; c < mat.cols; c++)
140  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
141  // fill second quadrant
142  for (size_t r = 0; r < d; r++)
143  for (size_t c = 0; c < d; c++)
144  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
145  // fill third quadrant
146  for (size_t r = d + 1; r < mat.rows; r++)
147  for (size_t c = 0; c < d; c++)
148  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
149  // fill fourth quadrant
150  for (size_t r = d + 1; r < mat.rows; r++)
151  for (size_t c = d + 1; c < mat.cols; c++)
152  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
153 
154  // derivate
155  for (size_t r = 0; r < mat.rows; r++)
156  for (size_t c = 0; c < mat.cols; c++)
157  mat[r][c] *= int(d) - int(c);
158 
159  return mat;
160 }
161 
172 {
173  if (sigma < 0)
174  throw ExceptionDomain(StringUTF8("MatrixInt::NewGaussianSobelY(double sigma): ") +
175  _("Negative standard deviation"));
176  if (sigma <= 0.001)
177  return NewSobelY();
178  size_t d = (int)ceil(Thrice(sigma));
179 
180  MatrixInt mat(d + d + 1, d + d + 1);
181 
182  // fill central column
183  for (size_t r = 0; r < d; r++)
184  mat[r][d] = (int)(MULT * MeanGauss(double(d) - double(r), sigma));
185  for (size_t r = d; r < mat.rows; r++)
186  mat[r][d] = (int)(MULT * MeanGauss(double(r) - double(d), sigma));
187  // fill central row
188  for (size_t c = 0; c < d; c++)
189  mat[d][c] = (int)(MULT * MeanGauss(double(d) - double(c), sigma));
190  for (size_t c = d; c < mat.cols; c++)
191  mat[d][c] = (int)(MULT * MeanGauss(double(c) - double(d), sigma));
192  // fill first quadrant
193  for (size_t r = 0; r < d; r++)
194  for (size_t c = d + 1; c < mat.cols; c++)
195  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
196  // fill second quadrant
197  for (size_t r = 0; r < d; r++)
198  for (size_t c = 0; c < d; c++)
199  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
200  // fill third quadrant
201  for (size_t r = d + 1; r < mat.rows; r++)
202  for (size_t c = 0; c < d; c++)
203  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
204  // fill fourth quadrant
205  for (size_t r = d + 1; r < mat.rows; r++)
206  for (size_t c = d + 1; c < mat.cols; c++)
207  mat[r][c] = mat[r][d] * mat[d][c] / MULT;
208  // derivate
209  for (size_t r = 0; r < mat.rows; r++)
210  for (size_t c = 0; c < mat.cols; c++)
211  mat[r][c] *= int(d) - int(r);
212 
213  return mat;
214 }
215 
222 {
223  MatrixInt mat(3, 3, 0);
224 
225  mat[0][0] = 1;
226  mat[1][0] = 2;
227  mat[2][0] = 1;
228  mat[0][2] = -1;
229  mat[1][2] = -2;
230  mat[2][2] = -1;
231 
232  return mat;
233 }
234 
241 {
242  MatrixInt mat(3, 3, 0);
243 
244  mat[0][0] = 1;
245  mat[0][1] = 2;
246  mat[0][2] = 1;
247  mat[2][0] = -1;
248  mat[2][1] = -2;
249  mat[2][2] = -1;
250 
251  return mat;
252 }
253 
260 {
261  MatrixInt m(3, 3, -1);
262  m[0][1] = 1;
263  m[1][0] = 1;
264  return m;
265 }
266 
273 {
274  MatrixInt m(3, 3, -1);
275  m[1][1] = 0;
276  m[2][1] = 1;
277  m[1][2] = 1;
278  return m;
279 }
280 
287 {
288  MatrixInt m(3, 3, -1);
289  m[0][0] = 1;
290  m[0][1] = 1;
291  m[0][2] = 1;
292  m[1][0] = 1;
293  return m;
294 }
295 
302 {
303  MatrixInt m(3, 3, -1);
304  m[1][1] = 0;
305  m[1][2] = 1;
306  m[2][0] = 1;
307  m[2][1] = 1;
308  m[2][2] = 1;
309  return m;
310 }
311 
318 {
319  MatrixInt m(3, 3, -1);
320  m[0][0] = 4;
321  m[0][1] = 3;
322  m[0][2] = 4;
323  m[1][0] = 3;
324  return m;
325 }
326 
333 {
334  MatrixInt m(3, 3, -1);
335  m[1][1] = 0;
336  m[1][2] = 3;
337  m[2][0] = 4;
338  m[2][1] = 3;
339  m[2][2] = 4;
340  return m;
341 }
342 
344  Cloner::Register<MatrixInt>();
345 CRN_END_CLASS_CONSTRUCTOR(MatrixInt)
346 
static MatrixInt NewGaussianSobelY(double sigma)
Creates a Gaussian Sobel Y derivation mask.
static MatrixInt NewChamferDT2()
Creates the second chamfer DT mask.
static MatrixInt NewSobelY()
Creates a Sobel Y derivation mask.
#define _(String)
Definition: CRNi18n.h:51
double MeanGauss(double x, double sigma)
Computes Gauss function at x for a given standard deviation (centered in 0) – to use with matrices...
Definition: CRNMath.h:81
size_t rows
Definition: CRNMatrix.h:694
MatrixInt(const Matrix< int > &m)
Definition: CRNMatrixInt.h:44
static MatrixInt NewGaussianSobelX(double sigma)
Creates a Gaussian Sobel X derivation mask.
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:198
Integer matrix class.
Definition: CRNMatrixInt.h:40
static MatrixInt NewSobelX()
Creates a Sobel X derivation mask.
A generic domain error.
Definition: CRNException.h:83
static MatrixInt NewD8DT2()
Creates the second D8 DT mask.
static MatrixInt NewD4DT1()
Creates the first D4 DT mask.
static MatrixInt NewGaussian3()
Creates a 3x3 Gaussian mask.
static MatrixInt NewD8DT1()
Creates the first D8 DT mask.
constexpr TypeInfo< T >::SumType Thrice(const T &v) noexcept(noexcept(v+v))
Returns three times a value.
Definition: CRNMath.h:57
A character string class.
Definition: CRNStringUTF8.h:49
static MatrixInt NewGaussian(double sigma)
Creates a Gaussian mask given standard deviation.
#define MULT
size_t cols
Definition: CRNMatrix.h:694
static MatrixInt NewChamferDT1()
Creates the first chamfer DT mask.
static MatrixInt NewD4DT2()
Creates the second D4 DT mask.
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:185