Skip to content

fix type cast in drawDetectedMarkers, drawDetectedCornersCharuco, drawDetectedDiamonds#24247

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
AleksandrPanov:fix_drawDetectedCornersCharuco_type_error
Sep 12, 2023
Merged

fix type cast in drawDetectedMarkers, drawDetectedCornersCharuco, drawDetectedDiamonds#24247
asmorkalov merged 1 commit intoopencv:4.xfrom
AleksandrPanov:fix_drawDetectedCornersCharuco_type_error

Conversation

@AleksandrPanov
Copy link
Copy Markdown
Contributor

@AleksandrPanov AleksandrPanov commented Sep 8, 2023

  • drawDetectedMarkers(), drawDetectedCornersCharuco(), drawDetectedDiamonds() uses only Point2f (CV_32FC2) for detected corners. But this corners are casted to int later in the code.
  • drawDetectedMarkers(), drawDetectedDiamonds() has a requirement on CV_32FC2 for corners. This requirement is lost in drawDetectedCornersCharuco().

Strict input data requirements have been removed. Added only the requirement to have 2 channels. The input corners are then casted to CV_32SC2.

added tests

Pull Request Readiness Checklist

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

(_image.getMat().channels() == 1 || _image.getMat().channels() == 3));
CV_Assert((_charucoCorners.getMat().total() == _charucoIds.getMat().total()) ||
_charucoIds.getMat().total() == 0);
CV_Assert(_charucoCorners.getMat().type() == CV_32FC2);
Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov Sep 8, 2023

Choose a reason for hiding this comment

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

I propose alternative:

CV_Assert(_charucoCorners.channels() == 2);
...
_charucoCorners.getMat().convertTo(..., ...);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need to limit type here. Just use proper conversions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

then I will remove the limits from all functions (drawDetectedMarkers, drawDetectedCornersCharuco, drawDetectedDiamonds)

_charucoIds.getMat().total() == 0);
CV_Assert(_charucoCorners.getMat().type() == CV_32FC2);

size_t nCorners = _charucoCorners.getMat().total();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

InputArray has total() method. getMat() is redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed getMat()

@asmorkalov asmorkalov added this to the 4.9.0 milestone Sep 8, 2023
@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch from d65f3d3 to 093878a Compare September 8, 2023 14:12
@AleksandrPanov AleksandrPanov changed the title fix drawDetectedCornersCharuco type error fix type cast in drawDetectedMarkers, drawDetectedCornersCharuco, drawDetectedDiamonds Sep 8, 2023
@AleksandrPanov AleksandrPanov marked this pull request as draft September 8, 2023 15:53
@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch 3 times, most recently from 4dde75d to b7b8183 Compare September 8, 2023 17:47
@AleksandrPanov AleksandrPanov marked this pull request as ready for review September 8, 2023 17:58
@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch 4 times, most recently from 4bfb003 to 587dc3f Compare September 9, 2023 09:38
Mat currentMarker = _corners.getMat(i);
CV_Assert(currentMarker.total() == 4 && currentMarker.type() == CV_32FC2);
CV_Assert(currentMarker.total() == 4 && currentMarker.channels() == 2);
currentMarker.convertTo(currentMarker, CV_32SC2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It makes sense, to check if the input is already 32SC2. convertTo does not check it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added check:

        if (currentMarker.type() != CV_32SC2)
            currentMarker.convertTo(currentMarker, CV_32SC2);

Mat currentMarker = _corners.getMat(i);
CV_Assert(currentMarker.total() == 4 && currentMarker.type() == CV_32FC2);
CV_Assert(currentMarker.total() == 4 && currentMarker.channels() == 2);
currentMarker.convertTo(currentMarker, CV_32SC2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same for type check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added check:

        if (currentMarker.type() != CV_32SC2)
            currentMarker.convertTo(currentMarker, CV_32SC2);

Comment on lines +694 to +695
vector<vector<Point2d>> detected_double = {{Point2d(0., 0.), Point2d(10., 0.), Point2d(10., 10.), Point2d(0., 10.)}};
vector<vector<Point2d>> detected_int = {{Point(0, 0), Point(10, 0), Point2d(10, 10), Point2d(0, 10)}};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same test type Point2d and Point2d. Should it be 2i in the second case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed:
vector<vector<Point2i>> detected_int = {{Point(0, 0), Point(10, 0), Point(10, 10), Point(0, 10)}};

@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch from 587dc3f to fdec321 Compare September 11, 2023 07:56
@AleksandrPanov AleksandrPanov marked this pull request as draft September 11, 2023 13:01
@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch from fdec321 to 8f24080 Compare September 11, 2023 16:43
for (size_t i = 0ull; i < 4ull; i++) {
m = moments(contours[i]);
detectedCenter = Point(cvRound(m.m10/m.m00), cvRound(m.m01/m.m00));
ASSERT_TRUE(find(detected_int[0].begin(), detected_int[0].end(), detectedCenter) != detected_int[0].end());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

drawDetectedCornersCharuco is called for _float, but checked for _int. Also _double version is initialized, but not used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

detected_int and detected_float are equal, using int will help to avoid rounding errors
the "_double" version will be added

@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch from 8f24080 to 100b163 Compare September 11, 2023 21:52
@AleksandrPanov AleksandrPanov marked this pull request as ready for review September 11, 2023 21:53
@AleksandrPanov AleksandrPanov force-pushed the fix_drawDetectedCornersCharuco_type_error branch from 100b163 to ae1d1b6 Compare September 12, 2023 09:18
Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

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

👍

@asmorkalov asmorkalov merged commit 9761492 into opencv:4.x Sep 12, 2023
@asmorkalov asmorkalov self-assigned this Sep 12, 2023
@asmorkalov asmorkalov mentioned this pull request Sep 28, 2023
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