SourceXtractorPlusPlus  0.13
Please provide a description of the project.
BgDFTConvolutionImageSource.cpp
Go to the documentation of this file.
1 
17 /*
18  * BgDFTConvolutionImageSource.h
19  *
20  * Created on: Jun 12, 2019
21  * Author: Alejandro Alvarez
22  * Refactored out from: BackgroundConvolution.h
23  */
24 
28 
29 namespace SourceXtractor {
30 
31 
33  std::shared_ptr<DetectionImage> variance, SeFloat threshold,
35  : ProcessingImageSource<DetectionImage::PixelType>(image),
36  m_variance(variance), m_threshold(threshold), m_convolution(kernel) {
37 }
38 
40  return "BgDFTConvolutionImageSource(" + getImageRepr() + ")";
41 }
42 
44  ImageTile& tile, int start_x, int start_y, int width, int height) const {
45  int hx = m_convolution.getWidth() / 2;
46  int hy = m_convolution.getHeight() / 2;
47  int clip_x = std::max(start_x - hx, 0);
48  int clip_y = std::max(start_y - hy, 0);
49  int clip_w = std::min(width + hx * 2, image->getWidth() - clip_x);
50  int clip_h = std::min(height + hy * 2, image->getHeight() - clip_y);
51 
52  // Clip the image and variance map to the given size, accounting for the margins for the convolution
54  clip_w, clip_h, [image, clip_x, clip_y](int x, int y) {
55  return image->getValue(clip_x + x, clip_y + y);
56  }
57  );
59  clip_w, clip_h, [this, clip_x, clip_y](int x, int y) {
60  return m_variance->getValue(clip_x + x, clip_y + y);
61  }
62  );
63 
64  // Get the mask
65  // For instance, with a threshold of 0.5
66  // Variance Mask Negative
67  // 1 1 1 0 0 0 1 1 1
68  // 1 0 1 0 1 0 1 0 1
69  // 1 1 1 0 0 0 1 1 1
71  clipped_variance->getWidth(), clipped_variance->getHeight(),
72  [this, clipped_variance](int x, int y) -> DetectionImage::PixelType {
73  return clipped_variance->getValue(x, y) < m_threshold;
74  }
75  );
76 
77  // Get the image masking out values where the variance is greater than the threshold
79  clipped_img, mask, 0., 0.);
80 
81  // Convolve the masked image, padding with 0
82  auto conv_masked = VectorImage<DetectionImage::PixelType>::create(masked_img);
83  m_convolution.convolve(conv_masked);
84 
85  // Convolve the mask
86  // This gives us in each cell the sum of the kernel values that have been used,
87  // so we can divide the previous convolution.
89  m_convolution.convolve(conv_mask);
90 
91  // Copy out the value of the convolved image, divided by the negative mask, applying
92  // again the mask to the convolved result
93  int off_x = start_x - clip_x;
94  int off_y = start_y - clip_y;
95  for (int y = 0; y < height; ++y) {
96  for (int x = 0; x < width; ++x) {
97  if (mask->getValue(x + off_x, y + off_y)) {
98  tile.setValue(
99  x + start_x, y + start_y,
100  conv_masked->getValue(x + off_x, y + off_y) / conv_mask->getValue(x + off_x, y + off_y)
101  );
102  } else {
103  tile.setValue(x + start_x, y + start_y, 0);
104  }
105  }
106  }
107 }
108 
109 } // end namespace SourceXtractor
110 
SourceXtractor::ProcessingImageSource< DetectionImage::PixelType >::getImageRepr
std::string getImageRepr() const
Definition: ProcessingImageSource.h:69
SourceXtractor::FunctionalImage::getValue
T getValue(int x, int y) const final
Returns the value of the pixel with the coordinates (x,y)
Definition: FunctionalImage.h:52
std::string
STL class.
std::shared_ptr
STL class.
SourceXtractor::ImageTile::setValue
virtual void setValue(int x, int y, float value)=0
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition: Image.h:47
SourceXtractor::SeFloat
SeFloat32 SeFloat
Definition: Types.h:32
SourceXtractor::BgDFTConvolutionImageSource::generateTile
void generateTile(const std::shared_ptr< Image< DetectionImage::PixelType >> &image, ImageTile &tile, int start_x, int start_y, int width, int height) const override
Definition: BgDFTConvolutionImageSource.cpp:43
SourceXtractor::DFTConvolution::getHeight
std::size_t getHeight() const
Definition: DFT.h:92
SourceXtractor::ProcessingImageSource
Definition: ProcessingImageSource.h:33
SourceXtractor::MaskedImage::create
static std::shared_ptr< MaskedImage< T, M, Operator > > create(const std::shared_ptr< Image< T >> &image, const std::shared_ptr< Image< M >> &mask, T replacement, M mask_flag=0x01)
Definition: MaskedImage.h:75
SourceXtractor::DFTConvolution::getWidth
std::size_t getWidth() const
Definition: DFT.h:84
SourceXtractor::FunctionalImage::getWidth
int getWidth() const final
Returns the width of the image in pixels.
Definition: FunctionalImage.h:56
SourceXtractor::Image< DetectionImage::PixelType >
SourceXtractor::ImageTile
Definition: ImageTile.h:34
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::DFTConvolution::convolve
void convolve(std::shared_ptr< WriteableImage< T >> image_ptr, std::unique_ptr< ConvolutionContext > &context, Args... padding_args) const
Definition: DFT.h:151
FunctionalImage.h
SourceXtractor::BgDFTConvolutionImageSource::BgDFTConvolutionImageSource
BgDFTConvolutionImageSource(std::shared_ptr< Image< DetectionImage::PixelType >> image, std::shared_ptr< DetectionImage > variance, SeFloat threshold, std::shared_ptr< VectorImage< SeFloat >> kernel)
Definition: BgDFTConvolutionImageSource.cpp:32
SourceXtractor::FunctionalImage::getHeight
int getHeight() const final
Returns the height of the image in pixels.
Definition: FunctionalImage.h:60
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition: VectorImage.h:89
std::min
T min(T... args)
SourceXtractor::FunctionalImage::create
static std::shared_ptr< ImageBase< T > > create(Args &&... args)
Definition: FunctionalImage.h:44
SourceXtractor::BgDFTConvolutionImageSource::m_convolution
ConvolutionType m_convolution
Definition: BgDFTConvolutionImageSource.h:60
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:94
SourceXtractor::VectorImage< SeFloat >
BgDFTConvolutionImageSource.h
SourceXtractor::BgDFTConvolutionImageSource::getRepr
std::string getRepr() const override
Human readable representation of this source.
Definition: BgDFTConvolutionImageSource.cpp:39
SourceXtractor::BgDFTConvolutionImageSource::m_variance
std::shared_ptr< DetectionImage > m_variance
Definition: BgDFTConvolutionImageSource.h:58
MaskedImage.h
std::max
T max(T... args)
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:94