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