From db207c88b0d3949354f0125cc718962e69b7a637 Mon Sep 17 00:00:00 2001 From: happy-capybara-man Date: Mon, 10 Nov 2025 15:47:40 +0800 Subject: [PATCH] Merge pull request #27985 from happy-capybara-man:docs/fix-mat-clone-js docs(js): Fix Mat.clone() documentation to use mat_clone() for deep copy #27985 - Update code example to use ```mat_clone()``` instead of ```clone()``` - Add explanatory note about shallow copy issue due to Emscripten embind Problem - OpenCV.js documentation shows ```Mat.clone()``` usage, but this method performs shallow copy instead of deep copy due to Emscripten embind limitations, causing unexpected behavior where modifications to cloned matrices affect the original. Related Issues and PRs - Fixes documentation aspect of issue #27572 - Related to PR #26643 (js_clone_fix) ### 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 - [ ] 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 --- .../js_assets/js_image_arithmetics_bitwise.html | 2 +- doc/js_tutorials/js_assets/js_imgproc_camera.html | 2 +- doc/js_tutorials/js_assets/js_intelligent_scissors.html | 2 +- doc/js_tutorials/js_core/js_basic_ops/js_basic_ops.markdown | 6 ++++-- samples/dnn/js_face_recognition.html | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/doc/js_tutorials/js_assets/js_image_arithmetics_bitwise.html b/doc/js_tutorials/js_assets/js_image_arithmetics_bitwise.html index 05c0ffd730..ccf0bbbda7 100644 --- a/doc/js_tutorials/js_assets/js_image_arithmetics_bitwise.html +++ b/doc/js_tutorials/js_assets/js_image_arithmetics_bitwise.html @@ -82,7 +82,7 @@ cv.bitwise_and(logo, logo, imgFg, mask); // Put logo in ROI and modify the main image cv.add(imgBg, imgFg, sum); -dst = src.clone(); +dst = src.mat_clone(); for (let i = 0; i < logo.rows; i++) { for (let j = 0; j < logo.cols; j++) { dst.ucharPtr(i, j)[0] = sum.ucharPtr(i, j)[0]; diff --git a/doc/js_tutorials/js_assets/js_imgproc_camera.html b/doc/js_tutorials/js_assets/js_imgproc_camera.html index 2df68d7f33..273b11ff57 100644 --- a/doc/js_tutorials/js_assets/js_imgproc_camera.html +++ b/doc/js_tutorials/js_assets/js_imgproc_camera.html @@ -248,7 +248,7 @@ function backprojection(src) { if (base instanceof cv.Mat) { base.delete(); } - base = src.clone(); + base = src.mat_clone(); cv.cvtColor(base, base, cv.COLOR_RGB2HSV, 0); } cv.cvtColor(src, dstC3, cv.COLOR_RGB2HSV, 0); diff --git a/doc/js_tutorials/js_assets/js_intelligent_scissors.html b/doc/js_tutorials/js_assets/js_intelligent_scissors.html index 1782dc6f03..f48dd07e4b 100644 --- a/doc/js_tutorials/js_assets/js_intelligent_scissors.html +++ b/doc/js_tutorials/js_assets/js_intelligent_scissors.html @@ -53,7 +53,7 @@ canvas.addEventListener('click', e => { }); canvas.addEventListener('mousemove', e => { let x = e.offsetX, y = e.offsetY; //console.log(x, y); - let dst = src.clone(); + let dst = src.mat_clone(); if (hasMap && x >= 0 && x < src.cols && y >= 0 && y < src.rows) { let contour = new cv.Mat(); diff --git a/doc/js_tutorials/js_core/js_basic_ops/js_basic_ops.markdown b/doc/js_tutorials/js_core/js_basic_ops/js_basic_ops.markdown index ee110c37cb..74c3b9cc86 100644 --- a/doc/js_tutorials/js_core/js_basic_ops/js_basic_ops.markdown +++ b/doc/js_tutorials/js_core/js_basic_ops/js_basic_ops.markdown @@ -77,12 +77,14 @@ How to copy Mat There are 2 ways to copy a Mat: @code{.js} -// 1. Clone -let dst = src.clone(); +// 1. Clone (deep copy) +let dst = src.mat_clone(); // 2. CopyTo(only entries indicated in the mask are copied) src.copyTo(dst, mask); @endcode +@note In OpenCV.js, use `mat_clone()` instead of `clone()` to ensure deep copy behavior. The `clone()` method may perform shallow copy due to Emscripten embind limitations. + How to convert the type of Mat ------------------------------ diff --git a/samples/dnn/js_face_recognition.html b/samples/dnn/js_face_recognition.html index 3b4ed390d7..aaee89327f 100644 --- a/samples/dnn/js_face_recognition.html +++ b/samples/dnn/js_face_recognition.html @@ -132,7 +132,7 @@ function main() { var cell = document.getElementById("targetNames").insertCell(0); cell.innerHTML = name; - persons[name] = face2vec(face).clone(); + persons[name] = face2vec(face).mat_clone(); var canvas = document.createElement("canvas"); canvas.setAttribute("width", 112);