SourceXtractorPlusPlus  0.13
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BgConvolutionImageSource.cpp
Go to the documentation of this file.
1 
17 /*
18  * BgConvolutionImageSource.cpp
19  *
20  * Created on: Jun 12, 2019
21  * Author: Alejandro Alvarez
22  * Refactored out from: BackgroundConvolution.h
23  */
24 
27 
28 namespace SourceXtractor {
29 
30 
32  std::shared_ptr<DetectionImage> variance, SeFloat threshold,
34  : ProcessingImageSource<DetectionImage::PixelType>(image),
35  m_variance(variance), m_threshold(threshold) {
37 }
38 
40  return "BgConvolutionImageSource(" + getImageRepr() + ")";
41 }
42 
44 applyKernel(const VectorImage<SeFloat>& kernel, ImageChunk<SeFloat>& image_chunk, ImageChunk<SeFloat>& variance_chunk,
45  int start_x, int start_y, int clip_w, int clip_h, SeFloat threshold) {
46  DetectionImage::PixelType total = 0.;
47  DetectionImage::PixelType conv_weight = 0.;
48 
49  for (int cy = 0; cy < kernel.getHeight(); ++cy) {
50  for (int cx = 0; cx < kernel.getWidth(); ++cx) {
51  int clip_x2 = start_x + cx;
52  int clip_y2 = start_y + cy;
53 
54  if (clip_x2 >= 0 && clip_x2 < clip_w && clip_y2 >= 0 && clip_y2 < clip_h) {
55  if (variance_chunk.getValue(clip_x2, clip_y2) < threshold) {
56  total += image_chunk.getValue(clip_x2, clip_y2) * kernel.getValue(cx, cy);
57  conv_weight += kernel.getValue(cx, cy);
58  }
59  }
60  }
61  }
62 
63  return std::make_pair(total, conv_weight);
64 }
65 
67  ImageTile& tile, int start_x, int start_y, int width, int height) const {
68  const int hx = m_kernel->getWidth() / 2;
69  const int hy = m_kernel->getHeight() / 2;
70  const int clip_x = std::max(start_x - hx, 0);
71  const int clip_y = std::max(start_y - hy, 0);
72  const int clip_w = std::min(width + hx * 2, image->getWidth() - clip_x);
73  const int clip_h = std::min(height + hy * 2, image->getHeight() - clip_y);
74 
75  // "Materialize" the image and variance
76  auto image_chunk = image->getChunk(clip_x, clip_y, clip_w, clip_h);
77  auto variance_chunk = m_variance->getChunk(clip_x, clip_y, clip_w, clip_h);
78 
79  // Convolve both and copy out to the tile
80  const int off_x = start_x - clip_x;
81  const int off_y = start_y - clip_y;
82 
83  for (int iy = 0; iy < height; ++iy) {
84  for (int ix = 0; ix < width; ++ix) {
85  if (variance_chunk->getValue(ix + off_x, iy + off_y) < m_threshold) {
86  DetectionImage::PixelType total = 0.;
87  DetectionImage::PixelType conv_weight = 0.;
88 
89  std::tie(total, conv_weight) = applyKernel(*m_kernel, *image_chunk, *variance_chunk,
90  ix - hx + off_x, iy - hy + off_y, clip_w, clip_h,
91  m_threshold);
92 
93  // Note that because of the conditional, at least the center pixel is below the threshold,
94  // so checking for conv_weight > 0 is redundant
95  tile.getImage<DetectionImage::PixelType>()->setValue(ix, iy, total / conv_weight);
96  }
97  else {
98  tile.getImage<DetectionImage::PixelType>()->setValue(ix, iy, 0.);
99  }
100  }
101  }
102 }
103 
104 
105 } // end namespace SourceXtractor
106 
Mirrors an image in both X and Y axes.
Definition: MirrorImage.h:35
T tie(T...args)
BgConvolutionImageSource(std::shared_ptr< Image< DetectionImage::PixelType >> image, std::shared_ptr< DetectionImage > variance, SeFloat threshold, std::shared_ptr< VectorImage< SeFloat >> kernel)
int getHeight() const final
Returns the height of the image in pixels.
Definition: VectorImage.h:97
std::shared_ptr< VectorImage< T > > getImage() const
Definition: ImageTile.h:88
SeFloat32 SeFloat
Definition: Types.h:32
static std::shared_ptr< VectorImage< T > > create(Args &&...args)
Definition: VectorImage.h:89
STL class.
T min(T...args)
std::string getRepr() const override
Human readable representation of this source.
std::shared_ptr< VectorImage< SeFloat > > m_kernel
void generateTile(const std::shared_ptr< Image< DetectionImage::PixelType >> &image, ImageTile &tile, int start_x, int start_y, int width, int height) const override
Image implementation which keeps the pixel values in memory.
Definition: VectorImage.h:53
int getWidth() const final
Returns the width of the image in pixels.
Definition: VectorImage.h:101
T make_pair(T...args)
static std::tuple< float, float > applyKernel(const VectorImage< SeFloat > &kernel, ImageChunk< SeFloat > &image_chunk, ImageChunk< SeFloat > &variance_chunk, int start_x, int start_y, int clip_w, int clip_h, SeFloat threshold)
T max(T...args)
Interface representing an image.
Definition: Image.h:43
std::shared_ptr< DetectionImage > m_variance
T getValue(int x, int y) const final
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:57
T getValue(int x, int y) const final
Returns the value of the pixel with the coordinates (x,y)
Definition: VectorImage.h:106