Skip to content

Add FastCV DSP Initialization, QcAllocator and FastCV DSP Extension APIs #3931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 4.x
Choose a base branch
from
7 changes: 7 additions & 0 deletions modules/fastcv/include/opencv2/fastcv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
#include "opencv2/fastcv/thresh.hpp"
#include "opencv2/fastcv/tracking.hpp"
#include "opencv2/fastcv/warp.hpp"
#include "opencv2/fastcv/allocator.hpp"
#include "opencv2/fastcv/dsp_init.hpp"
#include "opencv2/fastcv/sad_dsp.hpp"
#include "opencv2/fastcv/thresh_dsp.hpp"
#include "opencv2/fastcv/fft_dsp.hpp"
#include "opencv2/fastcv/edges_dsp.hpp"
#include "opencv2/fastcv/blur_dsp.hpp"

/**
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
Expand Down
64 changes: 64 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/allocator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_ALLOCATOR_HPP
#define OPENCV_FASTCV_ALLOCATOR_HPP

#include <opencv2/core.hpp>
#include <set>
#include <mutex>

namespace cv {
namespace fastcv {

//! @addtogroup fastcv
//! @{

/**
* @brief Resource manager for FastCV allocations.
* This class manages active allocations.
*/
class QcResourceManager {
public:
static QcResourceManager& getInstance();

void addAllocation(void* ptr);
void removeAllocation(void* ptr);

private:
QcResourceManager() = default;
std::set<void*> activeAllocations;
std::mutex resourceMutex;
};

/**
* @brief Qualcomm's custom allocator.
* This allocator uses Qualcomm's memory management functions.
*/
class QcAllocator : public cv::MatAllocator {
public:
QcAllocator();
~QcAllocator();

cv::UMatData* allocate(int dims, const int* sizes, int type, void* data0, size_t* step, cv::AccessFlag flags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
bool allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE;
void deallocate(cv::UMatData* u) const CV_OVERRIDE;
};

/**
* @brief Gets the default Qualcomm's allocator.
* This function returns a pointer to the default Qualcomm's allocator, which is optimized
* for use with DSP.
*
* @return Pointer to the default FastCV allocator.
*/
CV_EXPORTS cv::MatAllocator* getQcAllocator();

//! @}

} // namespace fastcv
} // namespace cv

#endif // OPENCV_FASTCV_ALLOCATOR_HPP
34 changes: 34 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/blur_dsp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_BLUR_DSP_HPP
#define OPENCV_FASTCV_BLUR_DSP_HPP

#include <opencv2/core.hpp>

namespace cv {
namespace fastcv {
namespace dsp {
/**
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
*/

/**
* @brief Filter an image with non-separable kernel
* @param _src Intput image with type CV_8UC1, src size should be greater than 176*144
* @param _dst Output image with type CV_8UC1, CV_16SC1 or CV_32FC1
* @param ddepth The depth of output image
* @param _kernel Filer kernel data
*
* @sa Filter2D
*/
CV_EXPORTS void filter2D(InputArray _src, OutputArray _dst, int ddepth, InputArray _kernel);

//! @}
} // dsp::
} // fastcv::
} // cv::

#endif // OPENCV_FASTCV_BLUR_DSP_HPP
49 changes: 49 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/dsp_init.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_DSP_INIT_HPP
#define OPENCV_FASTCV_DSP_INIT_HPP

#include <opencv2/core.hpp>

namespace cv {
namespace fastcv {
namespace dsp {

//! @addtogroup fastcv
//! @{

/**
* @brief Initializes the FastCV DSP environment.
*
* This function sets up the necessary environment and resources for the DSP to operate.
* It must be called once at the very beginning of the use case or program to ensure that
* the DSP is properly initialized before any DSP-related operations are performed.
*
* @note This function must be called at the start of the use case or program, before any
* DSP-related operations.
*
* @return int Returns 0 on success, and a non-zero value on failure.
*/
CV_EXPORTS int fcvdspinit();

/**
* @brief Deinitializes the FastCV DSP environment.
*
* This function releases the resources and environment set up by the 'fcvdspinit' function.
* It should be called before the use case or program exits to ensure that all DSP resources
* are properly cleaned up and no memory leaks occur.
*
* @note This function must be called at the end of the use case or program, after all DSP-related
* operations are complete.
*/
CV_EXPORTS void fcvdspdeinit();
//! @}

} // dsp::
} // fastcv::
} // cv::

#endif // OPENCV_FASTCV_DSP_INIT_HPP
38 changes: 38 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/edges_dsp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_EDGES_DSP_HPP
#define OPENCV_FASTCV_EDGES_DSP_HPP

#include "opencv2/core/mat.hpp"

namespace cv {
namespace fastcv {
namespace dsp {

/**
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
*/

//! @addtogroup fastcv
//! @{

/**
* @brief Canny edge detector applied to a 8 bit grayscale image
* @param _src Input image with type CV_8UC1
* @param _dst Output 8-bit image containing the edge detection results
* @param lowThreshold First threshold
* @param highThreshold Second threshold
* @param apertureSize The Sobel kernel size for calculating gradient. Supported sizes are 3, 5 and 7.
* @param L2gradient L2 Gradient or L1 Gradient
*/
CV_EXPORTS void Canny(InputArray _src, OutputArray _dst, int lowThreshold, int highThreshold, int apertureSize = 3, bool L2gradient = false);
//! @}

} // dsp::
} // fastcv::
} // cv::

#endif //OPENCV_FASTCV_EDGES_DSP_HPP
49 changes: 49 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/fft_dsp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_FFT_DSP_HPP
#define OPENCV_FASTCV_FFT_DSP_HPP

#include <opencv2/core.hpp>

namespace cv {
namespace fastcv {
namespace dsp {

//! @addtogroup fastcv
//! @{

/**
* @brief Computes the 1D or 2D Fast Fourier Transform of a real valued matrix.
For the 2D case, the width and height of the input and output matrix must be powers of 2.
For the 1D case, the height of the matrices must be 1, while the width must be a power of 2.

* @param src Input array of CV_8UC1. The dimensions of the matrix must be powers of 2 for the 2D case,
and in the 1D case, the height must be 1, while the width must be a power of 2.
* @param dst The computed FFT matrix of type CV_32FC2. The FFT Re and Im coefficients are stored in different channels.
Hence the dimensions of the dst are (srcWidth, srcHeight)
*/
CV_EXPORTS void FFT(InputArray src, OutputArray dst);

/**
* @brief Computes the 1D or 2D Inverse Fast Fourier Transform of a complex valued matrix.
For the 2D case, The width and height of the input and output matrix must be powers of 2.
For the 1D case, the height of the matrices must be 1, while the width must be a power of 2.

* @param src Input array of type CV_32FC2 containing FFT Re and Im coefficients stored in separate channels.
The dimensions of the matrix must be powers of 2 for the 2D case, and in the 1D case, the height must be 1,
while the width must be a power of 2.
* @param dst The computed IFFT matrix of type CV_8U. The matrix is real valued and has no imaginary components.
Hence the dimensions of the dst are (srcWidth , srcHeight)
*/
CV_EXPORTS void IFFT(InputArray src, OutputArray dst);

//! @}

} // dsp::
} // fastcv::
} // cv::

#endif // OPENCV_FASTCV_FFT_DSP_HPP
34 changes: 34 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/sad_dsp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_SAD_HPP
#define OPENCV_FASTCV_SAD_HPP

#include <opencv2/core.hpp>

namespace cv {
namespace fastcv {
namespace dsp {

/**
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions
*/

//! @addtogroup fastcv
//! @{
/**
* @brief Sum of absolute differences of an image against an 8x8 template.
* @param _patch The first input image data, type CV_8UC1
* @param _src The input image data, type CV_8UC1
* @param _dst The output image data, type CV_16UC1
*/
CV_EXPORTS void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst);
//! @}

} // dsp::
} // fastcv::
} // cv::

#endif // OPENCV_FASTCV_SAD_HPP
39 changes: 39 additions & 0 deletions modules/fastcv/include/opencv2/fastcv/thresh_dsp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef OPENCV_FASTCV_THRESH_DSP_HPP
#define OPENCV_FASTCV_THRESH_DSP_HPP

#include <opencv2/core.hpp>

namespace cv {
namespace fastcv {
namespace dsp {

//! @addtogroup fastcv
//! @{

/**
* @brief Binarizes a grayscale image using Otsu's method.
* Sets the pixel to max(255) if it's value is greater than the threshold;
* else, set the pixel to min(0). The threshold is searched that minimizes
* the intra-class variance (the variance within the class).
*
* @param _src Input 8-bit grayscale image. Size of buffer is srcStride*srcHeight bytes.
* @param _dst Output 8-bit binarized image. Size of buffer is dstStride*srcHeight bytes.
* @param type Threshold type that can be either 0 or 1.
* NOTE: For threshold type=0, the pixel is set as
* maxValue if it's value is greater than the threshold; else, it is set as zero.
* For threshold type=1, the pixel is set as zero if it's
* value is greater than the threshold; else, it is set as maxValue.
*/
CV_EXPORTS void thresholdOtsu(InputArray _src, OutputArray _dst, bool type);

//! @}
} // dsp::
} // fastcv::
} // cv::

#endif // OPENCV_FASTCV_THRESH_DSP_HPP
Loading
Loading