Ocean
FrameFilterLaplace.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_FRAME_FILTER_LAPLACE_H
9 #define META_OCEAN_CV_FRAME_FILTER_LAPLACE_H
10 
11 #include "ocean/cv/CV.h"
13 
14 #include "ocean/base/Worker.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 /**
23  * This class implements Laplace frame filter.
24  * The default Laplace filter is a 3x3 box filter with (positive peak):
25  * <pre>
26  * | 0 -1 0 |
27  * | -1 4 -1 |
28  * | 0 -1 0 |
29  * </pre>
30  * @ingroup cv
31  */
32 class OCEAN_CV_EXPORT FrameFilterLaplace
33 {
34  public:
35 
36  /**
37  * Filters a given 1 channel 8 bit frame using a 3x3 Laplace filter with (with positive peak).
38  * The border pixels of the target frame will be set to zero.<br>
39  * The filter response is divided by 8 (normalized by 1/8),
40  * so that each filter response has a value range between [-127, 127].
41  * @param source The source frame to be filtered, must be valid
42  * @param target the target frame receiving the filter result, with same image resolution as the source frame, must be valid
43  * @param width The width of the source frame (and target frame) in pixel, with range [3, infinity)
44  * @param height The height of the source frame (and target frame) in pixel, with range [3, infinity)
45  * @param worker Optional worker object to distribute the computation
46  * @param sourcePaddingElements Optional number of padding elements at the end of each source row, with range [0, infinity)
47  * @param targetPaddingElements Optional number of padding elements at the end of each target row, with range [0, infinity)
48  */
49  static inline void filter1Channel8Bit(const uint8_t* source, int8_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
50 
51  /**
52  * Filters a given 1 channel 8 bit frame using a 3x3 Laplace filter with (with positive peak).
53  * The border pixels of the target frame will be set to zero.<br>
54  * The filter response is not normalized, so that each filter response has a value range between [-255 * 4, 255 * 4].
55  * @param source The source frame to be filtered, must be valid
56  * @param target the target frame receiving the filter result, with same image resolution as the source frame, must be valid
57  * @param width The width of the source frame (and target frame) in pixel, with range [3, infinity)
58  * @param height The height of the source frame (and target frame) in pixel, with range [3, infinity)
59  * @param worker Optional worker object to distribute the computation
60  * @param sourcePaddingElements Optional number of padding elements at the end of each source row, with range [0, infinity)
61  * @param targetPaddingElements Optional number of padding elements at the end of each target row, with range [0, infinity)
62  */
63  static inline void filter1Channel8Bit(const uint8_t* source, int16_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
64 
65  /**
66  * Filters a given 1 channel 8 bit frame using a Laplace filter and returns the magnitude.
67  * For all border pixels the corresponding subset of the Laplace filter will be applied.
68  * @param source The source frame to be filtered, must be valid
69  * @param target the target frame receiving the filter result, with same image resolution as the source frame, must be valid
70  * @param width The width of the source frame (and target frame) in pixel, with range [3, infinity)
71  * @param height The height of the source frame (and target frame) in pixel, with range [3, infinity)
72  * @param worker Optional worker object to distribute the computation
73  * @param sourcePaddingElements Optional number of padding elements at the end of each source row, with range [0, infinity)
74  * @param targetPaddingElements Optional number of padding elements at the end of each target row, with range [0, infinity)
75  */
76  static void filterMagnitude1Channel8Bit(const uint8_t* source, uint16_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
77 
78  /**
79  * Filters a given 1 channel 8 bit frame using a Laplace filter and returns the magnitude and normalizes the filter response to the range [0, 255].
80  * @param source The source frame to be filtered, must be valid
81  * @param target The target frame receiving the filter result, must be valid
82  * @param width The width of the source frame in pixel, with range [1, infinity)
83  * @param height The height of the source frame in pixel, with range [1, infinity)
84  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
85  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
86  * @param worker Optional worker object to distribute the computation
87  */
88  static void filterNormalizedMagnitude1Channel8Bit(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
89 
90  /**
91  * Determines the variance of all Laplace responses for a given 1-channel 8 bit frame.
92  * This function ignores all pixels at the image border.
93  * @param frame The frame to be filtered, must be valid
94  * @param width The width of the frame, in pixels, with range [3, 33026]
95  * @param height The height of the frame, in pixels, with range [3, infinity)
96  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
97  * @return The resulting variance in the Laplace responses, with range [0, infinity)
98  */
99  static double variance1Channel8Bit(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements);
100 
101 #if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
102 
103  /**
104  * Applies the Laplace filter to one row and determines the sum of responses and the sum of squared responses for a given 1-channel 8 bit row.
105  * This function ignores the first and last pixel in the row.
106  * @param row The row to which the filter will be applied (must have a row above and a row below), must be valid
107  * @param width The width of the frame, in pixels, with range [3, 33026]
108  * @param rowStrideElements The number of stride elements between the start location of two consecutive rows, in elements, with range [width, infinity)
109  * @param sumResponse_s_32x4x2 Two sum of responses, when using NEON
110  * @param sumSqrResponse_u_32x4x2 Two sum of squared responses, when using NEON
111  * @param sumResponse_s_32x1 One sum of responses, as backup when NEON cannot be applied
112  * @param sumSqrResponse_u_32x1 One sum of squared responses, as backup when NEON cannot be applied
113  */
114  static void varianceRow1Channel8BitNEON(const uint8_t* row, const unsigned int width, const unsigned int rowStrideElements, int32x4x2_t& sumResponse_s_32x4x2, uint32x4x2_t& sumSqrResponse_u_32x4x2, int32_t& sumResponse_s_32x1, uint32_t& sumSqrResponse_u_32x1);
115 
116 #endif // OCEAN_HARDWARE_NEON_VERSION
117 
118  private:
119 
120  /**
121  * Applies the Laplace filter to a row and stores the absolute response.
122  * @param sourceRow The row of the source frame, must be valid
123  * @param targetRow The row of the target response frame, must be valid
124  * @param width The width of the source and target frame in pixel, with range [3, infinity)
125  * @param height The height of the source and target frame in pixel, with range [3, infinity)
126  * @param rowIndex The index of the row to which the filter is applied, with range [0, height - 1]
127  * @param sourceStrideElements The number of elements between the start of two consecutive source rows, with range [width * tSourceChannels, infinity)
128  * @param targetStrideElements The number of elements between the start of two consecutive target rows, with range [width * tTargetChannels, infinity)
129  */
130  static void filterAbsoluteRow(const uint8_t* sourceRow, uint16_t* targetRow, const unsigned int width, const unsigned int height, unsigned int rowIndex, const unsigned int sourceStrideElements, const unsigned int targetStrideElements);
131 };
132 
133 inline void FrameFilterLaplace::filter1Channel8Bit(const uint8_t* source, int8_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
134 {
135  ocean_assert(source != nullptr && target != nullptr);
136  ocean_assert(width >= 3u && height >= 3u);
137 
138  /*
139  * | 0 -1 0 |
140  * | -1 4 -1 | * 1/8
141  * | 0 -1 0 |
142  */
143  typedef FrameFilterTemplate<int16_t, 0, -1, 0, -1, 4, -1, 0, -1, 0> LaplaceFilter;
144 
145  constexpr int16_t normalization = 8;
146  constexpr int16_t normalizationBias = 0;
147 
148  constexpr unsigned int channels = 1u;
149 
150  LaplaceFilter::filter<uint8_t, int8_t, int16_t, normalization, normalizationBias, channels, PD_NORTH>(source, target, width, height, sourcePaddingElements, targetPaddingElements, worker);
151 }
152 
153 inline void FrameFilterLaplace::filter1Channel8Bit(const uint8_t* source, int16_t* target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
154 {
155  ocean_assert(source != nullptr && target != nullptr);
156  ocean_assert(width >= 3u && height >= 3u);
157 
158  /*
159  * | 0 -1 0 |
160  * | -1 4 -1 |
161  * | 0 -1 0 |
162  */
163  typedef FrameFilterTemplate<int16_t, 0, -1, 0, -1, 4, -1, 0, -1, 0> LaplaceFilter;
164 
165  constexpr short normalization = 1;
166  constexpr short normalizationBias = 0;
167 
168  constexpr unsigned int channels = 1u;
169 
170  LaplaceFilter::filter<uint8_t, int16_t, int16_t, normalization, normalizationBias, channels, PD_NORTH>(source, target, width, height, sourcePaddingElements, targetPaddingElements, worker);
171 }
172 
173 }
174 
175 }
176 
177 #endif // META_OCEAN_CV_FRAME_FILTER_LAPLACE_H
This class implements Laplace frame filter.
Definition: FrameFilterLaplace.h:33
static void filterAbsoluteRow(const uint8_t *sourceRow, uint16_t *targetRow, const unsigned int width, const unsigned int height, unsigned int rowIndex, const unsigned int sourceStrideElements, const unsigned int targetStrideElements)
Applies the Laplace filter to a row and stores the absolute response.
static double variance1Channel8Bit(const uint8_t *frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements)
Determines the variance of all Laplace responses for a given 1-channel 8 bit frame.
static void filterMagnitude1Channel8Bit(const uint8_t *source, uint16_t *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Filters a given 1 channel 8 bit frame using a Laplace filter and returns the magnitude.
static void varianceRow1Channel8BitNEON(const uint8_t *row, const unsigned int width, const unsigned int rowStrideElements, int32x4x2_t &sumResponse_s_32x4x2, uint32x4x2_t &sumSqrResponse_u_32x4x2, int32_t &sumResponse_s_32x1, uint32_t &sumSqrResponse_u_32x1)
Applies the Laplace filter to one row and determines the sum of responses and the sum of squared resp...
static void filter1Channel8Bit(const uint8_t *source, int8_t *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Filters a given 1 channel 8 bit frame using a 3x3 Laplace filter with (with positive peak).
Definition: FrameFilterLaplace.h:133
static void filterNormalizedMagnitude1Channel8Bit(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Filters a given 1 channel 8 bit frame using a Laplace filter and returns the magnitude and normalizes...
This class implements a 3x3 filter with arbitrary filter pattern.
Definition: FrameFilterTemplate.h:49
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15