SourceXtractorPlusPlus  0.12
Please provide a description of the project.
 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 
28 
30 
31 namespace SourceXtractor {
32 
33 template<typename Combine>
35 }
36 
37 template<typename Combine>
39  : m_flag_image(flag_image), m_flag_instance(flag_instance) {
40 }
41 
42 
43 template<typename Combine>
45  // FIXME: for flag_image access, the external flags image not part of detection frame?!
47 
48  const auto& detection_frame_info = source.getProperty<DetectionFrameInfo>();
49 
50  if (m_flag_image->getWidth() != detection_frame_info.getWidth() ||
51  m_flag_image->getHeight() != detection_frame_info.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_frame_info.getWidth() << "x" << detection_frame_info.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.
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)
STL class.
T push_back(T...args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
T lock(T...args)
static std::pair< std::int64_t, int > combine(const std::vector< FlagImage::PixelType > &pixel_flags)
T count(T...args)
ExternalFlagTask(std::shared_ptr< FlagImage > flag_image, unsigned int flag_instance)
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)