Skip to content

Named arguments handling#19156

Merged
opencv-pushbot merged 1 commit intoopencv:nextfrom
vpisarev:named_args1
Dec 18, 2020
Merged

Named arguments handling#19156
opencv-pushbot merged 1 commit intoopencv:nextfrom
vpisarev:named_args1

Conversation

@vpisarev
Copy link
Copy Markdown
Contributor

@vpisarev vpisarev commented Dec 18, 2020

This is initial implementation of OE34 (https://github.com/opencv/opencv/wiki/OE-34.-Named-Parameters; discussion: #17997).

It's incomplete in a sense that while it's planned to refactor some of OpenCV 5 functionality to follow this style, this patch just adds a single C++ function with the new-style API. But it's a proof-of-concept and it also provides a method for Python, Java, JS etc. generators to handle such API.

For Python there is a special modification that maps named parameters in Python directly to named parameters in C++ API, e.g.

kernel = np.array([[1,1,1], [1,1,1], [1,1,1]], dtype='float32')
dst = filter2Dp(src, kernel, scale=1./9)

will map to

Mat kernel=Mat::ones(3,3,CV_32F), dst;
Filter2DParams params;
params.scale = 1./9;
filter2D(src, kernel, dst, params);

and if you use C++ 20, you can replace the code above with

Mat kernel=Mat::ones(3,3,CV_32F), dst;
filter2D(src, kernel, dst, {.scale=1./9});

Pull Request Readiness Checklist

  • 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

Comment on lines +1563 to +1568
if (ddepth < 0)
ddepth = src.depth();
if (params.scale != 1) {
int kdepth = K.depth();
K.convertTo(tempK,
kdepth == CV_32F || kdepth == CV_64F ? kdepth : CV_32F,
params.scale, 0);
K = tempK;
}
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.

Why do we need such logic here? Is it already handled by filter2D()?
We should avoid duplication of such code (especially in incomplete form with missing checks).

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.

  • ddepth check was removed, thanks!
  • scale check is retained, because it's extension to the existing API

Comment on lines +1511 to +1524
class CV_EXPORTS_W_PARAMS Filter2DParams
{
public:
CV_PROP_RW int anchorX = -1;
CV_PROP_RW int anchorY = -1;
CV_PROP_RW int borderType = BORDER_DEFAULT;
CV_PROP_RW Scalar borderValue = Scalar();
CV_PROP_RW int ddepth = -1;
CV_PROP_RW double scale = 1.;
CV_PROP_RW double shift = 0.;
};

CV_EXPORTS_AS(filter2Dp) void filter2D( InputArray src, OutputArray dst, InputArray kernel,
const Filter2DParams& params=Filter2DParams());
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.

As previously noted this is just a syntax sugar.

  • These entries should be generated by automatic script (like binding generator, but for these named parameters)
  • This stuff should not be EXPORTed in terms of C++ (its ABI is unstable)
  • Implementation should be straightforward and "inline". No extra logic inside!
  • Dedicated header should be used (to be generated by script)

Copy link
Copy Markdown
Contributor Author

@vpisarev vpisarev Dec 18, 2020

Choose a reason for hiding this comment

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

  • previously noted where and by whom?
  • we don't care about ABI starting from OpenCV 4.0.
  • this will be the main API for complex functions in OpenCV 5 (optical flow, camera calibration, video writer initialization, etc.). The previous API will become backward-compatibility wrappers. The current solution for filter2D when the new API is implemented via the old API is temporary.
  • in this particular case the new API is extension of the previous API. No automatic script can handle that

…ed parameters

* added sample filter2D[p]() function and a little python test for it to demonstrate the concept
@vpisarev
Copy link
Copy Markdown
Contributor Author

👍

@opencv-pushbot opencv-pushbot merged commit 71be47e into opencv:next Dec 18, 2020
@vpisarev vpisarev removed the pr: needs test New functionality requires minimal tests set label Dec 18, 2020
@alalek alalek mentioned this pull request Jan 23, 2023
6 tasks
asmorkalov added a commit to asmorkalov/opencv that referenced this pull request May 29, 2023
asmorkalov added a commit that referenced this pull request May 30, 2023
Re-implement named parameters bindings for Python #23705

Reverted named argument handling from #19156.
Ported new solution from #23224
The port is required to harmonize 4.x -> 5.x merges.

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants