SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  unsigned int flag_instance)
41  : m_flag_images(flag_images),
42  m_flag_instance(flag_instance) {
43 }
44 
45 
46 template<typename Combine>
48  // FIXME: for flag_image access, the external flags image not part of detection frame?!
49  const auto& detection_frame_info = source.getProperty<DetectionFrameInfo>();
50 
51  auto flag_image_acc = ImageAccessor<int64_t>(m_flag_images.at(detection_frame_info.getHduIndex()));
52 
53  if (flag_image_acc.getWidth() != detection_frame_info.getWidth() ||
54  flag_image_acc.getHeight() != detection_frame_info.getHeight()) {
55  throw Elements::Exception()
56  << "The flag image size does not match the detection image size: "
57  << flag_image_acc.getWidth() << "x" << flag_image_acc.getHeight() << " != "
58  << detection_frame_info.getWidth() << "x" << detection_frame_info.getHeight();
59  }
60 
62  for (auto& coords : source.getProperty<PixelCoordinateList>().getCoordinateList()) {
63  pixel_flags.push_back(flag_image_acc.getValue(coords.m_x, coords.m_y));
64  }
65  std::int64_t flag = 0;
66  int count = 0;
67  std::tie(flag, count) = Combine::combine(pixel_flags);
68  source.setIndexedProperty<ExternalFlag>(m_flag_instance, flag, count);
69 }
70 
71 
72 namespace ExternalFlagCombineTypes {
73 
74 struct Or {
76  std::int64_t flag = 0;
77  int count = 0;
78  for (auto pix_flag : pixel_flags) {
79  if (pix_flag != 0) {
80  flag |= pix_flag;
81  ++count;
82  }
83  }
84  return {flag, count};
85  }
86 };
87 
88 struct And {
91  int count = pixel_flags.size();
92  for (auto pix_flag : pixel_flags) {
93  flag &= pix_flag;
94  }
95  return {flag, count};
96  }
97 };
98 
99 struct Min {
102  int count = 0;
103  for (auto pix_flag : pixel_flags) {
104  if (pix_flag < flag) {
105  flag = pix_flag;
106  count = 1;
107  } else if (pix_flag == flag) {
108  ++count;
109  }
110  }
111  if (count == 0) {
112  flag = 0;
113  }
114  return {flag, count};
115  }
116 };
117 
118 struct Max {
120  std::int64_t flag = 0;
121  int count = 0;
122  for (auto pix_flag : pixel_flags) {
123  if (pix_flag > flag) {
124  flag = pix_flag;
125  count = 1;
126  } else if (pix_flag == flag) {
127  ++count;
128  }
129  }
130  if (count == 0) {
131  flag = 0;
132  }
133  return {flag, count};
134  }
135 };
136 
137 struct Most {
140  for (auto pix_flag : pixel_flags) {
141  counters[pix_flag] += 1;
142  }
143  std::int64_t flag = 0;
144  int count = 0;
145  for (auto& pair : counters) {
146  if (pair.second > count) {
147  flag = pair.first;
148  count = pair.second;
149  }
150  }
151  return {flag, count};
152  }
153 };
154 
155 } // end of namespace ExternalFlagCombineTypes
156 
157 template class ExternalFlagTask<ExternalFlagCombineTypes::Or>;
158 template class ExternalFlagTask<ExternalFlagCombineTypes::And>;
159 template class ExternalFlagTask<ExternalFlagCombineTypes::Min>;
160 template class ExternalFlagTask<ExternalFlagCombineTypes::Max>;
161 template class ExternalFlagTask<ExternalFlagCombineTypes::Most>;
162 
163 } // SourceXtractor namespace
164 
165 
166 
void computeProperties(SourceInterface &source) const override
Computes one or more properties for the Source.
const PropertyType & getProperty(unsigned int index=0) const
Convenience template method to call getProperty() with a more user-friendly syntax.
T tie(T...args)
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)
ExternalFlagTask(const std::vector< std::shared_ptr< FlagImage >> &flag_images, unsigned int flag_instance)
STL class.
T push_back(T...args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
T count(T...args)
T size(T...args)
STL class.
const std::vector< PixelCoordinate > & getCoordinateList() const
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
The SourceInterface is an abstract &quot;source&quot; that has properties attached to it.
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)