libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNDifferential.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, 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: CRNDifferential.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNDIFFERENTIAL_HEADER
23 #define CRNDIFFERENTIAL_HEADER
24 
26 #include <CRNImage/CRNImageGray.h>
27 #include <mutex>
28 
32 namespace crn
33 {
34  class Differential;
35  CRN_ALIAS_SMART_PTR(Differential)
36 }
37 namespace crn
38 {
39  /****************************************************************************/
51  {
52  public:
53  enum class RGBProjection { ABSMAX, SUM };
55  static Differential NewGaussian(const ImageRGB &src, RGBProjection proj, double sigma);
57  static Differential NewHalfDiff(const ImageRGB &src, RGBProjection proj);
59  static Differential NewHalfDiffAbsMax(const ImageRGB &src, RGBProjection proj);
61  static Differential NewHalfDiffAbsMin(const ImageRGB &src, RGBProjection proj);
62 
64  template<typename T> static Differential NewGaussian(const Image<T> &src, double sigma)
65  { auto i = ImageDoubleGray(src); return NewGaussian(i, sigma); }
67  template<typename T> static Differential NewHalfDiff(const Image<T> &src)
68  { auto i = ImageDoubleGray(src); return NewHalfDiff(i); }
70  template<typename T> static Differential NewHalfDiffAbsMax(const Image<T> &src)
71  { auto i = ImageDoubleGray(src); return NewHalfDiffAbsMax(i); }
73  template<typename T> static Differential NewHalfDiffAbsMin(const Image<T> &src)
74  { auto i = ImageDoubleGray(src); return NewHalfDiffAbsMin(i); }
75 
77  static Differential NewGaussian(const ImageDoubleGray &src, double sigma);
79  static Differential NewHalfDiff(const ImageDoubleGray &src);
84 
86  ~Differential() = default;
87 
88  Differential(const Differential &) = delete;
89  Differential(Differential &&) = default;
90  Differential& operator=(const Differential &) = delete;
91  Differential& operator=(Differential &&) = default;
92 
94  ImageDoubleGray& GetLx() noexcept { return lx; }
96  const ImageDoubleGray& GetLx() const { return lx; }
98  ImageDoubleGray& GetLy() noexcept { return ly; }
100  const ImageDoubleGray& GetLy() const { return ly; }
102  ImageDoubleGray& GetLx2Ly2() noexcept { return lx2ly2; }
104  const ImageDoubleGray& GetLx2Ly2() const { return lx2ly2; }
106  const ImageDoubleGray& GetLxx();
108  const ImageDoubleGray& GetLxy();
110  const ImageDoubleGray& GetLyx();
112  const ImageDoubleGray& GetLyy();
113 
115  void Diffuse(size_t maxiter, double maxdiv = std::numeric_limits<double>::max());
116 
125 
131  ImageDoubleGray MakeHessianCorner(double s = 0.1);
154 
156  double AutoThreshold();
158  double GetThreshold() const noexcept { return thres; }
160  void SetThreshold(double d) noexcept { thres = d; }
162  bool IsSignificant(size_t i) const noexcept { return GetLx2Ly2().At(i) > thres; }
164  bool IsSignificant(size_t x, size_t y) const noexcept { return GetLx2Ly2().At(x, y) > thres; }
170  template<typename T> void FillNonSignificant(Image<T> &img, T val = T(0)) const
171  {
172  if ((img.GetWidth() != lx2ly2.GetWidth()) || (img.GetHeight() != lx2ly2.GetHeight()))
173  throw ExceptionDimension("The image does not have the same dimensions as the differential.");
174  for (auto tmp : Range(img))
175  {
176  if (!IsSignificant(tmp))
177  img.At(tmp) = val;
178  }
179  }
180 
181  private:
183  Differential(ImageGray &&src, ImageDoubleGray &&xdiff, ImageDoubleGray &&ydiff, ImageDoubleGray &&xxdiff, ImageDoubleGray &&yydiff);
185  Differential(ImageGray &&src, ImageDoubleGray &&xdiff, ImageDoubleGray &&ydiff);
186 
188  void updateLx2ly2();
189 
190  ImageGray srcigray;
192  ImageDoubleGray lx;
193  ImageDoubleGray ly;
194  ImageDoubleGray lxx;
195  ImageDoubleGray lxy;
196  ImageDoubleGray lyx;
197  ImageDoubleGray lyy;
198  ImageDoubleGray lx2ly2;
199  double thres;
200  std::unique_ptr<std::mutex> lazydata;
201  };
202 }
203 #endif
double GetThreshold() const noexcept
Gets the threshold for lx2ly2.
ImageDoubleGray & GetLy() noexcept
Returns a reference to the internal y derivate.
Abstract class for images.
Definition: CRNImage.h:141
ScalarRange< T > Range(T b, T e)
Creates a range [[b, e[[.
Definition: CRNType.h:257
ImageDoubleGray MakeCorner()
Returns the corner image.
ImageDoubleGray MakeHessianCorner(double s=0.1)
Returns the Hessian corner image.
ImageDoubleGray MakeLvv()
Returns the second derivative of the tangent to the isophote.
ImageDoubleGray & GetLx() noexcept
Returns a reference to the internal x derivate.
std::vector< pixel_type >::reference At(size_t x, size_t y) noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:224
ImageDoubleGray MakeLww()
Returns the second derivative of the normal to the isophote.
const ImageDoubleGray & GetLxy()
Returns a reference to the internal xy derivate.
size_t GetHeight() const noexcept
Definition: CRNImage.h:74
ImageDoubleGray & GetLx2Ly2() noexcept
Returns a reference to the internal first derivatives norm.
ImageDoubleGray MakeEdge()
Returns the edge image.
ImageDoubleGray MakeIsophoteCurvature()
Returns the isophote curvature image.
ImageDoubleGray MakeKappa2()
Returns the second eigenvalue of the Hessian as an image.
const ImageDoubleGray & GetLx() const
Returns a reference to the internal x derivate.
static Differential NewHalfDiffAbsMax(const Image< T > &src)
AbsMax half derivatives.
bool IsSignificant(size_t x, size_t y) const noexcept
Is a pixel significant.
static Differential NewHalfDiffAbsMax(const ImageRGB &src, RGBProjection proj)
AbsMax half derivatives.
ImageDoubleGray MakeDivergence()
Creates the continuous image skeleton.
double AutoThreshold()
Computes the best threshold for lx2ly2.
const ImageDoubleGray & GetLy() const
Returns a reference to the internal y derivate.
~Differential()=default
Destructor.
static Differential NewHalfDiff(const ImageRGB &src, RGBProjection proj)
Alternate half derivatives.
static Differential NewGaussian(const ImageRGB &src, RGBProjection proj, double sigma)
Convolution with Gaussian derivatives.
Differential & operator=(const Differential &)=delete
const ImageDoubleGray & GetLxx()
Returns a reference to the internal xx derivate.
Image< double > ImageDoubleGray
double Grayscale image class
void FillNonSignificant(Image< T > &img, T val=T(0)) const
Cleans an image of non-significant values.
const ImageDoubleGray & GetLyx()
Returns a reference to the internal yx derivate.
static Differential NewGaussian(const Image< T > &src, double sigma)
Convolution with Gaussian derivatives.
static Differential NewHalfDiffAbsMin(const ImageRGB &src, RGBProjection proj)
AbsMin half derivatives.
ImageDoubleGray MakeCanny()
Returns Canny's edge detector image.
A dimension error.
Definition: CRNException.h:119
bool IsSignificant(size_t i) const noexcept
Is a pixel significant.
const ImageDoubleGray & GetLx2Ly2() const
Returns a reference to the internal first derivatives norm.
Gradient image in polar form.
Differential(const Differential &)=delete
ImageDoubleGray MakeKappa1()
Returns the first eigenvalue of the Hessian as an image.
ImageDoubleGray MakeFlowlineCurvature()
Returns the flowline curvature image.
size_t GetWidth() const noexcept
Definition: CRNImage.h:72
ImageGradient MakeImageGradient()
Returns the gradient image.
static Differential NewHalfDiff(const Image< T > &src)
Alternate half derivatives.
ImageDoubleGray MakeGaussianCurvature()
Returns the Gaussian curvature image.
ImageGray MakeGradientCurvature()
Returns the gradient curvature image.
CRN_ALIAS_SMART_PTR(ImageBW)
Differential computation on images.
void SetThreshold(double d) noexcept
Sets the threshold for lx2ly2.
ImageDoubleGray MakeLw()
Returns the derivative of the normal to the isophotes.
ImageDoubleGray MakeLaplacian()
Returns the Laplacian image.
ImageDoubleGray MakeLvw()
Returns the cross derivative of the tangent and normal to the isophote.
void Diffuse(size_t maxiter, double maxdiv=std::numeric_limits< double >::max())
Diffuses the gradient.
static Differential NewHalfDiffAbsMin(const Image< T > &src)
AbsMin half derivatives.
ImageDoubleGray MakeGradientModule()
Returns the gradient module image.
const ImageDoubleGray & GetLyy()
Returns a reference to the internal yy derivate.