Skip to content

Commit 6e8ce30

Browse files
Merge pull request #3931 from CodeLinaro:apreetam_5thPost
Add FastCV DSP Initialization, QcAllocator and FastCV DSP Extension APIs #3931 Merge with opencv/opencv#27290 **Detailed Description** This PR introduces FastCV DSP Extension APIs within the '**cv::fastcv::dsp**' namespace. The following APIs have been added: 1. **fcvdspinit**: Initializes the FastCV DSP environment. 2. **fcvdspdeinit**: Deinitializes the FastCV DSP environment. 3. **sumOfAbsoluteDiffs**: Computes the sum of absolute differences of an image against an 8x8 template. 4. **thresholdOtsu**: Binarizes a grayscale image using Otsu's method. 5. **FFT**: Computes the 1D or 2D Fast Fourier Transform of a real-valued matrix. 6. **IFFT**: Computes the 1D or 2D Inverse Fast Fourier Transform of a complex-valued matrix. 7. **canny**: Applies the Canny edge detector to an 8-bit grayscale image. 8. **filter2D**: Applies a generic 2D filter to an image. The **QcAllocator** has been added to manage memory allocations on Qualcomm's Chipsets. This allocator ensures that matrices are allocated using the Qualcomm hardware memory allocator, providing efficient DSP operations. Requires updated binary from: opencv/opencv_3rdparty#97 Requires binary from opencv/opencv_3rdparty#95 Lib Hash Update: opencv/opencv#27403 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
1 parent 0a5ac86 commit 6e8ce30

30 files changed

+1584
-4
lines changed

modules/fastcv/include/opencv2/fastcv.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
#include "opencv2/fastcv/thresh.hpp"
3131
#include "opencv2/fastcv/tracking.hpp"
3232
#include "opencv2/fastcv/warp.hpp"
33+
#include "opencv2/fastcv/allocator.hpp"
34+
#include "opencv2/fastcv/dsp_init.hpp"
35+
#include "opencv2/fastcv/sad_dsp.hpp"
36+
#include "opencv2/fastcv/thresh_dsp.hpp"
37+
#include "opencv2/fastcv/fft_dsp.hpp"
38+
#include "opencv2/fastcv/edges_dsp.hpp"
39+
#include "opencv2/fastcv/blur_dsp.hpp"
3340

3441
/**
3542
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_ALLOCATOR_HPP
7+
#define OPENCV_FASTCV_ALLOCATOR_HPP
8+
9+
#include <opencv2/core.hpp>
10+
#include <set>
11+
#include <mutex>
12+
13+
namespace cv {
14+
namespace fastcv {
15+
16+
//! @addtogroup fastcv
17+
//! @{
18+
19+
/**
20+
* @brief Resource manager for FastCV allocations.
21+
* This class manages active allocations.
22+
*/
23+
class QcResourceManager {
24+
public:
25+
static QcResourceManager& getInstance();
26+
27+
void addAllocation(void* ptr);
28+
void removeAllocation(void* ptr);
29+
30+
private:
31+
QcResourceManager() = default;
32+
std::set<void*> activeAllocations;
33+
std::mutex resourceMutex;
34+
};
35+
36+
/**
37+
* @brief Qualcomm's custom allocator.
38+
* This allocator uses Qualcomm's memory management functions.
39+
*/
40+
class QcAllocator : public cv::MatAllocator {
41+
public:
42+
QcAllocator();
43+
~QcAllocator();
44+
45+
cv::UMatData* allocate(int dims, const int* sizes, int type, void* data0, size_t* step, cv::AccessFlag flags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
46+
bool allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
47+
void deallocate(cv::UMatData* u) const CV_OVERRIDE;
48+
};
49+
50+
/**
51+
* @brief Gets the default Qualcomm's allocator.
52+
* This function returns a pointer to the default Qualcomm's allocator, which is optimized
53+
* for use with DSP.
54+
*
55+
* @return Pointer to the default FastCV allocator.
56+
*/
57+
CV_EXPORTS cv::MatAllocator* getQcAllocator();
58+
59+
//! @}
60+
61+
} // namespace fastcv
62+
} // namespace cv
63+
64+
#endif // OPENCV_FASTCV_ALLOCATOR_HPP
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_BLUR_DSP_HPP
7+
#define OPENCV_FASTCV_BLUR_DSP_HPP
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
//! @addtogroup fastcv
16+
//! @{
17+
18+
/**
19+
* @brief Filter an image with non-separable kernel
20+
* @param _src Intput image with type CV_8UC1, src size should be greater than 176*144
21+
* @param _dst Output image with type CV_8UC1, CV_16SC1 or CV_32FC1
22+
* @param ddepth The depth of output image
23+
* @param _kernel Filer kernel data
24+
*/
25+
CV_EXPORTS void filter2D(InputArray _src, OutputArray _dst, int ddepth, InputArray _kernel);
26+
27+
//! @}
28+
29+
} // dsp::
30+
} // fastcv::
31+
} // cv::
32+
33+
#endif // OPENCV_FASTCV_BLUR_DSP_HPP
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_DSP_INIT_HPP
7+
#define OPENCV_FASTCV_DSP_INIT_HPP
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
//! @addtogroup fastcv
16+
//! @{
17+
18+
/**
19+
* @brief Initializes the FastCV DSP environment.
20+
*
21+
* This function sets up the necessary environment and resources for the DSP to operate.
22+
* It must be called once at the very beginning of the use case or program to ensure that
23+
* the DSP is properly initialized before any DSP-related operations are performed.
24+
*
25+
* @note This function must be called at the start of the use case or program, before any
26+
* DSP-related operations.
27+
*
28+
* @return int Returns 0 on success, and a non-zero value on failure.
29+
*/
30+
CV_EXPORTS int fcvdspinit();
31+
32+
/**
33+
* @brief Deinitializes the FastCV DSP environment.
34+
*
35+
* This function releases the resources and environment set up by the 'fcvdspinit' function.
36+
* It should be called before the use case or program exits to ensure that all DSP resources
37+
* are properly cleaned up and no memory leaks occur.
38+
*
39+
* @note This function must be called at the end of the use case or program, after all DSP-related
40+
* operations are complete.
41+
*/
42+
CV_EXPORTS void fcvdspdeinit();
43+
//! @}
44+
45+
} // dsp::
46+
} // fastcv::
47+
} // cv::
48+
49+
#endif // OPENCV_FASTCV_DSP_INIT_HPP
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_EDGES_DSP_HPP
7+
#define OPENCV_FASTCV_EDGES_DSP_HPP
8+
9+
#include "opencv2/core/mat.hpp"
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
/**
16+
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
17+
*/
18+
19+
//! @addtogroup fastcv
20+
//! @{
21+
22+
/**
23+
* @brief Canny edge detector applied to a 8 bit grayscale image
24+
* @param _src Input image with type CV_8UC1
25+
* @param _dst Output 8-bit image containing the edge detection results
26+
* @param lowThreshold First threshold
27+
* @param highThreshold Second threshold
28+
* @param apertureSize The Sobel kernel size for calculating gradient. Supported sizes are 3, 5 and 7.
29+
* @param L2gradient L2 Gradient or L1 Gradient
30+
*/
31+
CV_EXPORTS void Canny(InputArray _src, OutputArray _dst, int lowThreshold, int highThreshold, int apertureSize = 3, bool L2gradient = false);
32+
//! @}
33+
34+
} // dsp::
35+
} // fastcv::
36+
} // cv::
37+
38+
#endif //OPENCV_FASTCV_EDGES_DSP_HPP
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_FFT_DSP_HPP
7+
#define OPENCV_FASTCV_FFT_DSP_HPP
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
//! @addtogroup fastcv
16+
//! @{
17+
18+
/**
19+
* @brief Computes the 1D or 2D Fast Fourier Transform of a real valued matrix.
20+
For the 2D case, the width and height of the input and output matrix must be powers of 2.
21+
For the 1D case, the height of the matrices must be 1, while the width must be a power of 2.
22+
23+
* @param src Input array of CV_8UC1. The dimensions of the matrix must be powers of 2 for the 2D case,
24+
and in the 1D case, the height must be 1, while the width must be a power of 2.
25+
* @param dst The computed FFT matrix of type CV_32FC2. The FFT Re and Im coefficients are stored in different channels.
26+
Hence the dimensions of the dst are (srcWidth, srcHeight)
27+
*/
28+
CV_EXPORTS void FFT(InputArray src, OutputArray dst);
29+
30+
/**
31+
* @brief Computes the 1D or 2D Inverse Fast Fourier Transform of a complex valued matrix.
32+
For the 2D case, The width and height of the input and output matrix must be powers of 2.
33+
For the 1D case, the height of the matrices must be 1, while the width must be a power of 2.
34+
35+
* @param src Input array of type CV_32FC2 containing FFT Re and Im coefficients stored in separate channels.
36+
The dimensions of the matrix must be powers of 2 for the 2D case, and in the 1D case, the height must be 1,
37+
while the width must be a power of 2.
38+
* @param dst The computed IFFT matrix of type CV_8U. The matrix is real valued and has no imaginary components.
39+
Hence the dimensions of the dst are (srcWidth , srcHeight)
40+
*/
41+
CV_EXPORTS void IFFT(InputArray src, OutputArray dst);
42+
43+
//! @}
44+
45+
} // dsp::
46+
} // fastcv::
47+
} // cv::
48+
49+
#endif // OPENCV_FASTCV_FFT_DSP_HPP
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_SAD_HPP
7+
#define OPENCV_FASTCV_SAD_HPP
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
/**
16+
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
17+
*/
18+
19+
//! @addtogroup fastcv
20+
//! @{
21+
/**
22+
* @brief Sum of absolute differences of an image against an 8x8 template.
23+
* @param _patch The first input image data, type CV_8UC1
24+
* @param _src The input image data, type CV_8UC1
25+
* @param _dst The output image data, type CV_16UC1
26+
*/
27+
CV_EXPORTS void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst);
28+
//! @}
29+
30+
} // dsp::
31+
} // fastcv::
32+
} // cv::
33+
34+
#endif // OPENCV_FASTCV_SAD_HPP
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef OPENCV_FASTCV_THRESH_DSP_HPP
7+
#define OPENCV_FASTCV_THRESH_DSP_HPP
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace fastcv {
13+
namespace dsp {
14+
15+
//! @addtogroup fastcv
16+
//! @{
17+
18+
/**
19+
* @brief Binarizes a grayscale image using Otsu's method.
20+
* Sets the pixel to max(255) if it's value is greater than the threshold;
21+
* else, set the pixel to min(0). The threshold is searched that minimizes
22+
* the intra-class variance (the variance within the class).
23+
*
24+
* @param _src Input 8-bit grayscale image. Size of buffer is srcStride*srcHeight bytes.
25+
* @param _dst Output 8-bit binarized image. Size of buffer is dstStride*srcHeight bytes.
26+
* @param type Threshold type that can be either 0 or 1.
27+
* NOTE: For threshold type=0, the pixel is set as
28+
* maxValue if it's value is greater than the threshold; else, it is set as zero.
29+
* For threshold type=1, the pixel is set as zero if it's
30+
* value is greater than the threshold; else, it is set as maxValue.
31+
*/
32+
CV_EXPORTS void thresholdOtsu(InputArray _src, OutputArray _dst, bool type);
33+
34+
//! @}
35+
} // dsp::
36+
} // fastcv::
37+
} // cv::
38+
39+
#endif // OPENCV_FASTCV_THRESH_DSP_HPP

0 commit comments

Comments
 (0)