Ocean
FrequencyAnalysis.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_FREQUENCY_ANALYSIS_H
9 #define META_OCEAN_CV_FREQUENCY_ANALYSIS_H
10 
11 #include "ocean/cv/CV.h"
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Worker.h"
15 
16 #include "ocean/math/Complex.h"
17 
18 namespace Ocean
19 {
20 
21 namespace CV
22 {
23 
24 /**
25  * This class provides frame frequency analysis functions.
26  * @ingroup cv
27  */
28 class OCEAN_CV_EXPORT FrequencyAnalysis
29 {
30  public:
31 
32  /**
33  * Applies a Fourier transformation for a given frame.
34  * @param frame The frame for which the analysis is requested, must be valid
35  * @param frequencies Resulting frequencies, the number of resulting frequency components will be adjusted automatically
36  * @param worker Optional worker object to distribute the computation
37  * @return True, if succeeded
38  */
39  static inline bool image2frequencies(const Frame& frame, Complexes& frequencies, Worker* worker = nullptr);
40 
41  /**
42  * Applies a Fourier transformation for a given frame.
43  * @param frame The frame for which the analysis is requested, must be valid
44  * @param frequencies Resulting frequencies
45  * @param worker Optional worker object to distribute the computation
46  * @return True, if succeeded
47  */
48  static bool image2frequencies(const Frame& frame, Complex* frequencies, Worker* worker = nullptr);
49 
50  /**
51  * Applies an inverse Fourier analysis for given frequencies.
52  * The frame type of the resulting frame must match with the given set of frequencies.
53  * @param frequencies The frequencies for which an inverse Fourier transformation will be applied, each channel must be provided as joined block of frequencies with `frame.channels() * (frame.width() * frame.height())` elements, must be valid
54  * @param frame Resulting frame that defines the resulting frame type, must be valid
55  * @param worker Optional worker to distribute the computation
56  * @return True, if succeeded
57  */
58  static bool frequencies2image(const Complex* frequencies, Frame& frame, Worker* worker = nullptr);
59 
60  /**
61  * Applies a Fourier transformation for a given 1 channel 8 bit frame.
62  * @param frame The frame for which the analysis is requested, must be valid
63  * @param width The width of the frame in pixel, with range [1, infinity)
64  * @param height The height of the frame in pixel, with range [1, infinity)
65  * @param channels The number of interleaved frame channels, with range [1, infinity)
66  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
67  * @param frequencies Resulting frequencies, with `channels * (width * height)` elements, each channel will be provided as joined block of frequencies, must be valid
68  * @param worker Optional worker object to distribute the computation
69  */
70  static void image2frequencies8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int channels, const unsigned int framePaddingElements, Complex* frequencies, Worker* worker = nullptr);
71 
72  /**
73  * Applies an inverse Fourier transformation for a given 1 channel 8 bit frame.
74  * @param frequencies The complex frequencies for which the inverse transformation will be applied
75  * @param width The width of the frame in pixel, with range [1, infinity)
76  * @param height The height of the frame in pixel, with range [1, infinity)
77  * @param channels Number of interleaved frame channels, each channel is provided as joined block of frequencies, with range [1, infinity)
78  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
79  * @param frame Resulting frame data with interleaved data channels
80  * @param worker Optional worker object to distribute the computation
81  */
82  static void frequencies2image8BitPerChannel(const Complex* frequencies, const unsigned int width, const unsigned int height, const unsigned int channels, const unsigned int framePaddingElements, uint8_t* frame, Worker* worker = nullptr);
83 
84  /**
85  * Creates a magnitude frame for a given set of frequencies.
86  * @param frequencies The complex frequencies for which the magnitude frame will be created, each channel needs to be provided as joined block of frequencies, must be valid
87  * @param width The width of the resulting frame (and also the original frame) in pixel
88  * @param height The height of the resulting frame (and also the original frame) in pixel
89  * @param channels The number of channels the frequencies have, with range [1, infinity)
90  * @param octaves Number of octaves that will be displayed
91  * @param shift True, to create a shifted magnitude frame
92  * @return Resulting magnitude frame
93  * @see FourierTransformation::shiftHalfDimension2(), FrameNormalizer::normalizeLogarithmToUint8().
94  */
95  static Frame magnitudeFrame(const Complex* frequencies, const unsigned int width, const unsigned int height, const unsigned int channels, const Scalar octaves = Scalar(5), const bool shift = true);
96 
97  /**
98  * Performs a Fourier transformation on a given frame (that is transformed into a grayscale frame before) and returns a magnitude frame for the resulting set of frequencies.
99  * @param frame The frame for which the magnitude frame will be determined
100  * @param octaves Number of octaves that will be displayed
101  * @param shift True, to create a shifted magnitude frame
102  * @param worker Optional worker object to distribute the computation
103  * @return Resulting magnitude frame
104  * @see FourierTransformation::shiftHalfDimension2(), FrameNormalizer::normalizeLogarithmToUint8().
105  */
106  static Frame magnitudeFrame(const Frame& frame, const Scalar octaves, const bool shift, Worker* worker = nullptr);
107 };
108 
109 inline bool FrequencyAnalysis::image2frequencies(const Frame& frame, Complexes& frequencies, Worker* worker)
110 {
111  ocean_assert(frame.isValid() && frame.numberPlanes() == 1u);
112 
113  frequencies.resize(frame.pixels() * FrameType::channels(frame.pixelFormat()));
114  return image2frequencies(frame, frequencies.data(), worker);
115 }
116 
117 }
118 
119 }
120 
121 #endif // META_OCEAN_CV_FREQUENCY_ANALYSIS_H
This class provides frame frequency analysis functions.
Definition: FrequencyAnalysis.h:29
static bool image2frequencies(const Frame &frame, Complexes &frequencies, Worker *worker=nullptr)
Applies a Fourier transformation for a given frame.
Definition: FrequencyAnalysis.h:109
static void image2frequencies8BitPerChannel(const uint8_t *frame, const unsigned int width, const unsigned int height, const unsigned int channels, const unsigned int framePaddingElements, Complex *frequencies, Worker *worker=nullptr)
Applies a Fourier transformation for a given 1 channel 8 bit frame.
static bool image2frequencies(const Frame &frame, Complex *frequencies, Worker *worker=nullptr)
Applies a Fourier transformation for a given frame.
static bool frequencies2image(const Complex *frequencies, Frame &frame, Worker *worker=nullptr)
Applies an inverse Fourier analysis for given frequencies.
static Frame magnitudeFrame(const Frame &frame, const Scalar octaves, const bool shift, Worker *worker=nullptr)
Performs a Fourier transformation on a given frame (that is transformed into a grayscale frame before...
static void frequencies2image8BitPerChannel(const Complex *frequencies, const unsigned int width, const unsigned int height, const unsigned int channels, const unsigned int framePaddingElements, uint8_t *frame, Worker *worker=nullptr)
Applies an inverse Fourier transformation for a given 1 channel 8 bit frame.
static Frame magnitudeFrame(const Complex *frequencies, const unsigned int width, const unsigned int height, const unsigned int channels, const Scalar octaves=Scalar(5), const bool shift=true)
Creates a magnitude frame for a given set of frequencies.
This class implements Ocean's image class.
Definition: Frame.h:1760
T * data(const unsigned int planeIndex=0u)
Returns a pointer to the pixel data of a specific plane.
Definition: Frame.h:4127
bool isValid() const
Returns whether this frame is valid.
Definition: Frame.h:4416
unsigned int pixels() const
Returns the number of pixels for the frame.
Definition: Frame.h:3161
uint32_t numberPlanes() const
Returns the number of planes of the pixel format of this frame.
Definition: Frame.h:3151
PixelFormat pixelFormat() const
Returns the pixel format of the frame.
Definition: Frame.h:3121
unsigned int channels() const
Returns the number of individual channels the frame has.
Definition: Frame.h:3141
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::complex< Scalar > Complex
Definition of a complex number based on the default floating point precision data type.
Definition: Complex.h:34
std::vector< Complex > Complexes
Definition of a vector holding complex objects.
Definition: Complex.h:40
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15