opencv/modules/photo/perf/perf_inpaint.cpp
Yuya Unno 500c55a808
Merge pull request #25122 from unnonouno:pqueue
Use std::priority_queue in inpaint function for performance improvement #25122

In `cv::inpaint` implementation, it uses a priority queue with O(n) time linear search. For large images it is very slow.
I replaced it with C++'s standard library `std::priority_queue`, that uses O(log(n)) algorithm.
In my use case, it is x10 faster than the original.

### 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
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] 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
2024-03-02 13:43:58 +03:00

61 lines
1.7 KiB
C++

#include "perf_precomp.hpp"
namespace opencv_test
{
CV_ENUM(InpaintingMethod, INPAINT_NS, INPAINT_TELEA)
typedef tuple<Size, InpaintingMethod> InpaintArea_InpaintingMethod_t;
typedef perf::TestBaseWithParam<InpaintArea_InpaintingMethod_t> InpaintArea_InpaintingMethod;
typedef perf::TestBaseWithParam<InpaintingMethod> Perf_InpaintingMethod;
PERF_TEST_P(InpaintArea_InpaintingMethod, inpaint,
testing::Combine(
testing::Values(::perf::szSmall24, ::perf::szSmall32, ::perf::szSmall64),
InpaintingMethod::all()
)
)
{
Mat src = imread(getDataPath("gpu/hog/road.png"));
Size sz = get<0>(GetParam());
int inpaintingMethod = get<1>(GetParam());
Mat mask(src.size(), CV_8UC1, Scalar(0));
Mat result(src.size(), src.type());
Rect inpaintArea(src.cols/3, src.rows/3, sz.width, sz.height);
mask(inpaintArea).setTo(255);
declare.in(src, mask).out(result).time(120);
TEST_CYCLE() inpaint(src, mask, result, 10.0, inpaintingMethod);
Mat inpaintedArea = result(inpaintArea);
SANITY_CHECK(inpaintedArea);
}
PERF_TEST_P(Perf_InpaintingMethod, inpaintDots, InpaintingMethod::all())
{
Mat src = imread(getDataPath("gpu/hog/road.png"));
int inpaintingMethod = GetParam();
Mat mask(src.size(), CV_8UC1, Scalar(0));
Mat result(src.size(), src.type());
for (int i = 0; i < src.size().height; i += 16) {
for (int j = 0; j < src.size().width; j += 16) {
mask.at<unsigned char>(i, j) = 255;
}
}
declare.in(src, mask).out(result).time(120);
TEST_CYCLE() inpaint(src, mask, result, 10.0, inpaintingMethod);
SANITY_CHECK_NOTHING();
}
} // namespace