Skip to content

Common fixed size Python sequence conversion to OpenCV types#19344

Merged
opencv-pushbot merged 1 commit intoopencv:3.4from
VadimLevin:dev/vlevin/generic-sequence-conversion
Jan 25, 2021
Merged

Common fixed size Python sequence conversion to OpenCV types#19344
opencv-pushbot merged 1 commit intoopencv:3.4from
VadimLevin:dev/vlevin/generic-sequence-conversion

Conversation

@VadimLevin
Copy link
Copy Markdown
Contributor

@VadimLevin VadimLevin commented Jan 18, 2021

This PR introduces common way on how fixed size sequence can be converted to OpenCV type e.g. tuple/list/np.array to cv::Rect.
Each field of the OpenCV structs are packed to lightweight reference wrappers array, so only homogeneous sequence can be parsed, so TermCriteria is parsed separately.

This patch disables the conversion from Python complex to cv::Point, because in my opinion it is error prone and doesn't take into account conversion from floating point values to integers as others do.

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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
force_builders=ARMv7,Custom
build_image:Custom=centos:7
buildworker:Custom=linux-1

Comment on lines -1253 to -1258
if(PyComplex_Check(obj))
{
p.x = PyComplex_RealAsDouble(obj);
p.y = PyComplex_ImagAsDouble(obj);
return true;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vpisarev Any objections for removal of this change?

It is here from 2010.

Copy link
Copy Markdown
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.

Well done! Thank you 👍

Rect = namedtuple('Rect', ('x', 'y', 'w', 'h'))
try_to_convert = partial(self._try_to_convert, cv.utils.dumpRect)
for convertible in ((1, 2, 4, 5), [5, 3, 10, 20], np.array([10, 20, 23, 10]),
Rect(10, 30, 40, 55)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add test cases which are used here:

np.array([10, 20, 23, 10]),
tuple(np.array([10, 20, 24, 10])),
list(np.array([10, 20, 25, 10])),

@VadimLevin VadimLevin force-pushed the dev/vlevin/generic-sequence-conversion branch from cb6ad05 to 1d3207d Compare January 25, 2021 05:10
@opencv-pushbot opencv-pushbot merged commit 62b60b1 into opencv:3.4 Jan 25, 2021
@alalek alalek mentioned this pull request Jan 25, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
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.

3 participants