augmentations.h
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 1.0.0
4 * copyright (c) 2021, Università degli Studi di Modena e Reggio Emilia (UNIMORE), AImageLab
5 * Authors:
6 * Costantino Grana (costantino.grana@unimore.it)
7 * Federico Bolelli (federico.bolelli@unimore.it)
8 * Michele Cancilla (michele.cancilla@unimore.it)
9 * Laura Canalini (laura.canalini@unimore.it)
10 * Stefano Allegretti (stefano.allegretti@unimore.it)
11 * All rights reserved.
12 */
13 
14 #ifndef AUGMENTATIONS_H_
15 #define AUGMENTATIONS_H_
16 
17 #include "ecvl/core/arithmetic.h"
18 #include "ecvl/core/imgproc.h"
19 #include <array>
20 #include <map>
21 #include <memory>
22 #include <random>
23 #include <iostream>
24 #include <algorithm>
25 #include <iterator>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace ecvl
30 {
31 #define ECVL_ERROR_AUGMENTATION_NAME throw std::runtime_error(ECVL_ERROR_MSG "Cannot load augmentation name");
32 #define ECVL_ERROR_AUGMENTATION_FORMAT throw std::runtime_error(ECVL_ERROR_MSG "Format error while loading augmentation parameters");
33 
34 class param_list;
35 
36 class param
37 {
38  static std::istream& read_until(std::istream& is, std::string& s, const std::string& list)
39  {
40  s.clear();
41  while (is.peek() && is && list.find(is.peek()) == list.npos) {
42  s += is.get();
43  }
44  return is;
45  }
46 
47  void read_vals(std::istream& is, char closing_char)
48  {
49  double val;
50  char next_char;
51  do {
52  is >> val;
53  if (!is) {
54  break;
55  }
56  vals_.push_back(val);
57  is >> next_char;
58  } while (next_char == ',');
59  if (!is || next_char != closing_char) {
60  std::cerr << "Error while reading values of parameter " << name_ << "\n"; // TODO: standardize
61  throw std::runtime_error("Cannot read parameter value"); // TODO: standardize
62  }
63  }
64 
65 public:
66  enum class type { range, vector, number, string };
67 
68  static const char* to_string(type t)
69  {
70  switch (t) {
71  case type::range: return "range";
72  case type::vector: return "vector";
73  case type::number: return "number";
74  case type::string: return "string";
75  default:
77  }
78  }
79 
80  std::string name_;
82  std::vector<double> vals_;
83  std::string str_;
84 
85  param() {}
86  param(std::istream& is)
87  {
88  is >> std::ws;
89  read_until(is, name_, " =");
90  char next_char;
91  is >> std::ws >> next_char;
92  if (next_char != '=') {
93  throw std::runtime_error("Cannot read parameter name"); // TODO: standardize
94  }
95  is >> std::ws;
96  next_char = is.peek();
97  if (next_char == '[') { // range
98  is.ignore();
100  read_vals(is, ']');
101  } else if (next_char == '(') { // vector
102  is.ignore();
104  read_vals(is, ')');
105  } else if (next_char == '"') { // string
106  is.ignore();
108  std::getline(is, str_, '"');
109  } else {
111  vals_.resize(1);
112  is >> vals_[0];
113  }
114  if (!is) {
115  std::cerr << "Error while reading value of parameter " << name_ << "\n"; // TODO: standardize
116  throw std::runtime_error("Cannot read parameter value"); // TODO: standardize
117  }
118  }
119 
120  friend class param_list;
121  static param_list read(std::istream& is, std::string fn_name_);
122 };
123 
125 {
126  std::unordered_map<std::string, param> m_;
127  const std::string fn_name_;
128 public:
129  param_list(std::string fn_name) : fn_name_(move(fn_name)) {}
130 
131  auto& operator[](const std::string& s)
132  {
133  return m_[s];
134  }
135 
136  bool Get(const std::string& name, param::type type, bool required, param& value)
137  {
138  auto it = m_.find(name);
139  if (it != end(m_)) {
140  auto& p = it->second;
141  if (p.type_ != type) {
142  throw std::runtime_error(fn_name_ + ": " + name + " parameter must be a " + param::to_string(type));
143  }
144  value = p;
145  return true;
146  }
147  if (required) {
148  throw std::runtime_error(fn_name_ + ": " + name + " is a required parameter");
149  }
150  return false;
151  }
152 
153  bool GenericGet(const std::string& name, bool required, param& value)
154  {
155  auto it = m_.find(name);
156  if (it != end(m_)) {
157  auto& p = it->second;
158  value = p;
159  return true;
160  }
161  if (required) {
162  throw std::runtime_error(fn_name_ + ": " + name + " is a required parameter");
163  }
164  return false;
165  }
166 };
167 
175 {
176 public:
177  double min_, max_, value_;
178 
179  AugmentationParam() = default;
180  AugmentationParam(const double min, const double max) : min_(min), max_(max) {}
181 
185  {
186  value_ = std::uniform_real_distribution<>(min_, max_)(re_);
187  }
188  static std::default_random_engine re_;
189 
190  static constexpr unsigned seed_min = std::numeric_limits<unsigned>::min();
191  static constexpr unsigned seed_max = std::numeric_limits<unsigned>::max();
192 
196  static void SetSeed(unsigned seed)
197  {
198  re_.seed(seed);
199  }
200 };
201 
207 {
208 public:
209  std::unordered_map<std::string, AugmentationParam> params_;
210 
215  void Apply(ecvl::Image& img, const ecvl::Image& gt = Image())
216  {
217  for (auto& x : params_) {
218  x.second.GenerateValue();
219  }
220  RealApply(img, gt);
221  }
222  virtual std::shared_ptr<Augmentation> Clone() const = 0;
223  virtual ~Augmentation() = default;
224 
225 private:
226  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) = 0;
227 };
228 #define DEFINE_AUGMENTATION_CLONE(class_name) std::shared_ptr<Augmentation> Clone() const override { return std::make_shared<class_name>(*this); }
229 
231 {
232  static std::shared_ptr<Augmentation> create(std::istream& is)
233  {
234  std::string name;
235  is >> name;
236  if (!is) {
238  }
239  return create(name, is);
240  }
241 
242  static std::shared_ptr<Augmentation> create(const std::string& name, std::istream& is);
243 };
244 
252 {
258  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
259  {
260  for (auto& x : augs_) {
261  x->Apply(img, gt);
262  }
263  }
264  std::vector<std::shared_ptr<Augmentation>> augs_;
265 public:
267 
268  template<typename ...Ts>
269  SequentialAugmentationContainer(Ts&&... t) : augs_({ std::make_shared<Ts>(std::forward<Ts>(t))... }) {}
270 
271  SequentialAugmentationContainer(std::vector<std::shared_ptr<Augmentation>> augs) : augs_(augs) {}
272 
274  {
275  for (const auto& a : other.augs_) {
276  augs_.emplace_back(a->Clone());
277  }
278  }
279 
281  {
282  while (true) {
283  std::string name;
284  is >> name;
285  if (!is) {
287  }
288  if (name == "end") {
289  break;
290  }
291  augs_.emplace_back(AugmentationFactory::create(name, is));
292  }
293  }
294 };
295 
304 {
310  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
311  {
312  int index = std::uniform_int_distribution<>(0, vsize(augs_) - 1)(AugmentationParam::re_);
313  if (params_["p"].value_ <= p_) {
314  augs_[index]->Apply(img, gt);
315  }
316  }
317  std::vector<std::shared_ptr<Augmentation>> augs_;
318  double p_;
319 public:
321 
322  template<typename ...Ts>
323  OneOfAugmentationContainer(double p, Ts&&... t) : p_(p), augs_({ std::make_shared<Ts>(std::forward<Ts>(t))... })
324  {
325  params_["p"] = AugmentationParam(0, 1);
326  }
327 
328  OneOfAugmentationContainer(double p, std::vector<std::shared_ptr<Augmentation>> augs) : p_(p), augs_(augs)
329  {
330  params_["p"] = AugmentationParam(0, 1);
331  }
332 
334  {
335  for (const auto& a : other.augs_) {
336  augs_.emplace_back(a->Clone());
337  }
338  p_ = other.p_;
339  }
340 
341  OneOfAugmentationContainer(std::istream& is)
342  {
343  param p;
344  try {
345  auto m = param::read(is, "OneOfAugmentationContainer");
346  if (m.Get("p", param::type::number, true, p)) {
347  p_ = p.vals_[0];
348  }
349  } catch (std::runtime_error&) {
350  std::cout << ECVL_ERROR_MSG "The first parameter in OneOfAugmentationContainer must be the probability p" << std::endl;
352  }
353 
354  while (true) {
355  std::string name;
356  is >> name;
357  if (!is) {
359  }
360  if (name == "end") {
361  break;
362  }
363  augs_.emplace_back(AugmentationFactory::create(name, is));
364  }
365  }
366 };
367 
368 InterpolationType StrToInterpolationType(const std::string& interp, const std::string& aug_name);
369 BorderType StrToBorderType(const std::string& interp, const std::string& aug_name);
370 
372 // Augmentations
374 
379 class AugRotate : public Augmentation
380 {
381  std::vector<double> center_;
382  double scale_;
383  InterpolationType interp_, gt_interp_;
384 
385  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
386  {
387  const auto angle = params_["angle"].value_;
388  Rotate2D(img, img, angle, center_, scale_, interp_);
389  if (!gt.IsEmpty()) {
390  Rotate2D(gt, const_cast<Image&>(gt), angle, center_, scale_, gt_interp_);
391  }
392  }
393 public:
395 
396 
405  AugRotate(const std::array<double, 2>& angle,
406  const std::vector<double>& center = {},
407  const double& scale = 1.,
410  : center_(center), scale_(scale), interp_(interp), gt_interp_(gt_interp)
411  {
412  params_["angle"] = AugmentationParam(angle[0], angle[1]);
413  }
414 
415  AugRotate(std::istream& is)
416  {
417  auto m = param::read(is, "AugRotate");
418  param p;
419 
420  m.Get("angle", param::type::range, true, p);
421  params_["angle"] = AugmentationParam(p.vals_[0], p.vals_[1]);
422 
423  if (m.Get("center", param::type::vector, false, p)) {
424  center_ = p.vals_;
425  }
426 
427  scale_ = 1.;
428  if (m.Get("scale", param::type::number, false, p)) {
429  scale_ = p.vals_[0];
430  }
431 
432  interp_ = InterpolationType::linear;
433  gt_interp_ = InterpolationType::nearest;
434 
435  if (m.Get("interp", param::type::string, false, p)) {
436  interp_ = StrToInterpolationType(p.str_, "AugRotate");
437  }
438  if (m.Get("gt_interp", param::type::string, false, p)) {
439  gt_interp_ = StrToInterpolationType(p.str_, "AugRotate");
440  }
441  }
442 };
443 
449 {
450  std::vector<int> dims_;
451  InterpolationType interp_, gt_interp_;
452 
453  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
454  {
455  ResizeDim(img, img, dims_, interp_);
456  if (!gt.IsEmpty()) {
457  ResizeDim(gt, const_cast<Image&>(gt), dims_, gt_interp_);
458  }
459  }
460 public:
462 
463 
469  AugResizeDim(const std::vector<int>& dims,
470  const InterpolationType& interp = InterpolationType::linear,
471  const InterpolationType& gt_interp = InterpolationType::nearest)
472  : dims_{ dims }, interp_(interp), gt_interp_(gt_interp) {}
473 
474  AugResizeDim(std::istream& is)
475  {
476  auto m = param::read(is, "AugResizeDim");
477  param p;
478 
479  m.Get("dims", param::type::vector, true, p);
480  for (const auto& x : p.vals_) {
481  dims_.emplace_back(static_cast<int>(x));
482  }
483 
484  interp_ = InterpolationType::linear;
485  gt_interp_ = InterpolationType::nearest;
486 
487  if (m.Get("interp", param::type::string, false, p)) {
488  interp_ = StrToInterpolationType(p.str_, "");
489  }
490  if (m.Get("gt_interp", param::type::string, false, p)) {
491  gt_interp_ = StrToInterpolationType(p.str_, "AugResizeDim");
492  }
493  }
494 };
495 
501 {
502  std::vector<double> scale_;
503  InterpolationType interp_, gt_interp_;
504 
505  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
506  {
507  ResizeScale(img, img, scale_, interp_);
508  if (!gt.IsEmpty()) {
509  ResizeScale(gt, const_cast<Image&>(gt), scale_, gt_interp_);
510  }
511  }
512 public:
514 
515 
521  AugResizeScale(const std::vector<double>& scale,
522  const InterpolationType& interp = InterpolationType::linear,
523  const InterpolationType& gt_interp = InterpolationType::nearest
524  ) : scale_{ scale }, interp_(interp), gt_interp_(gt_interp){}
525 
526  AugResizeScale(std::istream& is)
527  {
528  auto m = param::read(is, "AugResizeScale");
529  param p;
530 
531  m.Get("scale", param::type::vector, true, p);
532  scale_ = p.vals_;
533 
534  interp_ = InterpolationType::linear;
535  gt_interp_ = InterpolationType::nearest;
536 
537  if (m.Get("interp", param::type::string, false, p)) {
538  interp_ = StrToInterpolationType(p.str_, "AugResizeScale");
539  }
540  if (m.Get("gt_interp", param::type::string, false, p)) {
541  gt_interp_ = StrToInterpolationType(p.str_, "AugResizeScale");
542  }
543  }
544 };
545 
550 class AugFlip : public Augmentation
551 {
552  double p_;
553 
554  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
555  {
556  const auto p = params_["p"].value_;
557  if (p <= p_) {
558  Flip2D(img, img);
559  if (!gt.IsEmpty()) {
560  Flip2D(gt, const_cast<Image&>(gt));
561  }
562  }
563  }
564 public:
566 
567 
571  AugFlip(double p = 0.5) : p_{ p }
572  {
573  params_["p"] = AugmentationParam(0, 1);
574  }
575 
576  AugFlip(std::istream& is) : AugFlip()
577  {
578  auto m = param::read(is, "AugFlip");
579  param p;
580 
581  if (m.Get("p", param::type::number, false, p)) {
582  p_ = p.vals_[0];
583  }
584  }
585 };
586 
591 class AugMirror : public Augmentation
592 {
593  double p_;
594 
595  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
596  {
597  const auto p = params_["p"].value_;
598  if (p <= p_) {
599  Mirror2D(img, img);
600  if (!gt.IsEmpty()) {
601  Mirror2D(gt, const_cast<Image&>(gt));
602  }
603  }
604  }
605 public:
607 
608 
612  AugMirror(double p = 0.5) : p_{ p }
613  {
614  params_["p"] = AugmentationParam(0, 1);
615  }
616 
617  AugMirror(std::istream& is) : AugMirror()
618  {
619  auto m = param::read(is, "AugMirror");
620  param p;
621 
622  if (m.Get("p", param::type::number, false, p)) {
623  p_ = p.vals_[0];
624  }
625  }
626 };
627 
633 {
634  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
635  {
636  const auto sigma = params_["sigma"].value_;
637  GaussianBlur(img, img, sigma);
638  }
639 public:
641 
642 
646  AugGaussianBlur(const std::array<double, 2>& sigma)
647  {
648  params_["sigma"] = AugmentationParam(sigma[0], sigma[1]);
649  }
650 
651  AugGaussianBlur(std::istream& is)
652  {
653  auto m = param::read(is, "AugGaussianBlur");
654  param p;
655 
656  m.Get("sigma", param::type::range, true, p);
657  params_["sigma"] = AugmentationParam(p.vals_[0], p.vals_[1]);
658  }
659 };
660 
666 {
667  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
668  {
669  const auto std_dev = params_["std_dev"].value_;
670  AdditiveLaplaceNoise(img, img, std_dev);
671  }
672 public:
674 
675 
680  AugAdditiveLaplaceNoise(const std::array<double, 2>& std_dev)
681  {
682  params_["std_dev"] = AugmentationParam(std_dev[0], std_dev[1]);
683  }
684 
685  AugAdditiveLaplaceNoise(std::istream& is)
686  {
687  auto m = param::read(is, "AugAdditiveLaplaceNoise");
688  param p;
689 
690  m.Get("std_dev", param::type::range, true, p);
691  params_["std_dev"] = AugmentationParam(p.vals_[0], p.vals_[1]);
692  }
693 };
694 
700 {
701  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
702  {
703  const auto lambda = params_["lambda"].value_;
704  AdditivePoissonNoise(img, img, lambda);
705  }
706 public:
708 
709 
714  AugAdditivePoissonNoise(const std::array<double, 2>& lambda)
715  {
716  params_["lambda"] = AugmentationParam(lambda[0], lambda[1]);
717  }
718 
719  AugAdditivePoissonNoise(std::istream& is)
720  {
721  auto m = param::read(is, "AugAdditivePoissonNoise");
722  param p;
723 
724  m.Get("lambda", param::type::range, true, p);
725  params_["lambda"] = AugmentationParam(p.vals_[0], p.vals_[1]);
726  }
727 };
728 
734 {
735  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
736  {
737  const auto gamma = params_["gamma"].value_;
738  GammaContrast(img, img, gamma);
739  }
740 public:
742 
743 
748  AugGammaContrast(const std::array<double, 2>& gamma)
749  {
750  params_["gamma"] = AugmentationParam(gamma[0], gamma[1]);
751  }
752 
753  AugGammaContrast(std::istream& is)
754  {
755  auto m = param::read(is, "AugGammaContrast");
756  param p;
757 
758  m.Get("gamma", param::type::range, true, p);
759  params_["gamma"] = AugmentationParam(p.vals_[0], p.vals_[1]);
760  }
761 };
762 
768 {
769  double per_channel_;
770 
771  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
772  {
773  const auto p = params_["p"].value_;
774  const auto drop_size = params_["drop_size"].value_;
775  const bool per_channel = params_["per_channel"].value_ <= per_channel_ ? true : false;
776  CoarseDropout(img, img, p, drop_size, per_channel);
777  }
778 public:
780 
781 
787  AugCoarseDropout(const std::array<double, 2>& p, const std::array<double, 2>& drop_size, const double& per_channel) : per_channel_(per_channel)
788  {
789  assert(per_channel >= 0 && per_channel <= 1);
790  params_["p"] = AugmentationParam(p[0], p[1]);
791  params_["drop_size"] = AugmentationParam(drop_size[0], drop_size[1]);
792  params_["per_channel"] = AugmentationParam(0, 1);
793  }
794  AugCoarseDropout(std::istream& is)
795  {
796  auto m = param::read(is, "AugCoarseDropout");
797  param p;
798 
799  m.Get("p", param::type::range, true, p);
800  params_["p"] = AugmentationParam(p.vals_[0], p.vals_[1]);
801 
802  m.Get("drop_size", param::type::range, true, p);
803  params_["drop_size"] = AugmentationParam(p.vals_[0], p.vals_[1]);
804 
805  m.Get("per_channel", param::type::number, true, p);
806  params_["per_channel"] = AugmentationParam(0, 1);
807  per_channel_ = p.vals_[0];
808  }
809 };
810 
816 {
817  double p_;
818 
819  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
820  {
821  const auto p = params_["p"].value_;
822  if (p <= p_) {
823  Transpose(img, img);
824  if (!gt.IsEmpty()) {
825  Transpose(gt, const_cast<Image&>(gt));
826  }
827  }
828  }
829 public:
831 
832 
836  AugTranspose(double p = 0.5) : p_{ p }
837  {
838  params_["p"] = AugmentationParam(0, 1);
839  }
840 
841  AugTranspose(std::istream& is) : AugTranspose()
842  {
843  auto m = param::read(is, "AugTranspose");
844  param p;
845 
846  if (m.Get("p", param::type::number, false, p)) {
847  p_ = p.vals_[0];
848  }
849  }
850 };
851 
857 {
858  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
859  {
860  const auto beta = params_["beta"].value_;
861  Add(img, beta, img);
862  }
863 public:
865 
866 
871  AugBrightness(const std::array<double, 2>& beta)
872  {
873  params_["beta"] = AugmentationParam(beta[0], beta[1]);
874  }
875 
876  AugBrightness(std::istream& is)
877  {
878  auto m = param::read(is, "AugBrightness");
879 
880  param p;
881 
882  m.Get("beta", param::type::range, true, p);
883  params_["beta"] = AugmentationParam(p.vals_[0], p.vals_[1]);
884  }
885 };
886 
892 {
893  std::array<float, 2> distort_limit_;
894  InterpolationType interp_;
895  BorderType border_type_;
896  int border_value_;
897 
898  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
899  {
900  const auto num_steps = params_["num_steps"].value_;
901  const auto seed = params_["seed"].value_;
902  GridDistortion(img, img, static_cast<int>(num_steps), distort_limit_, interp_, border_type_,
903  border_value_, static_cast<unsigned>(seed));
904  if (!gt.IsEmpty()) {
905  GridDistortion(gt, const_cast<Image&>(gt), static_cast<int>(num_steps), distort_limit_,
906  interp_, border_type_, border_value_, static_cast<unsigned>(seed));
907  }
908  }
909 public:
911 
912 
920  AugGridDistortion(const std::array<int, 2>& num_steps,
921  const std::array<float, 2>& distort_limit,
922  const InterpolationType& interp = InterpolationType::linear,
923  const BorderType& border_type = BorderType::BORDER_REFLECT_101,
924  const int& border_value = 0)
925  : distort_limit_(distort_limit), interp_(interp), border_type_(border_type), border_value_(border_value)
926  {
927  params_["num_steps"] = AugmentationParam(num_steps[0], num_steps[1]);
929  }
930 
931  AugGridDistortion(std::istream& is)
932  {
933  auto m = param::read(is, "AugGridDistortion");
934 
935  param p;
936 
937  m.Get("num_steps", param::type::range, true, p);
938  params_["num_steps"] = AugmentationParam(p.vals_[0], p.vals_[1]);
939 
940  // seed is managed by AugmentationParam
942 
943  m.Get("distort_limit", param::type::range, true, p);
944  distort_limit_ = { static_cast<float>(p.vals_[0]), static_cast<float>(p.vals_[1]) };
945 
946  interp_ = InterpolationType::linear;
947  if (m.Get("interp", param::type::string, false, p)) {
948  interp_ = StrToInterpolationType(p.str_, "AugGridDistortion");
949  }
950 
951  border_type_ = BorderType::BORDER_REFLECT_101;
952  if (m.Get("border_type", param::type::string, false, p)) {
953  if (p.str_ == "constant") {
954  border_type_ = BorderType::BORDER_CONSTANT;
955  } else if (p.str_ == "replicate") {
956  border_type_ = BorderType::BORDER_REPLICATE;
957  } else if (p.str_ == "reflect") {
958  border_type_ = BorderType::BORDER_REFLECT;
959  } else if (p.str_ == "wrap") {
960  border_type_ = BorderType::BORDER_WRAP;
961  } else if (p.str_ == "reflect_101") {
962  border_type_ = BorderType::BORDER_REFLECT_101;
963  } else if (p.str_ == "transparent") {
964  border_type_ = BorderType::BORDER_TRANSPARENT;
965  } else {
966  throw std::runtime_error("AugGridDistortion: invalid border type"); // TODO: standardize
967  }
968  }
969 
970  border_value_ = 0;
971  m.Get("border_value", param::type::number, false, p);
972  border_value_ = static_cast<int>(p.vals_[0]);
973  }
974 };
975 
981 {
982  InterpolationType interp_;
983  BorderType border_type_;
984  int border_value_;
985 
986  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
987  {
988  const auto alpha = params_["alpha"].value_;
989  const auto sigma = params_["sigma"].value_;
990  const auto seed = params_["seed"].value_;
991  ElasticTransform(img, img, alpha, sigma, interp_, border_type_, border_value_, static_cast<unsigned>(seed));
992  if (!gt.IsEmpty()) {
993  ElasticTransform(gt, const_cast<Image&>(gt), alpha, sigma, interp_,
994  border_type_, border_value_, static_cast<unsigned>(seed));
995  }
996  }
997 public:
999 
1000 
1008  AugElasticTransform(const std::array<double, 2>& alpha,
1009  const std::array<double, 2>& sigma,
1010  const InterpolationType& interp = InterpolationType::linear,
1011  const BorderType& border_type = BorderType::BORDER_REFLECT_101,
1012  const int& border_value = 0)
1013  : interp_(interp), border_type_(border_type), border_value_(border_value)
1014  {
1015  params_["alpha"] = AugmentationParam(alpha[0], alpha[1]);
1016  params_["sigma"] = AugmentationParam(sigma[0], sigma[1]);
1018  }
1019 
1020  AugElasticTransform(std::istream& is)
1021  {
1022  auto m = param::read(is, "AugElasticTransform");
1023 
1024  param p;
1025 
1026  m.Get("alpha", param::type::range, true, p);
1027  params_["alpha"] = AugmentationParam(p.vals_[0], p.vals_[1]);
1028 
1029  m.Get("sigma", param::type::range, true, p);
1030  params_["sigma"] = AugmentationParam(p.vals_[0], p.vals_[1]);
1031 
1032  // seed is managed by AugmentationParam
1034 
1035  interp_ = InterpolationType::linear;
1036  if (m.Get("interp", param::type::string, false, p)) {
1037  interp_ = StrToInterpolationType(p.str_, "AugElasticTransform");
1038  }
1039 
1040  border_type_ = BorderType::BORDER_REFLECT_101;
1041  if (m.Get("border_type", param::type::string, false, p)) {
1042  if (p.str_ == "constant") {
1043  border_type_ = BorderType::BORDER_CONSTANT;
1044  } else if (p.str_ == "replicate") {
1045  border_type_ = BorderType::BORDER_REPLICATE;
1046  } else if (p.str_ == "reflect") {
1047  border_type_ = BorderType::BORDER_REFLECT;
1048  } else if (p.str_ == "wrap") {
1049  border_type_ = BorderType::BORDER_WRAP;
1050  } else if (p.str_ == "reflect_101") {
1051  border_type_ = BorderType::BORDER_REFLECT_101;
1052  } else if (p.str_ == "transparent") {
1053  border_type_ = BorderType::BORDER_TRANSPARENT;
1054  } else {
1055  throw std::runtime_error("AugGridDistortion: invalid border type"); // TODO: standardize
1056  }
1057  }
1058 
1059  border_value_ = 0;
1060  m.Get("border_value", param::type::number, false, p);
1061  border_value_ = static_cast<int>(p.vals_[0]);
1062  }
1063 };
1064 
1070 {
1071  std::array<float, 2> distort_limit_;
1072  std::array<float, 2> shift_limit_;
1073  InterpolationType interp_;
1074  BorderType border_type_;
1075  int border_value_;
1076 
1077  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1078  {
1079  const auto seed = params_["seed"].value_;
1080  OpticalDistortion(img, img, distort_limit_, shift_limit_, interp_, border_type_,
1081  border_value_, static_cast<unsigned>(seed));
1082  if (!gt.IsEmpty()) {
1083  OpticalDistortion(gt, const_cast<Image&>(gt), distort_limit_, shift_limit_, interp_, border_type_,
1084  border_value_, static_cast<unsigned>(seed));
1085  }
1086  }
1087 public:
1089 
1090 
1098  AugOpticalDistortion(const std::array<float, 2>& distort_limit,
1099  const std::array<float, 2>& shift_limit,
1100  const InterpolationType& interp = InterpolationType::linear,
1101  const BorderType& border_type = BorderType::BORDER_REFLECT_101,
1102  const int& border_value = 0)
1103  : distort_limit_(distort_limit), shift_limit_(shift_limit), interp_(interp), border_type_(border_type), border_value_(border_value)
1104  {
1106  }
1107 
1108  AugOpticalDistortion(std::istream& is)
1109  {
1110  auto m = param::read(is, "AugOpticalDistortion");
1111 
1112  param p;
1113 
1114  // seed is managed by AugmentationParam
1116 
1117  m.Get("distort_limit", param::type::range, true, p);
1118  distort_limit_ = { static_cast<float>(p.vals_[0]), static_cast<float>(p.vals_[1]) };
1119 
1120  m.Get("shift_limit", param::type::range, true, p);
1121  shift_limit_ = { static_cast<float>(p.vals_[0]), static_cast<float>(p.vals_[1]) };
1122 
1123  interp_ = InterpolationType::linear;
1124  if (m.Get("interp", param::type::string, false, p)) {
1125  interp_ = StrToInterpolationType(p.str_, "AugOpticalDistortion");
1126  }
1127 
1128  border_type_ = BorderType::BORDER_REFLECT_101;
1129  if (m.Get("border_type", param::type::string, false, p)) {
1130  if (p.str_ == "constant") {
1131  border_type_ = BorderType::BORDER_CONSTANT;
1132  } else if (p.str_ == "replicate") {
1133  border_type_ = BorderType::BORDER_REPLICATE;
1134  } else if (p.str_ == "reflect") {
1135  border_type_ = BorderType::BORDER_REFLECT;
1136  } else if (p.str_ == "wrap") {
1137  border_type_ = BorderType::BORDER_WRAP;
1138  } else if (p.str_ == "reflect_101") {
1139  border_type_ = BorderType::BORDER_REFLECT_101;
1140  } else if (p.str_ == "transparent") {
1141  border_type_ = BorderType::BORDER_TRANSPARENT;
1142  } else {
1143  throw std::runtime_error("AugGridDistortion: invalid border type"); // TODO: standardize
1144  }
1145  }
1146 
1147  border_value_ = 0;
1148  m.Get("border_value", param::type::number, false, p);
1149  border_value_ = static_cast<int>(p.vals_[0]);
1150  }
1151 };
1152 
1157 class AugSalt : public Augmentation
1158 {
1159  double per_channel_;
1160 
1161  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1162  {
1163  const auto p = params_["p"].value_;
1164  const auto seed = params_["seed"].value_;
1165  const bool per_channel = params_["per_channel"].value_ <= per_channel_ ? true : false;
1166  Salt(img, img, p, per_channel, static_cast<unsigned>(seed));
1167  }
1168 public:
1170 
1171 
1176  AugSalt(const std::array<double, 2>& p, const double& per_channel) : per_channel_(per_channel)
1177  {
1178  assert(per_channel >= 0 && per_channel <= 1);
1179  params_["p"] = AugmentationParam(p[0], p[1]);
1180  params_["per_channel"] = AugmentationParam(0, 1);
1182  }
1183  AugSalt(std::istream& is)
1184  {
1185  auto m = param::read(is, "AugSalt");
1186  param p;
1187 
1188  // seed is managed by AugmentationParam
1190 
1191  m.Get("p", param::type::range, true, p);
1192  params_["p"] = AugmentationParam(p.vals_[0], p.vals_[1]);
1193 
1194  m.Get("per_channel", param::type::number, true, p);
1195  params_["per_channel"] = AugmentationParam(0, 1);
1196  per_channel_ = p.vals_[0];
1197  }
1198 };
1199 
1204 class AugPepper : public Augmentation
1205 {
1206  double per_channel_;
1207 
1208  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1209  {
1210  const auto p = params_["p"].value_;
1211  const auto seed = params_["seed"].value_;
1212  const bool per_channel = params_["per_channel"].value_ <= per_channel_ ? true : false;
1213  Pepper(img, img, p, per_channel, static_cast<unsigned>(seed));
1214  }
1215 public:
1217 
1218 
1223  AugPepper(const std::array<double, 2>& p, const double& per_channel) : per_channel_(per_channel)
1224  {
1225  assert(per_channel >= 0 && per_channel <= 1);
1226  params_["p"] = AugmentationParam(p[0], p[1]);
1227  params_["per_channel"] = AugmentationParam(0, 1);
1229  }
1230  AugPepper(std::istream& is)
1231  {
1232  auto m = param::read(is, "AugPepper");
1233  param p;
1234 
1235  // seed is managed by AugmentationParam
1237 
1238  m.Get("p", param::type::range, true, p);
1239  params_["p"] = AugmentationParam(p.vals_[0], p.vals_[1]);
1240 
1241  m.Get("per_channel", param::type::number, true, p);
1242  params_["per_channel"] = AugmentationParam(0, 1);
1243  per_channel_ = p.vals_[0];
1244  }
1245 };
1246 
1252 {
1253  double per_channel_;
1254 
1255  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1256  {
1257  const auto p = params_["p"].value_;
1258  const auto seed = params_["seed"].value_;
1259  const bool per_channel = params_["per_channel"].value_ <= per_channel_ ? true : false;
1260  SaltAndPepper(img, img, p, per_channel, static_cast<unsigned>(seed));
1261  }
1262 public:
1264 
1265 
1270  AugSaltAndPepper(const std::array<double, 2>& p, const double& per_channel) : per_channel_(per_channel)
1271  {
1272  assert(per_channel >= 0 && per_channel <= 1);
1273  params_["p"] = AugmentationParam(p[0], p[1]);
1274  params_["per_channel"] = AugmentationParam(0, 1);
1276  }
1277  AugSaltAndPepper(std::istream& is)
1278  {
1279  auto m = param::read(is, "AugSaltAndPepper");
1280  param p;
1281 
1282  // seed is managed by AugmentationParam
1284 
1285  m.Get("p", param::type::range, true, p);
1286  params_["p"] = AugmentationParam(p.vals_[0], p.vals_[1]);
1287 
1288  m.Get("per_channel", param::type::number, true, p);
1289  params_["per_channel"] = AugmentationParam(0, 1);
1290  per_channel_ = p.vals_[0];
1291  }
1292 };
1293 
1299 {
1300  double mean_ = 0., std_ = 1.;
1301 
1302  std::vector<double> ch_mean_;
1303  std::vector<double> ch_std_;
1304 
1305  bool per_channel_;
1306 
1307  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1308  {
1309  if (per_channel_) {
1310  Normalize(img, img, ch_mean_, ch_std_);
1311  } else {
1312  Normalize(img, img, mean_, std_);
1313  }
1314  }
1315 public:
1317 
1318 
1323  AugNormalize(const double& mean, const double& std) : mean_(mean), std_(std), per_channel_(false) {}
1324 
1330  AugNormalize(const std::vector<double>& mean, const std::vector<double>& std) : ch_mean_(mean), ch_std_(std), per_channel_(true) {}
1331 
1332  AugNormalize(std::istream& is)
1333  {
1334  auto m = param::read(is, "AugNormalize");
1335  param p;
1336 
1337  m.GenericGet("mean", true, p);
1338  if (p.type_ == param::type::number) {
1339  mean_ = p.vals_[0];
1340  per_channel_ = false;
1341  } else if (p.type_ == param::type::vector) {
1342  ch_mean_ = p.vals_;
1343  per_channel_ = true;
1344  } else {
1345  throw std::runtime_error("AugNormalize: invalid mean type");
1346  }
1347 
1348  if (per_channel_ == false) {
1349  m.Get("std", param::type::number, true, p);
1350  std_ = p.vals_[0];
1351  } else {
1352  m.Get("std", param::type::vector, true, p);
1353  ch_std_ = p.vals_;
1354  }
1355  }
1356 };
1357 
1363 {
1364  std::vector<int> size_;
1365  bool infer_;
1366 
1367  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1368  {
1369  std::vector<int> new_size = size_;
1370  if (infer_) {
1371  // TODO: 3D implementation
1372  new_size = std::vector<int>(2, std::min(img.Width(), img.Height()));
1373  }
1374  CenterCrop(img, img, new_size);
1375  if (!gt.IsEmpty()) {
1376  CenterCrop(gt, const_cast<Image&>(gt), new_size);
1377  }
1378  }
1379 public:
1381 
1382 
1387  AugCenterCrop() : infer_{ true } {}
1388 
1393  AugCenterCrop(const std::vector<int>& size) : size_{ size }, infer_{ false } {}
1394 
1395  AugCenterCrop(std::istream& is)
1396  {
1397  auto m = param::read(is, "AugCenterCrop");
1398  param p;
1399 
1400  if (m.Get("size", param::type::vector, false, p)) {
1401  for (const auto& x : p.vals_) {
1402  size_.emplace_back(static_cast<int>(x));
1403  }
1404  infer_ = false;
1405  } else {
1406  infer_ = true;
1407  }
1408  }
1409 };
1410 
1418 {
1419  double divisor_, divisor_gt_;
1420 
1421  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1422  {
1424  img.Div(divisor_);
1425 
1426  if (!gt.IsEmpty()) {
1427  const_cast<Image&>(gt).ConvertTo(DataType::float32);
1428  const_cast<Image&>(gt).Div(divisor_gt_);
1429  }
1430  }
1431 public:
1433 
1434 
1439  AugToFloat32(const double& divisor = 1., const double& divisor_gt = 1.) : divisor_{ divisor }, divisor_gt_{ divisor_gt } {}
1440  AugToFloat32(std::istream& is)
1441  {
1442  auto m = param::read(is, "AugToFloat32");
1443  param p;
1444 
1445  m.Get("divisor", param::type::number, false, p);
1446  divisor_ = p.vals_[0];
1447  m.Get("divisor_gt", param::type::number, false, p);
1448  divisor_gt_ = p.vals_[0];
1449  }
1450 };
1451 
1459 {
1460  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1461  {
1462  img.Div(255);
1463 
1464  if (!gt.IsEmpty()) {
1465  const_cast<Image&>(gt).Div(255);
1466  }
1467  }
1468 public:
1470 
1471 
1473  AugDivBy255(std::istream& is) {}
1474 };
1475 
1480 class AugScaleTo : public Augmentation
1481 {
1482  double new_min_, new_max_;
1483 
1484  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1485  {
1486  ScaleTo(img, img, new_min_, new_max_);
1487  }
1488 public:
1490 
1491 
1496  AugScaleTo(const double& new_min, const double& new_max) : new_min_{ new_min }, new_max_{ new_max } {}
1497  AugScaleTo(std::istream& is)
1498  {
1499  auto m = param::read(is, "AugScaleTo");
1500  param p;
1501 
1502  m.Get("new_min", param::type::number, true, p);
1503  new_min_ = p.vals_[0];
1504 
1505  m.Get("new_max", param::type::number, true, p);
1506  new_max_ = p.vals_[0];
1507  }
1508 };
1509 
1515 {
1516  std::vector<int> size_;
1517  BorderType border_type_;
1518  int border_value_;
1519 
1520  virtual void RealApply(ecvl::Image& img, const ecvl::Image& gt = Image()) override
1521  {
1522  const auto seed = params_["seed"].value_;
1523  RandomCrop(img, img, size_, true, border_type_, border_value_, static_cast<unsigned>(seed));
1524  if (!gt.IsEmpty()) {
1525  RandomCrop(gt, const_cast<Image&>(gt), size_, true, border_type_, border_value_, static_cast<unsigned>(seed));
1526  }
1527  }
1528 public:
1530 
1531 
1537  AugRandomCrop(const std::vector<int>& size, BorderType border_type = BorderType::BORDER_CONSTANT, const int& border_value = 0) :
1538  size_{ size }, border_type_{ border_type }, border_value_{ border_value }
1539  {
1540  params_["seed"] = AugmentationParam(AugmentationParam::seed_min, AugmentationParam::seed_max);
1541  }
1542 
1543  AugRandomCrop(std::istream& is)
1544  {
1545  auto m = param::read(is, "AugRandomCrop");
1546  param p;
1547 
1548  m.Get("size", param::type::vector, true, p);
1549  for (const auto& x : p.vals_) {
1550  size_.emplace_back(static_cast<int>(x));
1551  }
1552 
1553  m.Get("border_type", param::type::string, false, p);
1554  border_type_ = StrToBorderType(p.str_, "AugRandomCrop");
1555 
1556  m.Get("border_value", param::type::number, false, p);
1557  border_value_ = static_cast<int>(p.vals_[0]);
1558 
1560  }
1561 };
1562 } // namespace ecvl
1563 
1564 #endif // AUGMENTATIONS_H_
AugCenterCrop(const std::vector< int > &size)
AugCenterCrop constructor.
Augmentation wrapper for ecvl::Flip2D.
Augmentation wrapper for ecvl::Normalize.
AugmentationParam(const double min, const double max)
void ResizeDim(const ecvl::Image &src, ecvl::Image &dst, const std::vector< int > &newdims, InterpolationType interp=InterpolationType::linear)
Resizes an Image to the specified dimensions.
Image class.
Definition: image.h:72
void CoarseDropout(const Image &src, Image &dst, double p, double drop_size, bool per_channel)
Sets rectangular areas within an Image to zero.
void GridDistortion(const Image &src, Image &dst, int num_steps=5, const std::array< float, 2 > &distort_limit={ -0.3f, 0.3f }, InterpolationType interp=InterpolationType::linear, BorderType border_type=BorderType::BORDER_REFLECT_101, const int &border_value=0, const unsigned seed=std::default_random_engine::default_seed)
Randomly stretch or reduce each cell of the grid in which the input Image is divided into....
void ScaleTo(const Image &src, Image &dst, const double &new_min, const double &new_max)
Linearly scale an Image into a new range.
BorderType StrToBorderType(const std::string &interp, const std::string &aug_name)
OneOfAugmentationContainer(double p, std::vector< std::shared_ptr< Augmentation >> augs)
Augmentation wrapper for ecvl::ElasticTransform.
int Width() const
Returns the width of Image.
Definition: image.h:483
AugBrightness(std::istream &is)
AugRotate(std::istream &is)
aaaaaa|abcdefgh|hhhhhhh
Augmentation wrapper for ecvl::GridDistortion.
OneOfAugmentationContainer.
bool GenericGet(const std::string &name, bool required, param &value)
AugResizeScale(std::istream &is)
AugDivBy255(std::istream &is)
int vsize(const std::vector< T > &v)
Definition: image.h:34
void RandomCrop(const Image &src, Image &dst, const std::vector< int > &size, bool pad_if_needed=false, BorderType border_type=BorderType::BORDER_CONSTANT, const int &border_value=0, const unsigned seed=std::default_random_engine::default_seed)
Crop the source Image at a random location with the size specified.
param(std::istream &is)
Definition: augmentations.h:86
void CenterCrop(const ecvl::Image &src, ecvl::Image &dst, const std::vector< int > &size)
Crops the given image at the center.
bool Get(const std::string &name, param::type type, bool required, param &value)
SequentialAugmentationContainer(std::vector< std::shared_ptr< Augmentation >> augs)
AugMirror(std::istream &is)
AugToFloat32(std::istream &is)
AugElasticTransform(std::istream &is)
SequentialAugmentationContainer.
std::unordered_map< std::string, AugmentationParam > params_
AugFlip(std::istream &is)
InterpolationType
Enum class representing the ECVL interpolation types.
Definition: imgproc.h:37
void AdditiveLaplaceNoise(const Image &src, Image &dst, double std_dev)
Adds Laplace distributed noise to an Image.
Augmentation ToFloat32.
void Add(const Image &src1, const Image &src2, Image &dst, DataType dst_type=DataType::none, bool saturate=true)
Adds two source Images storing the result into a destination Image.
Definition: arithmetic.h:69
AugTranspose(std::istream &is)
void Transpose(const Image &src, Image &dst)
Swap rows and columns of an Image.
AugAdditivePoissonNoise(std::istream &is)
int Height() const
Returns the height of Image.
Definition: image.h:493
Augmentation wrapper for ecvl::Mirror2D.
static void SetSeed(unsigned seed)
Set a fixed seed for the random generated values. Useful to reproduce experiments with same augmentat...
iiiiii|abcdefgh|iiiiiii with some specified i
void Salt(const Image &src, Image &dst, double p, bool per_channel=false, const unsigned seed=std::default_random_engine::default_seed)
Adds salt noise (white pixels) to an Image.
void ResizeScale(const ecvl::Image &src, ecvl::Image &dst, const std::vector< double > &scales, InterpolationType interp=InterpolationType::linear)
Resizes an Image by scaling the dimensions to a given scale factor.
AugGridDistortion(std::istream &is)
cdefgh|abcdefgh|abcdefg
Augmentation CenterCrop wrapper for ecvl::CenterCrop.
#define DEFINE_AUGMENTATION_CLONE(class_name)
SequentialAugmentationContainer(const SequentialAugmentationContainer &other)
static std::shared_ptr< Augmentation > create(std::istream &is)
void GaussianBlur(const Image &src, Image &dst, int sizeX, int sizeY, double sigmaX, double sigmaY=0)
Blurs an Image using a Gaussian kernel.
Augmentation wrapper for ecvl::AdditivePoissonNoise.
AugRandomCrop(std::istream &is)
std::string name_
Definition: augmentations.h:80
void Normalize(const Image &src, Image &dst, const double &mean, const double &std)
Normalize Image image with mean and standard deviation.
fedcba|abcdefgh|hgfedcb
void ConvertTo(DataType dtype, bool saturate=true)
Convert Image to another DataType.
Definition: image.h:557
static std::default_random_engine re_
BorderType
Enum class representing the ECVL border types.
Definition: imgproc.h:363
Definition: any.h:69
void SaltAndPepper(const Image &src, Image &dst, double p, bool per_channel=false, const unsigned seed=std::default_random_engine::default_seed)
Adds salt and pepper noise (white and black pixels) to an Image. White and black pixels are equally l...
AugCoarseDropout(std::istream &is)
Augmentations parameters.
Augmentation wrapper for ecvl::Transpose.
AugNormalize(const std::vector< double > &mean, const std::vector< double > &std)
AugNormalize constructor with separate statistics for each channel.
static param_list read(std::istream &is, std::string fn_name_)
param_list(std::string fn_name)
InterpolationType StrToInterpolationType(const std::string &interp, const std::string &aug_name)
#define ECVL_ERROR_NOT_REACHABLE_CODE
AugGaussianBlur(std::istream &is)
void AdditivePoissonNoise(const Image &src, Image &dst, double lambda)
Adds Poisson distributed noise to an Image.
void Rotate2D(const ecvl::Image &src, ecvl::Image &dst, double angle, const std::vector< double > &center={}, double scale=1.0, InterpolationType interp=InterpolationType::linear)
Rotates an Image.
auto & operator[](const std::string &s)
static constexpr unsigned seed_min
Augmentation wrapper for ecvl::GaussianBlur.
void Div(const T &rhs, bool saturate=true)
In-place division.
Definition: image.h:544
Augmentation wrapper for ecvl::AdditiveLaplaceNoise.
Augmentation wrapper for ecvl::Pepper.
#define ECVL_ERROR_MSG
void OpticalDistortion(const Image &src, Image &dst, const std::array< float, 2 > &distort_limit={ -0.3f, 0.3f }, const std::array< float, 2 > &shift_limit={ -0.1f, 0.1f }, InterpolationType interp=InterpolationType::linear, BorderType border_type=BorderType::BORDER_REFLECT_101, const int &border_value=0, const unsigned seed=std::default_random_engine::default_seed)
Barrel / pincushion distortion. Based on https://github.com/albumentations-team/albumentations/blob/m...
AugAdditiveLaplaceNoise(std::istream &is)
std::string str_
Definition: augmentations.h:83
Augmentation wrapper for ecvl::CoarseDropout.
AugCenterCrop(std::istream &is)
AugSalt(std::istream &is)
OneOfAugmentationContainer(double p, Ts &&... t)
void Apply(ecvl::Image &img, const ecvl::Image &gt=Image())
Generate the random value for each parameter and call the specialized augmentation functions.
OneOfAugmentationContainer(std::istream &is)
Augmentation wrapper for ecvl::Rotate2D.
Augmentation wrapper for ecvl::ResizeDim.
AugGammaContrast(std::istream &is)
Abstract class which represent a generic Augmentation function.
AugSaltAndPepper(std::istream &is)
SequentialAugmentationContainer(std::istream &is)
void GammaContrast(const Image &src, Image &dst, double gamma)
Adjust contrast by scaling each pixel value X to 255 * ((X/255) ** gamma).
AugResizeDim(std::istream &is)
Augmentation wrapper for brightness adjustment.
static constexpr unsigned seed_max
static const char * to_string(type t)
Definition: augmentations.h:68
void Flip2D(const ecvl::Image &src, ecvl::Image &dst)
Flips an Image.
virtual ~Augmentation()=default
Augmentation wrapper for ecvl::RandomCrop.
virtual std::shared_ptr< Augmentation > Clone() const =0
AugNormalize(std::istream &is)
Augmentation wrapper for ecvl::SaltAndPepper.
Augmentation DivBy255.
AugOpticalDistortion(std::istream &is)
std::vector< double > vals_
Definition: augmentations.h:82
#define ECVL_ERROR_AUGMENTATION_NAME
Definition: augmentations.h:31
Augmentation wrapper for ecvl::ScaleTo.
void Pepper(const Image &src, Image &dst, double p, bool per_channel=false, const unsigned seed=std::default_random_engine::default_seed)
Adds pepper noise (black pixels) to an Image.
void GenerateValue()
Generate the random value between min_ and max_.
AugPepper(std::istream &is)
Augmentation wrapper for ecvl::GammaContrast.
void ElasticTransform(const Image &src, Image &dst, double alpha=34., double sigma=4., InterpolationType interp=InterpolationType::linear, BorderType border_type=BorderType::BORDER_REFLECT_101, const int &border_value=0, const unsigned seed=std::default_random_engine::default_seed)
Elastic deformation of input Image. Based on https://github.com/albumentations-team/albumentations/bl...
Augmentation wrapper for ecvl::OpticalDistortion.
void Mirror2D(const ecvl::Image &src, ecvl::Image &dst)
Mirrors an Image.
OneOfAugmentationContainer(const OneOfAugmentationContainer &other)
Augmentation wrapper for ecvl::Salt.
#define ECVL_ERROR_AUGMENTATION_FORMAT
Definition: augmentations.h:32
Augmentation wrapper for ecvl::ResizeScale.
AugScaleTo(std::istream &is)