SourceXtractorPlusPlus  0.10
Please provide a description of the project.
ExternalFlagTask.cpp
Go to the documentation of this file.
1 
23 #include <mutex>
24 
26 
29 
31 
32 namespace SourceXtractor {
33 
34 template<typename Combine>
36 }
37 
38 template<typename Combine>
40  : m_flag_image(flag_image), m_flag_instance(flag_instance) {
41 }
42 
43 
44 template<typename Combine>
47 
48  const auto& detection_frame = source.getProperty<DetectionFrame>();
49  const auto& detection_image = detection_frame.getFrame()->getOriginalImage();
50 
51  if (m_flag_image->getWidth() != detection_image->getWidth() || m_flag_image->getHeight() != detection_image->getHeight()) {
52  throw Elements::Exception()
53  << "The flag image size does not match the detection image size: "
54  << m_flag_image->getWidth() << "x" << m_flag_image->getHeight() << " != "
55  << detection_image->getWidth() << "x" << detection_image->getHeight();
56  }
57 
59  for (auto& coords : source.getProperty<PixelCoordinateList>().getCoordinateList()) {
60  pixel_flags.push_back(m_flag_image->getValue(coords.m_x, coords.m_y));
61  }
62  std::int64_t flag = 0;
63  int count = 0;
64  std::tie(flag, count) = Combine::combine(pixel_flags);
65  source.setIndexedProperty<ExternalFlag>(m_flag_instance, flag, count);
66 }
67 
68 
69 namespace ExternalFlagCombineTypes {
70 
71 struct Or {
73  std::int64_t flag = 0;
74  int count = 0;
75  for (auto pix_flag : pixel_flags) {
76  if (pix_flag != 0) {
77  flag |= pix_flag;
78  ++count;
79  }
80  }
81  return {flag, count};
82  }
83 };
84 
85 struct And {
88  int count = pixel_flags.size();
89  for (auto pix_flag : pixel_flags) {
90  flag &= pix_flag;
91  }
92  return {flag, count};
93  }
94 };
95 
96 struct Min {
99  int count = 0;
100  for (auto pix_flag : pixel_flags) {
101  if (pix_flag < flag) {
102  flag = pix_flag;
103  count = 1;
104  } else if (pix_flag == flag) {
105  ++count;
106  }
107  }
108  if (count == 0) {
109  flag = 0;
110  }
111  return {flag, count};
112  }
113 };
114 
115 struct Max {
117  std::int64_t flag = 0;
118  int count = 0;
119  for (auto pix_flag : pixel_flags) {
120  if (pix_flag > flag) {
121  flag = pix_flag;
122  count = 1;
123  } else if (pix_flag == flag) {
124  ++count;
125  }
126  }
127  if (count == 0) {
128  flag = 0;
129  }
130  return {flag, count};
131  }
132 };
133 
134 struct Most {
137  for (auto pix_flag : pixel_flags) {
138  counters[pix_flag] += 1;
139  }
140  std::int64_t flag = 0;
141  int count = 0;
142  for (auto& pair : counters) {
143  if (pair.second > count) {
144  flag = pair.first;
145  count = pair.second;
146  }
147  }
148  return {flag, count};
149  }
150 };
151 
152 } // end of namespace ExternalFlagCombineTypes
153 
154 template class ExternalFlagTask<ExternalFlagCombineTypes::Or>;
155 template class ExternalFlagTask<ExternalFlagCombineTypes::And>;
156 template class ExternalFlagTask<ExternalFlagCombineTypes::Min>;
157 template class ExternalFlagTask<ExternalFlagCombineTypes::Max>;
158 template class ExternalFlagTask<ExternalFlagCombineTypes::Most>;
159 
160 } // SourceXtractor namespace
161 
162 
163 
void computeProperties(SourceInterface &source) const override
Computes one or more properties for the Source.
STL class.
T tie(T... args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
STL class.
T push_back(T... args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
const PropertyType & getProperty(unsigned int index=0) const
Convenience template method to call getProperty() with a more user-friendly syntax.
T lock(T... args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
T max(T... args)
T count(T... args)
ExternalFlagTask(std::shared_ptr< FlagImage > flag_image, unsigned int flag_instance)
T size(T... args)
STL class.
void setIndexedProperty(std::size_t index, Args... args)
Convenience template method to call setProperty() with a more user-friendly syntax.
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
std::shared_ptr< DetectionImageFrame > getFrame() const
The SourceInterface is an abstract "source" that has properties attached to it.
const std::vector< PixelCoordinate > & getCoordinateList() const
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)