SourceXtractorPlusPlus  0.13
Please provide a description of the project.
MultiThresholdPartitionStep.cpp
Go to the documentation of this file.
1 
17 /*
18  * MultiThresholdPartitionStep.cpp
19  *
20  * Created on: Jan 17, 2017
21  * Author: mschefer
22  */
23 
26 
29 
36 
38 
40 
41 namespace SourceXtractor {
42 
43 class MultiThresholdNode : public std::enable_shared_from_this<MultiThresholdNode> {
44 public:
45 
47  : m_pixel_list(pixel_list), m_is_split(false), m_threshold(threshold) {
48  }
49 
51  m_children.push_back(child);
52  child->m_parent = shared_from_this();
53  }
54 
55  bool contains(const Lutz::PixelGroup& pixel_group) const {
56  for (auto pixel : m_pixel_list) {
57  if (pixel_group.pixel_list[0] == pixel) {
58  return true;
59  }
60  }
61  return false;
62  }
63 
65  return m_children;
66  }
67 
69  return m_parent.lock();
70  }
71 
72  double getTotalIntensity(DetectionImage& image, const PixelCoordinate& offset) const {
73  DetectionImage::PixelType total_intensity = 0;
74  for (const auto& pixel_coord : m_pixel_list) {
75  total_intensity += (image.getValue(pixel_coord - offset) - m_threshold);
76  }
77 
78  return total_intensity;
79  }
80 
81  bool isSplit() const {
82  return m_is_split;
83  }
84 
85  void flagAsSplit() {
86  m_is_split = true;
87  auto parent = m_parent.lock();
88  if (parent != nullptr) {
89  parent->flagAsSplit();
90  }
91  }
92 
94  return m_pixel_list;
95  }
96 
97  void debugPrint() const {
98  std::cout << "(" << m_pixel_list.size();
99 
100  for (auto& child : m_children) {
101  std::cout << ", ";
102  child->debugPrint();
103  }
104 
105  std::cout << ")";
106  }
107 
108  void addPixel(PixelCoordinate pixel) {
109  m_pixel_list.emplace_back(pixel);
110  }
111 
113  return m_threshold;
114  }
115 
116 private:
118 
121 
123 
125 };
126 
128  std::shared_ptr<SourceInterface> original_source) const {
129 
130  auto parent_source_id = original_source->getProperty<SourceId>().getSourceId();
131 
132  auto& detection_frame = original_source->getProperty<DetectionFrame>();
133 
134  auto& detection_frame_images = original_source->getProperty<DetectionFrameImages>();
135  const auto labelling_image = detection_frame_images.getLockedImage(LayerFilteredImage);
136 
137  auto& pixel_boundaries = original_source->getProperty<PixelBoundaries>();
138 
139  auto& pixel_coords = original_source->getProperty<PixelCoordinateList>().getCoordinateList();
140 
141  auto offset = pixel_boundaries.getMin();
142  auto thumbnail_image = VectorImage<DetectionImage::PixelType>::create(
143  pixel_boundaries.getWidth(), pixel_boundaries.getHeight());
144  thumbnail_image->fillValue(0);
145 
146  auto min_value = original_source->getProperty<PeakValue>().getMinValue() * .8;
147  auto peak_value = original_source->getProperty<PeakValue>().getMaxValue();
148 
149  {
151  for (auto pixel_coord : pixel_coords) {
152  auto value = labelling_image->getValue(pixel_coord);
153  thumbnail_image->setValue(pixel_coord - offset, value);
154  }
155  }
156 
157  auto root = std::make_shared<MultiThresholdNode>(pixel_coords, 0);
158 
159  std::list<std::shared_ptr<MultiThresholdNode>> active_nodes { root };
161 
162  // Build the tree
163  for (unsigned int i = 1; i < m_thresholds_nb; i++) {
164 
165  auto threshold = min_value * pow(peak_value / min_value, (double) i / m_thresholds_nb);
166  auto subtracted_image = SubtractImage<DetectionImage::PixelType>::create(thumbnail_image, threshold);
167 
168  LutzList lutz;
169  lutz.labelImage(*subtracted_image, offset);
170 
171  std::list<std::shared_ptr<MultiThresholdNode>> active_nodes_copy(active_nodes);
172  for (auto& node : active_nodes_copy) {
173  int nb_of_groups_inside = 0;
174  for (auto& pixel_group : lutz.getGroups()) {
175  if (pixel_group.pixel_list.size() >= m_min_deblend_area && node->contains(pixel_group)) {
176  nb_of_groups_inside++;
177  }
178  }
179 
180  if (nb_of_groups_inside == 0) {
181  active_nodes.remove(node);
182  }
183 
184  if (nb_of_groups_inside > 1) {
185  active_nodes.remove(node);
186  junction_nodes.push_back(node);
187  for (auto& pixel_group : lutz.getGroups()) {
188  if (pixel_group.pixel_list.size() >= m_min_deblend_area && node->contains(pixel_group)) {
189  auto new_node = std::make_shared<MultiThresholdNode>(pixel_group.pixel_list, threshold);
190  node->addChild(new_node);
191  active_nodes.push_back(new_node);
192  }
193  }
194  }
195  }
196  }
197 
198  // Identify the sources
199  double intensity_threshold = root->getTotalIntensity(*thumbnail_image, offset) * m_contrast;
200 
202  while (!junction_nodes.empty()) {
203  auto node = junction_nodes.back();
204  junction_nodes.pop_back();
205 
206  int nb_of_children_above_threshold = 0;
207 
208  for (auto child : node->getChildren()) {
209  if (child->getTotalIntensity(*thumbnail_image, offset) > intensity_threshold) {
210  nb_of_children_above_threshold++;
211  }
212  }
213 
214  if (nb_of_children_above_threshold >= 2) {
215  node->flagAsSplit();
216  for (auto child : node->getChildren()) {
217  if (child->getTotalIntensity(*thumbnail_image, offset) > intensity_threshold && !child->isSplit()) {
218  source_nodes.push_back(child);
219  }
220  }
221  }
222  }
223 
225  if (source_nodes.empty()) {
226  return { original_source }; // no split, just forward the source unchanged
227  }
228 
229  for (auto source_node : source_nodes) {
230  // remove pixels in the new sources from the image
231  for (auto& pixel : source_node->getPixels()) {
232  thumbnail_image->setValue(pixel - offset, 0);
233  }
234 
235  auto new_source = m_source_factory->createSource();
236 
237  new_source->setProperty<PixelCoordinateList>(source_node->getPixels());
238  new_source->setProperty<DetectionFrame>(detection_frame.getEncapsulatedFrame());
239 
240  sources.push_back(new_source);
241  }
242 
243  auto new_sources = reassignPixels(sources, pixel_coords, thumbnail_image, source_nodes, offset);
244 
245  for (auto& new_source : new_sources) {
246  new_source->setProperty<DetectionFrame>(detection_frame.getEncapsulatedFrame());
247  new_source->setProperty<SourceId>(parent_source_id);
248  }
249 
250  return new_sources;
251 }
252 
255  const std::vector<PixelCoordinate>& pixel_coords,
258  const PixelCoordinate& offset
259  ) const {
260 
261  std::vector<SeFloat> amplitudes;
262  for (auto& source : sources) {
263  auto& pixel_list = source->getProperty<PixelCoordinateList>().getCoordinateList();
264  auto& shape_parameters = source->getProperty<ShapeParameters>();
265 
266  auto thresh = source->getProperty<PeakValue>().getMinValue();
267  auto peak = source->getProperty<PeakValue>().getMaxValue();
268 
269  auto dist = pixel_list.size() / (2.0 * M_PI * shape_parameters.getAbcor() * shape_parameters.getEllipseA() * shape_parameters.getEllipseB());
270  auto amp = dist < 70.0 ? thresh * expf(dist) : 4.0 * peak;
271 
272  // limit expansion ??
273  if (amp > 4.0 * peak) {
274  amp = 4.0 * peak;
275  }
276 
277  amplitudes.push_back(amp);
278  }
279 
280  for (auto pixel : pixel_coords) {
281  if (image->getValue(pixel - offset) > 0) {
282  SeFloat cumulated_probability = 0;
283  std::vector<SeFloat> probabilities;
284 
286  std::shared_ptr<MultiThresholdNode> closest_source_node;
287 
288  int i = 0;
289  for (auto& source : sources) {
290  auto& shape_parameters = source->getProperty<ShapeParameters>();
291  auto& pixel_centroid = source->getProperty<PixelCentroid>();
292 
293  auto dx = pixel.m_x - pixel_centroid.getCentroidX();
294  auto dy = pixel.m_y - pixel_centroid.getCentroidY();
295 
296  auto dist = 0.5 * (shape_parameters.getEllipseCxx()*dx*dx +
297  shape_parameters.getEllipseCyy()*dy*dy + shape_parameters.getEllipseCxy()*dx*dy) /
298  shape_parameters.getAbcor();
299 
300  if (dist < min_dist) {
301  min_dist = dist;
302  closest_source_node = source_nodes[i];
303  }
304 
305  cumulated_probability += dist < 70.0 ? amplitudes[i] * expf(-dist) : 0.0;
306 
307  probabilities.push_back(cumulated_probability);
308  i++;
309  }
310 
311  if (probabilities.back() > 1.0e-31) {
312  // TODO probably should use a better RNG
313  auto drand = double(probabilities.back()) * double(rand()) / RAND_MAX;
314 
315  unsigned int i=0;
316  for (; i<probabilities.size() && drand >= probabilities[i]; i++);
317  if (i < source_nodes.size()) {
318  source_nodes[i]->addPixel(pixel);
319  } else {
320  std::cout << i << " oops " << drand << " " << probabilities.back() << std::endl;
321  }
322 
323  } else {
324  // select closest source
325  closest_source_node->addPixel(pixel);
326  }
327  }
328  }
329 
330  int total_pixels = 0;
331 
333  for (auto source_node : source_nodes) {
334  // remove pixels in the new sources from the image
335  for (auto& pixel : source_node->getPixels()) {
336  image->setValue(pixel - offset, 0);
337  }
338 
339  auto new_source = m_source_factory->createSource();
340 
341  auto pixels = source_node->getPixels();
342  total_pixels += pixels.size();
343 
344  new_source->setProperty<PixelCoordinateList>(pixels);
345 
346  new_sources.push_back(new_source);
347  }
348 
349  return new_sources;
350 }
351 
352 
353 } // namespace
354 
355 
SourceXtractor::PixelBoundaries
The bounding box of all the pixels in the source. Both min and max coordinate are inclusive.
Definition: PixelBoundaries.h:37
SourceXtractor::PixelCoordinateList
Definition: PixelCoordinateList.h:31
SourceXtractor::MultiThresholdNode::debugPrint
void debugPrint() const
Definition: MultiThresholdPartitionStep.cpp:97
std::lock
T lock(T... args)
SourceXtractor::PixelCoordinate
A pixel coordinate made of two integers m_x and m_y.
Definition: PixelCoordinate.h:37
SourceXtractor::DetectionFrameImages
Definition: DetectionFrameImages.h:32
SourceXtractor::MultiThresholdNode::getPixels
const std::vector< PixelCoordinate > & getPixels() const
Definition: MultiThresholdPartitionStep.cpp:93
PixelBoundaries.h
std::shared_ptr
STL class.
std::list
STL class.
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition: Image.h:47
SourceId.h
SourceXtractor::PixelCentroid
The centroid of all the pixels in the source, weighted by their DetectionImage pixel values.
Definition: PixelCentroid.h:37
PeakValue.h
SourceXtractor::SeFloat
SeFloat32 SeFloat
Definition: Types.h:32
std::vector
STL class.
std::vector::size
T size(T... args)
SourceXtractor::LutzList::labelImage
void labelImage(const DetectionImage &image, PixelCoordinate offset=PixelCoordinate(0, 0))
Definition: Lutz.h:75
SourceXtractor::MultiThresholdNode::getParent
std::shared_ptr< MultiThresholdNode > getParent() const
Definition: MultiThresholdPartitionStep.cpp:68
std::lock_guard< std::recursive_mutex >
std::list::back
T back(T... args)
SourceXtractor::MultiThresholdNode::addPixel
void addPixel(PixelCoordinate pixel)
Definition: MultiThresholdPartitionStep.cpp:108
SourceXtractor::MultithreadedMeasurement::g_global_mutex
static std::recursive_mutex g_global_mutex
Definition: MultithreadedMeasurement.h:53
SourceXtractor::LayerFilteredImage
@ LayerFilteredImage
Definition: Frame.h:39
SourceXtractor::MultiThresholdPartitionStep::m_contrast
SeFloat m_contrast
Definition: MultiThresholdPartitionStep.h:68
SourceXtractor::Image< SeFloat >
VectorImage.h
DetectionFramePixelValues.h
MultiThresholdPartitionStep.h
std::list::push_back
T push_back(T... args)
SourceXtractor::SourceId
Definition: SourceId.h:31
SourceXtractor
Definition: Aperture.h:30
std::enable_shared_from_this< MultiThresholdNode >::shared_from_this
T shared_from_this(T... args)
SourceXtractor::Image::getValue
virtual T getValue(int x, int y) const =0
Returns the value of the pixel with the coordinates (x,y)
SourceXtractor::MultiThresholdPartitionStep::m_thresholds_nb
unsigned int m_thresholds_nb
Definition: MultiThresholdPartitionStep.h:69
SourceXtractor::MultiThresholdNode
Definition: MultiThresholdPartitionStep.cpp:43
dy
std::shared_ptr< EngineParameter > dy
Definition: MoffatModelFittingTask.cpp:93
std::cout
SourceXtractor::MultiThresholdPartitionStep::m_source_factory
std::shared_ptr< SourceFactory > m_source_factory
Definition: MultiThresholdPartitionStep.h:67
SourceXtractor::LutzList
Definition: Lutz.h:65
SourceXtractor::MultiThresholdPartitionStep::partition
virtual std::vector< std::shared_ptr< SourceInterface > > partition(std::shared_ptr< SourceInterface > source) const
Definition: MultiThresholdPartitionStep.cpp:127
std::enable_shared_from_this
SourceXtractor::DetectionFrame
Definition: DetectionFrame.h:33
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition: VectorImage.h:89
SourceXtractor::MultiThresholdNode::m_parent
std::weak_ptr< MultiThresholdNode > m_parent
Definition: MultiThresholdPartitionStep.cpp:119
SourceXtractor::MultiThresholdNode::isSplit
bool isSplit() const
Definition: MultiThresholdPartitionStep.cpp:81
ProcessedImage.h
SourceXtractor::MultiThresholdNode::getChildren
const std::vector< std::shared_ptr< MultiThresholdNode > > & getChildren() const
Definition: MultiThresholdPartitionStep.cpp:64
SourceXtractor::PeakValue
Definition: PeakValue.h:32
std::list::pop_back
T pop_back(T... args)
Lutz.h
SourceXtractor::MultiThresholdNode::m_is_split
bool m_is_split
Definition: MultiThresholdPartitionStep.cpp:122
SourceXtractor::MultiThresholdNode::m_children
std::vector< std::shared_ptr< MultiThresholdNode > > m_children
Definition: MultiThresholdPartitionStep.cpp:120
SourceXtractor::Lutz::PixelGroup
Definition: Lutz.h:39
std::rand
T rand(T... args)
std::weak_ptr
STL class.
SourceXtractor::ProcessedImage::create
static std::shared_ptr< ProcessedImage< T, P > > create(std::shared_ptr< const Image< T >> image_a, std::shared_ptr< const Image< T >> image_b)
Definition: ProcessedImage.h:54
SourceXtractor::LutzList::getGroups
const std::vector< PixelGroup > & getGroups() const
Definition: Lutz.h:71
std::endl
T endl(T... args)
SourceXtractor::MultiThresholdNode::MultiThresholdNode
MultiThresholdNode(const std::vector< PixelCoordinate > &pixel_list, SeFloat threshold)
Definition: MultiThresholdPartitionStep.cpp:46
SourceXtractor::VectorImage< DetectionImage::PixelType >
MultithreadedMeasurement.h
SourceXtractor::MultiThresholdNode::getTotalIntensity
double getTotalIntensity(DetectionImage &image, const PixelCoordinate &offset) const
Definition: MultiThresholdPartitionStep.cpp:72
std::list::empty
T empty(T... args)
SourceXtractor::MultiThresholdNode::addChild
void addChild(std::shared_ptr< MultiThresholdNode > child)
Definition: MultiThresholdPartitionStep.cpp:50
SourceXtractor::MultiThresholdNode::contains
bool contains(const Lutz::PixelGroup &pixel_group) const
Definition: MultiThresholdPartitionStep.cpp:55
ShapeParameters.h
std::numeric_limits::max
T max(T... args)
dx
std::shared_ptr< EngineParameter > dx
Definition: MoffatModelFittingTask.cpp:93
SourceXtractor::MultiThresholdPartitionStep::reassignPixels
std::vector< std::shared_ptr< SourceInterface > > reassignPixels(const std::vector< std::shared_ptr< SourceInterface >> &sources, const std::vector< PixelCoordinate > &pixel_coords, std::shared_ptr< VectorImage< DetectionImage::PixelType >> image, const std::vector< std::shared_ptr< MultiThresholdNode >> &source_nodes, const PixelCoordinate &offset) const
Definition: MultiThresholdPartitionStep.cpp:253
SourceXtractor::MultiThresholdNode::m_pixel_list
std::vector< PixelCoordinate > m_pixel_list
Definition: MultiThresholdPartitionStep.cpp:117
SourceXtractor::ShapeParameters::getAbcor
SeFloat getAbcor() const
Definition: ShapeParameters.h:91
SourceXtractor::Lutz::PixelGroup::pixel_list
std::vector< PixelCoordinate > pixel_list
Definition: Lutz.h:44
SourceXtractor::ShapeParameters
Definition: ShapeParameters.h:32
PixelCentroid.h
SourceXtractor::MultiThresholdNode::getThreshold
SeFloat getThreshold() const
Definition: MultiThresholdPartitionStep.cpp:112
SourceXtractor::MultiThresholdNode::flagAsSplit
void flagAsSplit()
Definition: MultiThresholdPartitionStep.cpp:85
SourceXtractor::MultiThresholdNode::m_threshold
SeFloat m_threshold
Definition: MultiThresholdPartitionStep.cpp:124
SourceXtractor::MultiThresholdPartitionStep::m_min_deblend_area
unsigned int m_min_deblend_area
Definition: MultiThresholdPartitionStep.h:70
DetectionFrameImages.h
std::pow
T pow(T... args)