0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00

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
This commit is contained in:
happy-capybara-man
2025-11-10 15:47:40 +08:00
committed by GitHub
parent 64bbaa9f41
commit db207c88b0
5 changed files with 8 additions and 6 deletions

View File

@@ -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];

View File

@@ -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);

View File

@@ -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();

View File

@@ -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
------------------------------

View File

@@ -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);