1
0
mirror of https://github.com/opencv/opencv_contrib.git synced 2025-10-16 22:35:51 +08:00
Files
opencv_contrib/modules/fastcv/test/test_thresh_dsp.cpp
Aakash Preetam 6e8ce301e0 Merge pull request #3931 from CodeLinaro:apreetam_5thPost
Add FastCV DSP Initialization, QcAllocator and FastCV DSP Extension APIs #3931

Merge with https://github.com/opencv/opencv/pull/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: https://github.com/opencv/opencv_3rdparty/pull/97
Requires binary from https://github.com/opencv/opencv_3rdparty/pull/95

Lib Hash Update: https://github.com/opencv/opencv/pull/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
2025-06-06 12:30:12 +03:00

93 lines
2.9 KiB
C++

/*
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
TEST(ThresholdOtsuTest, accuracy)
{
applyTestTag(CV_TEST_TAG_FASTCV_SKIP_DSP);
//Initialize DSP
int initStatus = cv::fastcv::dsp::fcvdspinit();
ASSERT_EQ(initStatus, 0) << "Failed to initialize FastCV DSP";
cv::Mat src;
src.allocator = cv::fastcv::getQcAllocator();
cv::imread(cvtest::findDataFile("cv/detectors_descriptors_evaluation/planar/box_in_scene.png"), src, cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(src.empty()) << "Could not read the image file.";
cv::Mat dst;
dst.allocator = cv::fastcv::getQcAllocator();
bool type = 0;
cv::fastcv::dsp::thresholdOtsu(src, dst, type);
// De-Initialize DSP
cv::fastcv::dsp::fcvdspdeinit();
EXPECT_FALSE(dst.empty());
EXPECT_EQ(src.size(), dst.size());
// Compare the result against the reference cv::threshold function with Otsu's method
cv::Mat referenceDst;
cv::threshold(src, referenceDst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
double maxDifference = 10.0;
cv::Mat diff;
cv::absdiff(dst, referenceDst, diff);
double maxVal;
cv::minMaxLoc(diff, nullptr, &maxVal);
EXPECT_LE(maxVal, maxDifference) << "The custom threshold result differs from the reference result by more than the acceptable threshold.";
}
TEST(ThresholdOtsuTest, inPlaceAccuracy)
{
applyTestTag(CV_TEST_TAG_FASTCV_SKIP_DSP);
// Initialize DSP
int initStatus = cv::fastcv::dsp::fcvdspinit();
ASSERT_EQ(initStatus, 0) << "Failed to initialize FastCV DSP";
cv::Mat src;
src.allocator = cv::fastcv::getQcAllocator();
cv::imread(cvtest::findDataFile("cv/detectors_descriptors_evaluation/planar/box_in_scene.png"), src, cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(src.empty()) << "Could not read the image file.";
// Use the same buffer for in-place operation
cv::Mat dst;
dst.allocator = cv::fastcv::getQcAllocator();
src.copyTo(dst);
bool type = false;
// Call the thresholdOtsu function for in-place operation
cv::fastcv::dsp::thresholdOtsu(dst, dst, type);
// De-Initialize DSP
cv::fastcv::dsp::fcvdspdeinit();
// Check if the output is not empty
EXPECT_FALSE(dst.empty());
EXPECT_EQ(src.size(), dst.size());
// Compare the result against the reference cv::threshold function with Otsu's method
cv::Mat referenceDst;
cv::threshold(src, referenceDst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
double maxDifference = 10.0;
cv::Mat diff;
cv::absdiff(dst, referenceDst, diff);
double maxVal;
cv::minMaxLoc(diff, nullptr, &maxVal);
EXPECT_LE(maxVal, maxDifference) << "The in-place threshold result differs from the reference result by more than the acceptable threshold.";
}
}} // namespaces opencv_test, ::