SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MaskedImage.h
Go to the documentation of this file.
1 
17 /*
18 * MaskedImage.h
19 *
20 * Created on: Feb 13, 2020
21 * Author: Alejandro Alvarez Ayllon
22 */
23 
24 #ifndef SEFRAMEWORK_SEFRAMEWORK_IMAGE_MASKEDIMAGE_H_
25 #define SEFRAMEWORK_SEFRAMEWORK_IMAGE_MASKEDIMAGE_H_
26 
29 
30 namespace SourceXtractor {
31 
44 template<typename T, typename M, template <typename> class Operator = std::bit_and>
45 class MaskedImage : public Image<T> {
46 private:
48  T replacement, M mask_flag) : m_image{image}, m_mask{mask}, m_replacement{replacement},
49  m_mask_flag{mask_flag}, m_n_masked{0}, m_n_total{0} {
50  }
51 
56  Operator<M> m_operator;
57 
60 
61 public:
62  virtual ~MaskedImage() = default;
63 
79  T replacement, M mask_flag = 0x01) {
80  assert(image->getWidth() == mask->getWidth() && image->getHeight() == mask->getHeight());
81  return std::shared_ptr<MaskedImage<T, M, Operator>>(new MaskedImage<T, M, Operator>(image, mask, replacement, mask_flag));
82  }
83 
84  std::string getRepr() const final {
85  if (m_n_total == (m_image->getWidth()*m_image->getHeight())){
86  char char_fract[8];
87  // determine the percentage of masked pixels
88  std::snprintf(char_fract, 7, "%.1f%", 100.0*this->getNMasked()/(m_image->getWidth()*m_image->getHeight()));
89  return std::string("Masked(" + m_image->getRepr() + ") with "+std::string(char_fract)+" weight image masked pixels;");
90  }
91  else
92  return std::string("Masked(" + m_image->getRepr() + ")");
93  }
94 
95  int getWidth() const final {
96  return m_image->getWidth();
97  }
98 
99  int getHeight() const final {
100  return m_image->getHeight();
101  }
102 
104  return m_n_masked;
105  }
106 
108  return m_n_total;
109  }
110 
111  std::shared_ptr<ImageChunk<T>> getChunk(int x, int y, int width, int height) const final {
112  auto chunk = UniversalImageChunk<T>::create(std::move(*m_image->getChunk(x, y, width, height)));
113  auto mask_chunk = m_mask->getChunk(x, y, width, height);
114  for (int iy = 0; iy < height; ++iy) {
115  for (int ix = 0; ix < width; ++ix) {
116  m_n_total += 1;
117  if (m_operator(mask_chunk->getValue(ix, iy), m_mask_flag)){
118  chunk->setValue(ix, iy, m_replacement);
119  m_n_masked+=1;
120  }
121  }
122  }
123  return chunk;
124  }
125 
126 };
127 
128 } // end of namespace SourceXtractor
129 
130 #endif // SEFRAMEWORK_SEFRAMEWORK_IMAGE_MASKEDIMAGE_H_
std::shared_ptr< Image< M > > m_mask
Definition: MaskedImage.h:53
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::size_t getNTotal() const
Definition: MaskedImage.h:107
std::shared_ptr< Image< T > > m_image
Definition: MaskedImage.h:52
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
int getHeight() const final
Returns the height of the image in pixels.
Definition: MaskedImage.h:99
STL class.
MaskedImage(const std::shared_ptr< Image< T >> &image, const std::shared_ptr< Image< M >> &mask, T replacement, M mask_flag)
Definition: MaskedImage.h:47
static std::shared_ptr< UniversalImageChunk< T > > create(Args &&...args)
Definition: ImageChunk.h:142
std::string getRepr() const final
Get a string identifying this image in a human readable manner.
Definition: MaskedImage.h:84
static std::shared_ptr< MaskedImage< T, M, Operator > > create(const std::shared_ptr< Image< T >> &image, const std::shared_ptr< Image< M >> &mask, T replacement, M mask_flag=0x01)
Definition: MaskedImage.h:78
std::size_t getNMasked() const
Definition: MaskedImage.h:103
T move(T...args)
virtual ~MaskedImage()=default
Interface representing an image.
Definition: Image.h:43
T snprintf(T...args)
std::shared_ptr< ImageChunk< T > > getChunk(int x, int y, int width, int height) const final
Definition: MaskedImage.h:111
int getWidth() const final
Returns the width of the image in pixels.
Definition: MaskedImage.h:95