libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNImage.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, INSA-Lyon, 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: CRNImage.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNIMAGE_HEADER
23 #define CRNIMAGE_HEADER
24 
25 #include <CRNMath/CRNMath.h>
26 #include <CRNException.h>
27 #include <CRNString.h>
29 #include <vector>
30 #include <type_traits>
31 
32 namespace crn
33 {
37 #define FOREACHPIXEL(x, y, img) for (size_t y = 0; y < (img).GetHeight(); y++) for (size_t x = 0; x < (img).GetWidth(); x++)
38 
39  class Rect;
40  class MatrixInt;
41  class MatrixDouble;
42  class Path;
43  class Point2DInt;
44 
46  class ImageBase
47  {
48  public:
49  virtual ~ImageBase();
50 
51  /**************************************************************************************
52  * Construction and copy
53  *************************************************************************************/
54  ImageBase(size_t w, size_t h): width(w), height(h) {}
55  ImageBase(const ImageBase&) = default;
56  ImageBase(ImageBase&&) = default;
57  ImageBase& operator=(const ImageBase&) = default;
58  ImageBase& operator=(ImageBase&&) = default;
59 
60  virtual std::unique_ptr<ImageBase> Clone() const = 0;
61 
63  virtual void SavePNG(const Path &fname) const = 0;
65  virtual void SaveJPEG(const Path &fname, unsigned int qual) const = 0;
66 
67  /**************************************************************************************
68  * Accessors
69  *************************************************************************************/
70 
72  size_t GetWidth() const noexcept { return width; }
74  size_t GetHeight() const noexcept { return height; }
76  Rect GetBBox() const noexcept;
78  size_t Size() const noexcept { return width * height; }
79 
80  /**************************************************************************************
81  * Edition
82  *************************************************************************************/
83 
85  virtual void ScaleToSize(size_t w, size_t h) = 0;
86 
87  /**************************************************************************************
88  * Transformation
89  *************************************************************************************/
90 
91  protected:
92  size_t width, height;
93 
94 #ifdef CRN_USING_GDIPLUS
95  struct gditoken;
96  static std::unique_ptr<gditoken> gdiinit;
98 #endif
99  };
100 
101  using SImage = std::shared_ptr<ImageBase>;
102  using SCImage = std::shared_ptr<const ImageBase>;
103  using WImage = std::weak_ptr<ImageBase>;
104  using WCImage = std::weak_ptr<const ImageBase>;
105  using UImage = std::unique_ptr<ImageBase>;
106  using UCImage = std::unique_ptr<const ImageBase>;
107 
108  class Path;
110  UImage NewImageFromFile(const Path &fname);
111 
113  template<typename T> struct BoolNotBool
114  {
115  using type = bool;
116  };
119  {
120  inline BoolNotBoolDummy(int i = 0) {}
121  inline BoolNotBoolDummy& operator+=(const BoolNotBoolDummy&) { return *this; }
122  };
128  template<> struct BoolNotBool<bool>
129  {
131  };
132  /****************************************************************************/
141  template<class T> class Image: public ImageBase
142  {
143  public:
144  using pixel_type = T;
145 
146  /**************************************************************************************
147  * Construction and copy
148  *************************************************************************************/
149 
151  Image(size_t w, size_t h, pixel_type val = pixel_type(0));
153  Image():Image{1, 1} { }
155  Image(size_t w, size_t h, const pixel_type *data);
156 
158  Image(const Image &img) = default;
160  template<typename Y> explicit Image(const Image<Y> &img);
162  explicit Image(const Image<typename BoolNotBool<T>::type> &img);
164  template<typename Y> Image(const Image<Y> &img, const Rect &bbox);
166  Image(const Image<typename BoolNotBool<T>::type> &img, const Rect &bbox);
168  Image(Image &&img) = default;
169 
171  virtual ~Image() override = default;
172 
174  Image& operator=(const Image &img) = default;
176  template<typename Y> Image& operator=(const Image<Y> &img);
178  Image& operator=(const Image<typename BoolNotBool<T>::type> &img);
180  template<typename Y> void Assign(const Image<Y> &img);
182  Image& operator=(Image &&img) = default;
183 
184  virtual std::unique_ptr<ImageBase> Clone() const override { return std::make_unique<Image>(*this); }
185 
187  void Swap(Image &other)
188  {
189  std::swap(width, other.width);
190  std::swap(height, other.height);
191  std::swap(pixels, other.pixels);
192  }
193 
195  virtual void SavePNG(const Path &fname) const override;
197  virtual void SaveJPEG(const Path &fname, unsigned int qual) const override;
198 
200  template<typename MAT> MAT ToMatrix() const
201  {
202  auto mat = MAT{GetHeight(), GetWidth()};
203  std::copy(begin(), end(), mat[0]);
204  return mat;
205  }
206 
207  /**************************************************************************************
208  * Accessors
209  *************************************************************************************/
210 
211  typename std::vector<pixel_type>::iterator begin() { return pixels.begin(); }
212  typename std::vector<pixel_type>::const_iterator begin() const { return pixels.begin(); }
213  typename std::vector<pixel_type>::iterator end() { return pixels.end(); }
214  typename std::vector<pixel_type>::const_iterator end() const { return pixels.end(); }
215  typename std::vector<pixel_type>::const_iterator cbegin() const { return pixels.begin(); }
216  typename std::vector<pixel_type>::const_iterator cend() const { return pixels.end(); }
217 
219  inline typename std::vector<pixel_type>::pointer GetPixels() noexcept { return pixels.data(); }
221  inline typename std::vector<pixel_type>::const_pointer GetPixels() const noexcept { return pixels.data(); }
222 
224  inline typename std::vector<pixel_type>::reference At(size_t x, size_t y) noexcept { return pixels[x + y * width]; }
226  inline typename std::vector<pixel_type>::reference At(size_t offset) noexcept { return pixels[offset]; }
228  inline typename std::vector<pixel_type>::const_reference At(size_t x, size_t y) const noexcept { return pixels[x + y * width]; }
230  inline typename std::vector<pixel_type>::const_reference At(size_t offset) const noexcept { return pixels[offset]; }
231 
233  bool operator==(const Image &other) const;
235  bool operator!=(const Image &other) const { return !(*this == other); }
236 
237  /**************************************************************************************
238  * Arithmetic
239  *************************************************************************************/
240 
242  Image& operator+=(const Image &img);
244  Image& operator-=(const Image &img);
246  Image& operator*=(double f);
248  Image& operator*=(const Image &img);
250  Image& operator/=(const Image &img);
251 
252  /**************************************************************************************
253  * Edition
254  *************************************************************************************/
255 
257  void Negative();
259  void Complement(pixel_type maxval = std::numeric_limits<pixel_type>::max());
260 
262  template<typename Y> void Blit(const Image<Y> &src, const Rect &srczone, size_t dx, size_t dy);
263 
265  void FloodFill(size_t x, size_t y, const pixel_type &val, crn::DistanceType dist = crn::DistanceType::D4);
267  void ScanFill(size_t x, size_t y, const pixel_type &val, crn::DistanceType dist = crn::DistanceType::D4);
268 
270  void DrawRect(const Rect &r, pixel_type color, bool filled = false);
272  void DrawLine(size_t x1, size_t y1, size_t x2, size_t y2, pixel_type color);
273 
275  virtual void ScaleToSize(size_t w, size_t h) override;
276 
278  void Flip(const Orientation &ori);
279 
281  template<typename CMP = std::less<pixel_type>> void Dilate(const MatrixInt &strel, CMP cmp = std::less<pixel_type>{});
283  template<typename CMP = std::less<pixel_type>> void Erode(const MatrixInt &strel, CMP cmp = std::less<pixel_type>{});
285  template<typename CMP = std::less<pixel_type>> void FastDilate(size_t halfwin, size_t index = 0, CMP cmp = std::less<pixel_type>{});
287  template<typename CMP = std::less<pixel_type>> void FastErode(size_t halfwin, size_t index = 0, CMP cmp = std::less<pixel_type>{});
288 
290  void Convolve(const MatrixDouble &mat);
292  void GaussianBlur(double sigma);
293 
294  protected:
295  std::vector<pixel_type> pixels;
296  };
297 
298  /**************************************************************************************
299  * Characterization
300  *************************************************************************************/
301 
303  template<typename T> bool IsBitonal(const Image<T> &img);
304 
306  template<typename T, typename CMP = std::less<T>> std::pair<T, T> MinMax(const Image<T> &img, CMP cmp = CMP{});
307 
309  template<typename T> Rect AutoCrop(const Image<T> &img, const T &bgval);
311  template<typename T> Image<T> MakeAutoCrop(const Image<T> &img, const T &bgval);
312 
314  template<typename T, typename Y> Point2DInt CrossCorrelation(const Image<T> &img1, const Image<Y> &img2, T fill1 = T(0), Y fill2 = Y(0));
315 
316  /**************************************************************************************
317  * Transformation
318  *************************************************************************************/
319 
321  template<typename IMG, typename T> IMG Downgrade(const Image<T> &img);
322 
324  template<typename T> SummedAreaTable<SumType<T>> MakeSummedAreaTable(const Image<T> &img);
325 
327  template<typename T> Image<T> MakeRotation(const Image<T> &img, const Angle<Degree> &angle, const T &bgColor);
329  template<typename T> Image<T> Make90Rotation(const Image<T> &img);
331  template<typename T> Image<T> Make180Rotation(const Image<T> &img);
333  template<typename T> Image<T> Make270Rotation(const Image<T> &img);
334 
335  template<typename T> inline auto Size(const Image<T> &img) noexcept(noexcept(img.Size())) -> decltype(img.Size()) { return img.Size(); }
337 } // namespace crn
338 
341 template<typename T1, typename T2> crn::Image<crn::SumType<typename std::common_type<T1, T2>::type>> operator+(const crn::Image<T1> &i1, const crn::Image<T2> &i2);
342 template<typename T1, typename T2> crn::Image<crn::DiffType<typename std::common_type<T1, T2>::type>> operator-(const crn::Image<T1> &i1, const crn::Image<T2> &i2);
343 template<typename T1, typename T2> crn::Image<crn::SumType<typename std::common_type<T1, T2>::type>> operator*(const crn::Image<T1> &i1, const crn::Image<T2> &i2);
346 template<typename T1, typename T2> crn::Image<crn::SumType<typename std::common_type<T1, T2>::type>> operator/(const crn::Image<T1> &i1, const crn::Image<T2> &i2);
350 
351 namespace crn
352 {
354  UImageRGB NewImageRGBFromFile(const Path &fname);
356  UImageGray NewImageGrayFromFile(const Path &fname);
358  UImageBW NewImageBWFromFile(const Path &fname);
359 
364  template<typename IMG, typename T> IMG Downgrade(const Image<T> &img)
365  {
366  using new_pixel = typename IMG::pixel_type;
367  using common_pixel = typename std::common_type<T, new_pixel>::type;
368 
369  auto res = IMG(img.GetWidth(), img.GetHeight());
370  const auto mM = MinMax(img);
371  if ((common_pixel(mM.first) < common_pixel(std::numeric_limits<new_pixel>::lowest())) ||
372  (common_pixel(mM.second) > common_pixel(std::numeric_limits<new_pixel>::max())))
373  {
374  for (auto tmp : Range(img))
375  {
376  auto val = common_pixel(img.At(tmp)) - common_pixel(mM.first);
377  val = (val * common_pixel(std::numeric_limits<new_pixel>::max())) / common_pixel(mM.second - mM.first);
378  res.At(tmp) = new_pixel(val);
379  }
380  }
381  else
382  {
383  for (auto tmp : Range(img))
384  res.At(tmp) = new_pixel(img.At(tmp));
385  }
386  return res;
387  }
388 
394  template<typename IMG, typename T> IMG Downgrade(const Image<pixel::RGB<T>> &img)
395  {
396  using new_pixel = typename IMG::pixel_type;
397  using new_base = typename new_pixel::type;
398  using common_base = typename std::common_type<T, typename new_pixel::type>::type;
399  using common_pixel = pixel::RGB<common_base>;
400 
401  auto res = IMG(img.GetWidth(), img.GetHeight());
402  auto m = std::numeric_limits<T>::max();
403  auto M = std::numeric_limits<T>::lowest();
404  for (const auto &p : img)
405  {
406  if (p.r < m) m = p.r;
407  if (p.g < m) m = p.g;
408  if (p.b < m) m = p.b;
409  if (p.r > M) M = p.r;
410  if (p.g > M) M = p.g;
411  if (p.b > M) M = p.b;
412  }
413  if ((common_base(m) < common_base(std::numeric_limits<new_base>::lowest())) ||
414  (common_base(M) > common_base(std::numeric_limits<new_base>::max())))
415  {
416  for (auto tmp : Range(img))
417  {
418  auto val = common_pixel(img.At(tmp)) - common_pixel(m);
419  val = (val * common_base(std::numeric_limits<new_base>::max())) / common_base(M - m);
420  res.At(tmp) = new_pixel(val);
421  }
422  }
423  else
424  {
425  for (auto tmp : Range(img))
426  res.At(tmp) = new_pixel(img.At(tmp));
427  }
428  return res;
429  }
430 
431  /*************************************************************************************
432  * File operations
433  ************************************************************************************/
434 
435  namespace impl
436  {
438  void SavePNG(const ImageBW &img, const Path &fname);
440  void SaveJPEG(const ImageBW &img, const Path &fname, unsigned int qual);
441 
443  void SavePNG(const ImageGray &img, const Path &fname);
445  void SaveJPEG(const ImageGray &img, const Path &fname, unsigned int qual);
446 
448  void SavePNG(const ImageRGB &img, const Path &fname);
450  void SaveJPEG(const ImageRGB &img, const Path &fname, unsigned int qual);
451 
456  template<typename T> void SavePNG(const Image<pixel::RGB<T>> &img, const Path &fname)
457  {
458  SavePNG(Downgrade<ImageRGB>(img), fname);
459  }
465  template<typename T> void SaveJPEG(const Image<pixel::RGB<T>> &img, const Path &fname, unsigned int qual)
466  {
467  SaveJPEG(Downgrade<ImageRGB>(img), fname, qual);
468  }
469 
474  template<typename T> void SavePNG(const Image<T> &img, const Path &fname, typename std::enable_if<std::is_arithmetic<T>::value>::type *dummy = nullptr)
475  {
476  SavePNG(Downgrade<ImageGray>(img), fname);
477  }
483  template<typename T> void SaveJPEG(const Image<T> &img, const Path &fname, unsigned int qual, typename std::enable_if<std::is_arithmetic<T>::value>::type *dummy = nullptr)
484  {
485  SaveJPEG(Downgrade<ImageGray>(img), fname, qual);
486  }
487 
493  template<typename T> void SavePNG(const Image<T> &img, const Path &fname, typename std::enable_if<!std::is_arithmetic<T>::value>::type *dummy = nullptr)
494  {
495  throw ExceptionInvalidArgument("SavePNG(): pixel format not supported.");
496  }
497 
504  template<typename T> void SaveJPEG(const Image<T> &img, const Path &fname, unsigned int qual, typename std::enable_if<!std::is_arithmetic<T>::value>::type *dummy = nullptr)
505  {
506  throw ExceptionInvalidArgument("SaveJPEG(): pixel format not supported.");
507  }
508  }
509 
510 }
511 
512 #include <CRNImage/CRNImage.hpp>
513 
514 #endif
virtual std::unique_ptr< ImageBase > Clone() const =0
BoolNotBoolDummy operator-(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:124
void GaussianBlur(double sigma)
Gaussian blur.
Definition: CRNImage.hpp:1132
Abstract class for images.
Definition: CRNImage.h:141
void Dilate(const MatrixInt &strel, CMP cmp=std::less< pixel_type >{})
Morphological dilatation.
Definition: CRNImage.hpp:761
virtual ~ImageBase()
crn::Image< crn::SumType< typename std::common_type< T1, T2 >::type > > operator*(const crn::Image< T1 > &i1, const crn::Image< T2 > &i2)
Definition: CRNImage.hpp:1385
std::vector< pixel_type >::reference At(size_t offset) noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:226
std::shared_ptr< const ImageBase > SCImage
Definition: CRNImage.h:102
ScalarRange< T > Range(T b, T e)
Creates a range [[b, e[[.
Definition: CRNType.h:257
BoolNotBoolDummy & operator+=(const BoolNotBoolDummy &)
Definition: CRNImage.h:121
void ScanFill(size_t x, size_t y, const pixel_type &val, crn::DistanceType dist=crn::DistanceType::D4)
Fills a portion of the image.
Definition: CRNImage.hpp:363
Orientation
An enumeration of orientations.
Definition: CRNMath.h:152
virtual void SaveJPEG(const Path &fname, unsigned int qual) const =0
Saves as JPEG file.
virtual void ScaleToSize(size_t w, size_t h)=0
Scales the image.
std::vector< pixel_type >::const_pointer GetPixels() const noexcept
Gets a const pointer to the pixels.
Definition: CRNImage.h:221
void Assign(const Image< Y > &img)
Force copy operator (pixel cast)
Definition: CRNImage.hpp:147
UImageBW NewImageBWFromFile(const Path &fname)
Loads an image from a file and converts it if necessary.
Definition: CRNImage.cpp:702
virtual void SavePNG(const Path &fname) const =0
Saves as PNG file.
std::vector< pixel_type >::reference At(size_t x, size_t y) noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:224
Image< T > MakeRotation(const Image< T > &img, const Angle< Degree > &angle, const T &bgColor)
Creates a rotated version of the image.
Definition: CRNImage.hpp:1813
size_t GetHeight() const noexcept
Definition: CRNImage.h:74
std::vector< pixel_type > pixels
Definition: CRNImage.h:295
std::vector< pixel_type >::const_iterator begin() const
Definition: CRNImage.h:212
std::vector< pixel_type >::const_reference At(size_t offset) const noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:230
Rect AutoCrop(const Image< T > &img, const T &bgval)
Estimates the ideal crop for the image.
Definition: CRNImage.hpp:1459
std::unique_ptr< ImageBase > UImage
Definition: CRNImage.h:105
std::vector< pixel_type >::const_iterator cbegin() const
Definition: CRNImage.h:215
std::weak_ptr< const ImageBase > WCImage
Definition: CRNImage.h:104
std::vector< pixel_type >::const_iterator end() const
Definition: CRNImage.h:214
virtual std::unique_ptr< ImageBase > Clone() const override
Definition: CRNImage.h:184
void Flip(const Orientation &ori)
Flips the image.
Definition: CRNImage.hpp:729
void SavePNG(const ImageBW &img, const Path &fname)
Saves as PNG file.
crn::Image< crn::SumType< typename std::common_type< T1, T2 >::type > > operator/(const crn::Image< T1 > &i1, const crn::Image< T2 > &i2)
Definition: CRNImage.hpp:1410
std::vector< pixel_type >::const_iterator cend() const
Definition: CRNImage.h:216
std::vector< pixel_type >::iterator end()
Definition: CRNImage.h:213
void Erode(const MatrixInt &strel, CMP cmp=std::less< pixel_type >{})
Morphological erosion.
Definition: CRNImage.hpp:827
Image & operator*=(double f)
Multiplies all pixels.
Definition: CRNImage.hpp:221
Integer matrix class.
Definition: CRNMatrixInt.h:40
crn::Image< crn::DiffType< typename std::common_type< T1, T2 >::type > > operator-(const crn::Image< T1 > &i1, const crn::Image< T2 > &i2)
Definition: CRNImage.hpp:1374
BoolNotBoolDummy(int i=0)
Definition: CRNImage.h:120
void Swap(Image &other)
Swaps two images.
Definition: CRNImage.h:187
UImageGray NewImageGrayFromFile(const Path &fname)
Loads an image from a file and converts it if necessary.
Definition: CRNImage.cpp:681
size_t Size(const Vector &v) noexcept
Size of a vector.
Definition: CRNVector.h:169
bool operator!=(const Image &other) const
Tests equality.
Definition: CRNImage.h:235
Image< T > Make270Rotation(const Image< T > &img)
Creates a rotated version of the image.
Definition: CRNImage.hpp:1882
void FastDilate(size_t halfwin, size_t index=0, CMP cmp=std::less< pixel_type >{})
Morphological dilatation.
Definition: CRNImage.hpp:890
size_t width
Definition: CRNImage.h:92
Image & operator/=(const Image &img)
Divides by another image's pixels.
Definition: CRNImage.hpp:247
Image & operator=(const Image &img)=default
Copy operator.
UImageRGB NewImageRGBFromFile(const Path &fname)
Loads an image from a file and converts it if necessary.
Definition: CRNImage.cpp:660
A convenience class for file paths.
Definition: CRNPath.h:39
bool IsBitonal(const Image< T > &img)
Is the image binary (black & white)?
Definition: CRNImage.hpp:1431
Image & operator+=(const Image &img)
Adds another image.
Definition: CRNImage.hpp:194
BoolNotBoolDummy operator*(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:125
ImageBase & operator=(const ImageBase &)=default
SummedAreaTable< SumType< T > > MakeSummedAreaTable(const Image< T > &img)
Creates a summed area table of the image.
Definition: CRNImage.hpp:1613
BoolNotBoolDummy operator+(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:123
crn::Image< crn::SumType< typename std::common_type< T1, T2 >::type > > operator+(const crn::Image< T1 > &i1, const crn::Image< T2 > &i2)
Definition: CRNImage.hpp:1363
Image & operator-=(const Image &img)
Subtracts another image.
Definition: CRNImage.hpp:208
Image< T > Make90Rotation(const Image< T > &img)
Creates a rotated version of the image.
Definition: CRNImage.hpp:1857
Point2DInt CrossCorrelation(const Image< T > &img1, const Image< Y > &img2, T fill1=T(0), Y fill2=Y(0))
Best match between two images.
Definition: CRNImage.hpp:1550
virtual void SavePNG(const Path &fname) const override
Saves as PNG file.
Definition: CRNImage.hpp:160
void Convolve(const MatrixDouble &mat)
Convolution.
Definition: CRNImage.hpp:1023
void DrawLine(size_t x1, size_t y1, size_t x2, size_t y2, pixel_type color)
Draws a line using a specified color.
Definition: CRNImage.hpp:543
size_t height
Definition: CRNImage.h:92
virtual void SaveJPEG(const Path &fname, unsigned int qual) const override
Saves as JPEG file.
Definition: CRNImage.hpp:169
std::unique_ptr< const ImageBase > UCImage
Definition: CRNImage.h:106
DistanceType
An enumeration of distances.
Definition: CRNMath.h:159
UImage NewImageFromFile(const Path &fname)
Loads an image from a file.
Definition: CRNImage.cpp:609
std::vector< pixel_type >::iterator begin()
Definition: CRNImage.h:211
std::weak_ptr< ImageBase > WImage
Definition: CRNImage.h:103
Image()
Default constructor.
Definition: CRNImage.h:153
Rect GetBBox() const noexcept
Definition: CRNImage.cpp:56
BoolNotBoolDummy operator/(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:126
IMG Downgrade(const Image< T > &img)
Converts the image to a type that has a smaller pixel range.
Definition: CRNImage.h:364
void Complement(pixel_type maxval=std::numeric_limits< pixel_type >::max())
Complement.
Definition: CRNImage.hpp:270
void Blit(const Image< Y > &src, const Rect &srczone, size_t dx, size_t dy)
Copies a part of an image.
Definition: CRNImage.hpp:282
std::pair< T, T > MinMax(const Image< T > &img, CMP cmp=CMP{})
Returns min and max pixel values.
Definition: CRNImage.hpp:1447
void SaveJPEG(const ImageBW &img, const Path &fname, unsigned int qual)
Saves as JPEG file.
size_t GetWidth() const noexcept
Definition: CRNImage.h:72
void Negative()
Negative.
Definition: CRNImage.hpp:261
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
size_t Size() const noexcept
Definition: CRNImage.h:78
MAT ToMatrix() const
Converts to matrix.
Definition: CRNImage.h:200
Image< T > MakeAutoCrop(const Image< T > &img, const T &bgval)
Creates a new image as the ideal crop for the image.
Definition: CRNImage.hpp:1535
std::shared_ptr< ImageBase > SImage
Definition: CRNImage.h:101
std::vector< pixel_type >::pointer GetPixels() noexcept
Gets a pointer to the pixels.
Definition: CRNImage.h:219
T pixel_type
Definition: CRNImage.h:144
Image< T > Make180Rotation(const Image< T > &img)
Creates a rotated version of the image.
Definition: CRNImage.hpp:1869
void DrawRect(const Rect &r, pixel_type color, bool filled=false)
Draws a rectangle using a specified color.
Definition: CRNImage.hpp:515
ImageBase(size_t w, size_t h)
Definition: CRNImage.h:54
virtual void ScaleToSize(size_t w, size_t h) override
Scales the image.
Definition: CRNImage.hpp:614
bool operator==(const Image &other) const
Tests equality.
Definition: CRNImage.hpp:178
Base class for images.
Definition: CRNImage.h:46
std::vector< pixel_type >::const_reference At(size_t x, size_t y) const noexcept
Returns a reference to a pixel.
Definition: CRNImage.h:228
virtual ~Image() override=default
Destructor.
void FastErode(size_t halfwin, size_t index=0, CMP cmp=std::less< pixel_type >{})
Morphological erosion.
Definition: CRNImage.hpp:956
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
void FloodFill(size_t x, size_t y, const pixel_type &val, crn::DistanceType dist=crn::DistanceType::D4)
Flood fills a portion of the image.
Definition: CRNImage.hpp:311
A rectangle class.
Definition: CRNRect.h:46