SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
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
28namespace SourceXtractor {
29
30
38
40 return "BgConvolutionImageSource(" + getImageRepr() + ")";
41}
42
44applyKernel(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) {
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
68 int start_y, int width, int height) const {
69 const int hx = m_kernel->getWidth() / 2;
70 const int hy = m_kernel->getHeight() / 2;
71 const int clip_x = std::max(start_x - hx, 0);
72 const int clip_y = std::max(start_y - hy, 0);
73 const int clip_w = std::min(width + hx * 2, image->getWidth() - clip_x);
74 const int clip_h = std::min(height + hy * 2, image->getHeight() - clip_y);
75
76 // "Materialize" the image and variance
77 auto image_chunk = image->getChunk(clip_x, clip_y, clip_w, clip_h);
78 auto variance_chunk = m_variance->getChunk(clip_x, clip_y, clip_w, clip_h);
79
80 // Convolve both and copy out to the tile
81 const int off_x = start_x - clip_x;
82 const int off_y = start_y - clip_y;
83 auto& tile_image = *tile.getImage();
84 for (int iy = 0; iy < height; ++iy) {
85 for (int ix = 0; ix < width; ++ix) {
86 if (variance_chunk->getValue(ix + off_x, iy + off_y) < m_threshold) {
88 DetectionImage::PixelType conv_weight = 0.;
89
90 std::tie(total, conv_weight) = applyKernel(*m_kernel, *image_chunk, *variance_chunk,
91 ix - hx + off_x, iy - hy + off_y, clip_w, clip_h,
93
94 // Note that because of the conditional, at least the center pixel is below the threshold,
95 // so checking for conv_weight > 0 is redundant
96 tile_image.setValue(ix, iy, total / conv_weight);
97 }
98 else {
99 tile_image.setValue(ix, iy, 0.);
100 }
101 }
102 }
103}
104
105
106} // end namespace SourceXtractor
107
std::string getRepr() const override
Human readable representation of this source.
void generateTile(const std::shared_ptr< Image< DetectionImage::PixelType > > &image, ImageTileWithType< DetectionImage::PixelType > &tile, int start_x, int start_y, int width, int height) const override
std::shared_ptr< DetectionImage > m_variance
BgConvolutionImageSource(std::shared_ptr< Image< DetectionImage::PixelType > > image, std::shared_ptr< DetectionImage > variance, SeFloat threshold, std::shared_ptr< VectorImage< SeFloat > > kernel)
std::shared_ptr< VectorImage< SeFloat > > m_kernel
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition ImageChunk.h:58
const std::shared_ptr< VectorImage< T > > & getImage() const
Definition ImageTile.h:178
Interface representing an image.
Definition Image.h:44
static std::shared_ptr< MirrorImage< T > > create(Args &&... args)
Definition MirrorImage.h:44
ProcessingImageSource(std::shared_ptr< Image< DetectionImage::PixelType > > image)
Image implementation which keeps the pixel values in memory.
Definition VectorImage.h:52
int getWidth() const final
Returns the width of the image in pixels.
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
int getHeight() const final
Returns the height of the image in pixels.
T getValue(int x, int y) const
T make_pair(T... args)
T max(T... args)
T min(T... args)
SeFloat32 SeFloat
Definition Types.h:32
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)
Image< SeFloat > DetectionImage
Alias for the detection image, to make easier its type modification.
Definition Image.h:80
T tie(T... args)