Skip to content

Speed up and reduce memory consumption for findContours#26690

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:speed-up-and-reduce-memory-consumption-for-findContours
Dec 31, 2024
Merged

Speed up and reduce memory consumption for findContours#26690
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:speed-up-and-reduce-memory-consumption-for-findContours

Conversation

@MaximSmolskiy
Copy link
Copy Markdown
Contributor

Pull Request Readiness Checklist

Related to #26683

I ran reproducer from issue with one iteration.

Before

iteration 0 took 3.1967720985412598 seconds. Total process memory: 5,331,820,544

After

iteration 0 took 1.3795826435089111 seconds. Total process memory: 500,125,696

Speedup more than 2 times and memory consumption reduction more than 10 times

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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

@asmorkalov asmorkalov requested a review from mshabunin December 31, 2024 06:01
@asmorkalov asmorkalov added this to the 4.11.0 milestone Dec 31, 2024
@asmorkalov asmorkalov self-requested a review December 31, 2024 07:19
@asmorkalov asmorkalov self-assigned this Dec 31, 2024
@asmorkalov asmorkalov merged commit 4d26e16 into opencv:4.x Dec 31, 2024
@MaximSmolskiy MaximSmolskiy deleted the speed-up-and-reduce-memory-consumption-for-findContours branch December 31, 2024 10:00
@solidR22
Copy link
Copy Markdown

solidR22 commented Jan 15, 2025

Thank you for your work, but I tested and found that the speed of this function in version 4.11 is slower than in version 4.7.
Here are the test images and test results.

#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat img = imread("111.bmp", cv::IMREAD_GRAYSCALE);

	cv::Mat mask;
	inRange(img, 110, 120, mask);

	std::vector<std::vector<cv::Point>> v_cont;
	for (int i = 0; i < 10; i++)
	{
		v_cont.clear();
		auto start = chrono::system_clock::now();
		cv::findContours(mask, v_cont, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
		auto end = chrono::system_clock::now();
		auto duration = chrono::duration_cast<chrono::microseconds>(end - start) / 1000.;
		cout << "4.11 findContours cost" << double(duration.count()) << "ms" << endl;
	}
}

1234

image
image

@asmorkalov asmorkalov mentioned this pull request Jan 15, 2025
@mshabunin
Copy link
Copy Markdown
Contributor

the speed of this function in version 4.11 is slower than in version 4.7

This is because this function has been rewritten from C to C++ between these releases, it needs additional refactoring of internal structures in order to return to previous speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants