8 #ifndef META_OCEAN_CV_FRAME_FILTER_MAX_H
9 #define META_OCEAN_CV_FRAME_FILTER_MAX_H
77 template <
typename T,
unsigned int tChannels>
78 static void filter(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
Worker* worker =
nullptr);
91 template <
typename T,
unsigned int tChannels>
92 static void filter(T* frame,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
const unsigned int filterSize,
Worker* worker =
nullptr);
110 template <
typename T,
unsigned int tChannels,
typename THistogram>
111 static void filterHistogramSubset(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
const unsigned int firstRow,
const unsigned int numberRows);
127 template <
typename T,
unsigned int tChannels>
128 static void filterSequentialSubset(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
const unsigned int firstRow,
const unsigned int numberRows);
131 template <
typename T,
unsigned int tChannels>
132 void FrameFilterMax::filter(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
Worker* worker)
134 static_assert(tChannels != 0u,
"Invalid channel number!");
136 ocean_assert(source !=
nullptr && target !=
nullptr && source != target);
137 ocean_assert(filterSize / 2u <= width && filterSize / 2u <= height);
139 if constexpr (std::is_floating_point<T>::value ||
sizeof(T) >
sizeof(uint16_t))
143 worker->
executeFunction(
Worker::Function::createStatic(&filterSequentialSubset<T, tChannels>, source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, 0u), 0u, height, 7u, 8u, 20u);
147 filterSequentialSubset<T, tChannels>(source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, height);
150 else if (filterSize < 5u)
154 worker->
executeFunction(
Worker::Function::createStatic(&filterSequentialSubset<T, tChannels>, source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, 0u), 0u, height, 7u, 8u, 20u);
158 filterSequentialSubset<T, tChannels>(source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, height);
163 ocean_assert(!std::is_floating_point<T>::value);
167 constexpr
size_t histogramElements = 1 <<
sizeof(T) * 8;
169 ocean_assert(
sizeof(T) !=
sizeof(uint8_t) || histogramElements == 256);
170 ocean_assert(
sizeof(T) !=
sizeof(uint16_t) || histogramElements == 65536);
176 worker->
executeFunction(
Worker::Function::createStatic(&filterHistogramSubset<T, tChannels, Histogram>, source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, 0u), 0u, height, 7u, 8u, 20u);
180 filterHistogramSubset<T, tChannels, Histogram>(source, target, width, height, sourcePaddingElements, targetPaddingElements, filterSize, 0u, height);
185 template <
typename T,
unsigned int tChannels>
186 void FrameFilterMax::filter(T* frame,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
const unsigned int filterSize,
Worker* worker)
188 static_assert(tChannels != 0u,
"Invalid channel number!");
190 ocean_assert(frame !=
nullptr);
191 ocean_assert(filterSize / 2u <= width && filterSize / 2u <= height);
193 Memory memory(width * height *
sizeof(T) * tChannels);
195 constexpr
unsigned int memoryPaddingElements = 0u;
196 filter<T, tChannels>(frame, memory.
data<T>(), width, height, framePaddingElements, memoryPaddingElements, filterSize, worker);
198 if (framePaddingElements == 0u)
200 memcpy(frame, memory.
data(), memory.
size());
204 const T* memoryData = memory.
data<T>();
206 for (
unsigned int y = 0u; y < height; ++y)
208 memcpy(frame, memoryData, width * tChannels *
sizeof(T));
210 frame += width * tChannels + framePaddingElements;
211 memoryData += width * tChannels + memoryPaddingElements;
216 template <
typename T,
unsigned int tChannels,
typename THistogram>
217 void FrameFilterMax::filterHistogramSubset(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
const unsigned int firstRow,
const unsigned int numberRows)
219 static_assert(!std::is_floating_point<T>::value,
"Invalid data type!");
220 static_assert(tChannels != 0u,
"Invalid channel number");
222 ocean_assert(source !=
nullptr && target !=
nullptr);
224 ocean_assert(filterSize >= 3u && filterSize % 2u == 1u);
226 const unsigned int filterSize_2 = filterSize / 2u;
227 ocean_assert(filterSize_2 <= width && filterSize_2 <= height);
229 const unsigned int endRow = firstRow + numberRows;
231 const unsigned int sourceStrideElements = width * tChannels + sourcePaddingElements;
232 const unsigned int targetStrideElements = width * tChannels + targetPaddingElements;
234 THistogram histogram[tChannels];
238 for (
unsigned int y =
clampLower(firstRow, filterSize_2); y <=
clampUpper(firstRow, filterSize_2, height); ++y)
240 const T* sourceRow = source + y * sourceStrideElements;
242 for (
unsigned int x = 0u; x <= filterSize_2; ++x)
244 for (
unsigned int n = 0u; n < tChannels; ++n)
246 histogram[n].pushValue(*sourceRow++);
251 ocean_assert(histogram[0].values() >= (filterSize_2 + 1u) * (filterSize_2 + 1u));
252 ocean_assert(histogram[0].values() <= (filterSize_2 + 1u) * filterSize);
254 THistogram previousHistograms[tChannels];
256 for (
unsigned int y = firstRow; y < endRow; ++y)
258 T* targetRow = target + y * targetStrideElements;
261 for (
unsigned int n = 0u; n < tChannels; ++n)
263 previousHistograms[n] = histogram[n];
266 for (
unsigned int x = 0u; x < width; ++x)
268 for (
unsigned int n = 0u; n < tChannels; ++n)
270 ocean_assert(histogram[n]);
271 *targetRow++ = histogram[n].maxValue();
276 ocean_assert(x + 1u < width);
280 for (
unsigned int yy =
clampLower(y, filterSize_2); yy <=
clampUpper(y, filterSize_2, height); ++yy)
282 const T* sourceRow = source + yy * sourceStrideElements;
284 const unsigned int xxLeft = x - filterSize_2;
285 const unsigned int xxRight = x + filterSize_2 + 1u;
289 for (
unsigned int n = 0u; n < tChannels; ++n)
291 const T popValue = sourceRow[xxLeft * tChannels + n];
293 ocean_assert(histogram[n].hasValue(popValue));
294 histogram[n].popValue(popValue);
300 for (
unsigned int n = 0u; n < tChannels; ++n)
302 const T pushValue = sourceRow[xxRight * tChannels + n];
304 histogram[n].pushValue(pushValue);
310 ocean_assert(histogram[0].values() <= filterSize * filterSize);
313 if (y != endRow - 1u)
315 ocean_assert(y + 1u < endRow);
319 for (
unsigned int n = 0u; n < tChannels; ++n)
321 histogram[n] = previousHistograms[n];
324 const unsigned int yyTop = y - filterSize_2;
325 const unsigned int yyBottom = y + filterSize_2 + 1u;
329 const T* sourceRow = source + yyTop * sourceStrideElements;
331 for (
unsigned int x = 0u; x <= filterSize_2; ++x)
333 for (
unsigned int n = 0u; n < tChannels; ++n)
335 const T popValue = sourceRow[x * tChannels + n];
337 ocean_assert(histogram[n].hasValue(popValue));
338 histogram[n].popValue(popValue);
343 if (yyBottom < height)
345 const T* sourceRow = source + yyBottom * sourceStrideElements;
347 for (
unsigned int x = 0u; x <= filterSize_2; ++x)
349 for (
unsigned int n = 0u; n < tChannels; ++n)
351 const T pushValue = sourceRow[x * tChannels + n];
353 histogram[n].pushValue(pushValue);
361 template <
typename T,
unsigned int tChannels>
362 void FrameFilterMax::filterSequentialSubset(
const T* source, T* target,
const unsigned int width,
const unsigned int height,
const unsigned int sourcePaddingElements,
const unsigned int targetPaddingElements,
const unsigned int filterSize,
const unsigned int firstRow,
const unsigned int numberRows)
364 static_assert(tChannels != 0u,
"Invalid channel number");
366 ocean_assert(source !=
nullptr && target !=
nullptr);
367 ocean_assert(firstRow + numberRows <= height);
369 const unsigned int sourceStrideElements = width * tChannels + sourcePaddingElements;
370 const unsigned int targetStrideElements = width * tChannels + targetPaddingElements;
372 const unsigned int filterSize_2 = filterSize / 2u;
374 T maxValues[tChannels];
376 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
378 T* targetRow = target + y * targetStrideElements;
380 for (
unsigned int x = 0u; x < width; ++x)
382 for (
unsigned int n = 0u; n < tChannels; ++n)
387 for (
unsigned int xx = max(0,
int(x) -
int(filterSize_2)); xx < min(x + filterSize_2 + 1u, width); ++xx)
389 for (
unsigned int yy = max(0,
int(y) -
int(filterSize_2)); yy < min(y + filterSize_2 + 1u, height); ++yy)
391 ocean_assert(xx < width && yy < height);
393 const T*
const sourcePixel = source + yy * sourceStrideElements + xx * tChannels;
395 for (
unsigned int n = 0u; n < tChannels; ++n)
397 if (sourcePixel[n] > maxValues[n])
399 maxValues[n] = sourcePixel[n];
405 for (
unsigned int n = 0u; n < tChannels; ++n)
407 targetRow[n] = maxValues[n];
410 targetRow += tChannels;
The following comfort class provides comfortable functions simplifying prototyping applications but a...
Definition: FrameFilterMax.h:39
static bool filter(Frame &frame, const unsigned int filterSize, Worker *worker)
Filters a frame with a max filter with arbitrary size (a square patch).
static bool filter(const Frame &source, Frame &target, const unsigned int filterSize, Worker *worker)
Filters a frame with a max filter with arbitrary size (a square patch).
This class implements filters based on the max function.
Definition: FrameFilterMax.h:30
static void filterSequentialSubset(const T *source, T *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int filterSize, const unsigned int firstRow, const unsigned int numberRows)
Filters a subset of a floating point frame with a max filter with arbitrary size.
Definition: FrameFilterMax.h:362
static void filterHistogramSubset(const T *source, T *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int filterSize, const unsigned int firstRow, const unsigned int numberRows)
Filters a subset of an integer frame with a max filter with arbitrary size.
Definition: FrameFilterMax.h:217
static void filter(const T *source, T *target, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int filterSize, Worker *worker=nullptr)
Filters a frame with a max filter with arbitrary size (a square patch).
Definition: FrameFilterMax.h:132
This class implements a histogram for integer values.
Definition: FrameFilterSorted.h:42
This class implements the base class for all filters relying on sorted filter values.
Definition: FrameFilterSorted.h:31
static unsigned int clampLower(const unsigned int index, const unsigned int lowerOffset)
Returns the lower clamped offset to an index.
Definition: FrameFilterSorted.h:428
static unsigned int clampUpper(const unsigned int index, const unsigned int upperOffset, const unsigned int size)
Returns the upper clamped offset to an index.
Definition: FrameFilterSorted.h:433
This class implements an image histogram.
Definition: Histogram.h:32
static Caller< void > createStatic(typename StaticFunctionPointerMaker< void, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass >::Type function)
Creates a new caller container for a static function with no function parameter.
Definition: Caller.h:2876
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
size_t size() const
Returns the size of the memory in bytes.
Definition: base/Memory.h:386
void * data()
Returns the pointer to the writable memory which is allocated by this object.
Definition: base/Memory.h:303
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static constexpr T minValue()
Returns the min scalar value.
Definition: Numeric.h:3250
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15