SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEImplementation
src
lib
Segmentation
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
25
#include "
SEImplementation/Segmentation/BgConvolutionImageSource.h
"
26
#include "
SEFramework/Image/FunctionalImage.h
"
27
28
namespace
SourceXtractor
{
29
30
31
BgConvolutionImageSource::BgConvolutionImageSource
(
std::shared_ptr
<
Image<DetectionImage::PixelType>
>
image
,
32
std::shared_ptr<DetectionImage>
variance
,
SeFloat
threshold
,
33
std::shared_ptr
<
VectorImage<SeFloat>
>
kernel
)
34
:
ProcessingImageSource
<
DetectionImage
::PixelType>(
image
),
35
m_variance(
variance
), m_threshold(
threshold
) {
36
m_kernel
=
VectorImage<SeFloat>::create
(
MirrorImage<SeFloat>::create
(
kernel
));
37
}
38
39
std::string
BgConvolutionImageSource::getRepr
()
const
{
40
return
"BgConvolutionImageSource("
+
getImageRepr
() +
")"
;
41
}
42
43
static
std::tuple<float, float>
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
66
void
BgConvolutionImageSource::generateTile
(
const
std::shared_ptr
<
Image<DetectionImage::PixelType>
>&
image
,
67
ImageTileWithType<DetectionImage::PixelType>
&
tile
,
int
start_x
,
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
) {
87
DetectionImage::PixelType
total = 0.;
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
,
92
m_threshold
);
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
BgConvolutionImageSource.h
FunctionalImage.h
std::string
SourceXtractor::BgConvolutionImageSource::getRepr
std::string getRepr() const override
Human readable representation of this source.
Definition
BgConvolutionImageSource.cpp:39
SourceXtractor::BgConvolutionImageSource::generateTile
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
Definition
BgConvolutionImageSource.cpp:66
SourceXtractor::BgConvolutionImageSource::m_threshold
SeFloat m_threshold
Definition
BgConvolutionImageSource.h:54
SourceXtractor::BgConvolutionImageSource::m_variance
std::shared_ptr< DetectionImage > m_variance
Definition
BgConvolutionImageSource.h:53
SourceXtractor::BgConvolutionImageSource::BgConvolutionImageSource
BgConvolutionImageSource(std::shared_ptr< Image< DetectionImage::PixelType > > image, std::shared_ptr< DetectionImage > variance, SeFloat threshold, std::shared_ptr< VectorImage< SeFloat > > kernel)
Definition
BgConvolutionImageSource.cpp:31
SourceXtractor::BgConvolutionImageSource::m_kernel
std::shared_ptr< VectorImage< SeFloat > > m_kernel
Definition
BgConvolutionImageSource.h:55
SourceXtractor::Image< SeFloat >
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition
Image.h:48
SourceXtractor::MirrorImage
Mirrors an image in both X and Y axes.
Definition
MirrorImage.h:37
SourceXtractor::ProcessingImageSource
Definition
ProcessingImageSource.h:33
SourceXtractor::ProcessingImageSource< DetectionImage::PixelType >::getImageRepr
std::string getImageRepr() const
Definition
ProcessingImageSource.h:70
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition
VectorImage.h:100
std::function
std::make_pair
T make_pair(T... args)
std::max
T max(T... args)
std::min
T min(T... args)
SourceXtractor
Definition
Aperture.h:30
SourceXtractor::SeFloat
SeFloat32 SeFloat
Definition
Types.h:32
SourceXtractor::applyKernel
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)
Definition
BgConvolutionImageSource.cpp:44
std::shared_ptr
std::tie
T tie(T... args)
Generated by
1.10.0