Skip to content
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

replaced recursion DFS to stack DFS #3207

Merged
merged 2 commits into from
Apr 1, 2022
Merged

replaced recursion DFS to stack DFS #3207

merged 2 commits into from
Apr 1, 2022

Conversation

aimbot6120
Copy link
Contributor

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

[ Y] I agree to contribute to the project under Apache 2 License.
[ Y] 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
[ Y] The PR is proposed to the proper branch
[ N] There is a reference to the original bug report and related work
[ N] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
[ N] The feature is well documented and sample code can be built with the project CMake

The function DFSUtil in opencv/opencv_contrib/modules/text/sample/text_detection_swt.cpp implements Depth First Search (DFS) using recursion. This implementation uses the call stack of the program for recursion which makes it prone to stack overflow due to very deep recursion. This error was encountered for some images from my testset. The error was found through Valgrind.

Fix:
Implemented the DFSUtil function using external stack. This implementation was tested using the same images and the program is now working as intended.

@@ -92,14 +93,25 @@ static
void DFSUtil(int v, std::vector<bool> & visited, std::vector< std::vector<int> >& adj, int label, std::vector<int> &component_id)
{
// Mark the current node as visited and label it as belonging to the current component
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is related to removed/moved code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the comments

replaced recursion DFS to stack DFS
Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution 👍

@alalek alalek merged commit da967e0 into opencv:4.x Apr 1, 2022
@aimbot6120 aimbot6120 deleted the swtfix branch April 1, 2022 23:23
@opencv-pushbot opencv-pushbot mentioned this pull request Apr 23, 2022
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.

2 participants