mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
61bd5e7b55100bfc03d9891a17363adb91fb8747
Fix js ximgproc edge drawing #29387 ## Summary This PR fixes OpenCV.js binding generation for factory functions that return `cv::Ptr<T>` where `T` is a namespaced class exported with a module-prefixed JavaScript binding name. The failure is reproduced with OpenCV.js plus `opencv_contrib/modules/ximgproc`. The generated binding for `cv::ximgproc::EdgeDrawing` currently contains: ```cpp .constructor(select_overload<Ptr<EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) ``` but `EdgeDrawing` is not available in the generated C++ scope as an unqualified type. The generated constructor should use: ```cpp .constructor(select_overload<Ptr<cv::ximgproc::EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) ``` ## Related issues Fixes opencv/opencv_contrib#4161. Related to #27963, #28130, and #28143. #28143 added namespace qualification for factory `Ptr<...>` return types when the inner `Ptr` type matches the generator class key. The remaining `ximgproc::EdgeDrawing` case is different: ```text inner Ptr type: EdgeDrawing generator class key: ximgproc_EdgeDrawing C++ class name: cv::ximgproc::EdgeDrawing ``` Because `EdgeDrawing` does not match `ximgproc_EdgeDrawing`, the previous condition does not handle this case. ## Fix approach The JS generator now centralizes factory `Ptr<...>` return-type qualification in a helper used by both generator paths: * `gen_function_binding_with_wrapper` * `gen_function_binding` The helper keeps the existing behavior for already qualified types and for the existing class-key match. It additionally handles the case where the inner `Ptr` type matches the basename of the C++ class name: ```text cv::ximgproc::EdgeDrawing -> EdgeDrawing ``` This allows the generator to emit `Ptr<cv::ximgproc::EdgeDrawing>` for `createEdgeDrawing()` without changing generated files directly. ## Necessity as an opencv code change The failing API is exposed by `opencv_contrib/modules/ximgproc`, but the invalid C++ line is emitted by OpenCV core's JavaScript binding generator in `modules/js/generator/embindgen.py`. A contrib-only workaround would have to change the `ximgproc` public declaration or special-case the JS export list for `createEdgeDrawing`. That would only work around one symbol and would not fix the generator's handling of namespaced factory `Ptr` return types. The generator already has the class metadata needed to produce the correct fully qualified C++ type, so the fix belongs in OpenCV core. ## Verification Tested with: * OpenCV core `4.x` * opencv_contrib `4.x` * Emscripten `6.0.1` * CMake generator: Ninja * Build list: `core,imgproc,imgcodecs,video,calib3d,ximgproc,js` Generator target: ```bash emcmake cmake \ -S /path/to/opencv \ -B /path/to/build \ -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_CXX_STANDARD=17 \ -DOPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib/modules \ -DBUILD_LIST=core,imgproc,imgcodecs,video,calib3d,ximgproc,js \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_opencv_js=ON \ -DBUILD_TESTS=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_DOCS=OFF ninja -C /path/to/build gen_opencv_js_source ``` This generated the expected binding: ```cpp .constructor(select_overload<Ptr<cv::ximgproc::EdgeDrawing>()>(&cv::ximgproc::createEdgeDrawing)) .smart_ptr<Ptr<cv::ximgproc::EdgeDrawing>>("Ptr<ximgproc_EdgeDrawing>") ``` The same patch was also verified with the full OpenCV.js target: ```bash ninja -C /path/to/build opencv.js ```
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/4.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
Contributing
Please read the contribution guidelines before starting work on a pull request.
Summary of the guidelines:
- One pull request per issue;
- Choose the right base branch;
- Include tests and documentation;
- Clean up "oops" commits before submitting;
- Follow the coding style guide.
Additional Resources
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on Twitter
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.6%
C
3.2%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%