-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix infinite loop on ArUco apriltag refinement #3220
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
Merged
opencv-pushbot
merged 4 commits into
opencv:3.4
from
buq2:aruco-apriltag-infinite-loop-fix
Apr 14, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -717,4 +717,91 @@ TEST(CV_ArucoDetectMarkers, regression_2492) | |
} | ||
} | ||
|
||
struct ArucoThreading: public testing::TestWithParam<cv::aruco::CornerRefineMethod> | ||
{ | ||
struct NumThreadsSetter { | ||
NumThreadsSetter(const int num_threads) | ||
: original_num_threads_(cv::getNumThreads()) { | ||
cv::setNumThreads(num_threads); | ||
} | ||
|
||
~NumThreadsSetter() { | ||
cv::setNumThreads(original_num_threads_); | ||
} | ||
private: | ||
int original_num_threads_; | ||
}; | ||
}; | ||
|
||
TEST_P(ArucoThreading, number_of_threads_does_not_change_results) | ||
{ | ||
cv::Ptr<cv::aruco::DetectorParameters> params = cv::aruco::DetectorParameters::create(); | ||
// We are not testing against different dictionaries | ||
// As we are interested mostly in small images, smaller | ||
// markers is better -> 4x4 | ||
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); | ||
|
||
// Height of the test image can be chosen quite freely | ||
// We aim to test against small images as in those the | ||
// number of threads has most effect | ||
const int height_img = 20; | ||
// Just to get nice white boarder | ||
const int shift = height_img > 10 ? 5 : 1; | ||
const int height_marker = height_img-2*shift; | ||
|
||
// Create a test image | ||
cv::Mat img_marker; | ||
cv::aruco::drawMarker(dictionary, 23, height_marker, img_marker, 1); | ||
|
||
// Copy to bigger image to get a white border | ||
cv::Mat img(height_img, height_img, CV_8UC1, cv::Scalar(255)); | ||
img_marker.copyTo(img(cv::Rect(shift, shift, height_marker, height_marker))); | ||
|
||
params->cornerRefinementMethod = GetParam(); | ||
|
||
std::vector<std::vector<cv::Point2f> > original_corners; | ||
std::vector<int> original_ids; | ||
{ | ||
NumThreadsSetter thread_num_setter(1); | ||
cv::aruco::detectMarkers(img, dictionary, original_corners, original_ids, params); | ||
} | ||
|
||
ASSERT_EQ(original_ids.size(), 1); | ||
ASSERT_EQ(original_corners.size(), 1); | ||
|
||
int num_threads_to_test[] = { 2, 8, 16, 32, height_img-1, height_img, height_img+1}; | ||
|
||
for (size_t i_num_threads = 0; i_num_threads < sizeof(num_threads_to_test)/sizeof(int); ++i_num_threads) { | ||
NumThreadsSetter thread_num_setter(num_threads_to_test[i_num_threads]); | ||
|
||
std::vector<std::vector<cv::Point2f> > corners; | ||
std::vector<int> ids; | ||
cv::aruco::detectMarkers(img, dictionary, corners, ids, params); | ||
|
||
// If we don't find any markers, the test is broken | ||
ASSERT_EQ(ids.size(), 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: comparison between signed and unsigned integer expressions sorry, I overlooked |
||
|
||
// Make sure we got the same result as the first time | ||
ASSERT_EQ(corners.size(), original_corners.size()); | ||
ASSERT_EQ(ids.size(), original_ids.size()); | ||
ASSERT_EQ(ids.size(), corners.size()); | ||
for (size_t i = 0; i < corners.size(); ++i) { | ||
EXPECT_EQ(ids[i], original_ids[i]); | ||
for (size_t j = 0; j < corners[i].size(); ++j) { | ||
EXPECT_NEAR(corners[i][j].x, original_corners[i][j].x, 0.1f); | ||
EXPECT_NEAR(corners[i][j].y, original_corners[i][j].y, 0.1f); | ||
} | ||
} | ||
} | ||
} | ||
|
||
INSTANTIATE_TEST_CASE_P( | ||
CV_ArucoDetectMarkers, ArucoThreading, | ||
::testing::Values( | ||
cv::aruco::CORNER_REFINE_NONE, | ||
cv::aruco::CORNER_REFINE_SUBPIX, | ||
cv::aruco::CORNER_REFINE_CONTOUR, | ||
cv::aruco::CORNER_REFINE_APRILTAG | ||
)); | ||
|
||
}} // namespace |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: comparison between signed and unsigned integer expressions
sorry, I overlooked