js: restore deep copy behavior for Mat.clone()

Emscripten 3.1.71+ introduced ClassHandle.clone() which performs a shallow copy.
This shadowed the original OpenCV clone() method which was intended for deep copying.

This patch:
1. Hooks into onRuntimeInitialized in helpers.js
2. Overrides cv.Mat.prototype.clone to point to mat_clone (deep copy)
3. Updates JS tests to use clone() to verify the fix
This commit is contained in:
happy-capybara-man
2025-12-18 01:39:27 +08:00
parent e4d294d018
commit 8ea50a77fd
2 changed files with 18 additions and 1 deletions

View File

@@ -410,3 +410,20 @@ if (
if (cv.UMat) cv.UMat.prototype[Symbol.dispose] = cv.UMat.prototype.delete;
// Add more as OpenCV gains new manual-cleanup classes
}
// Override Emscripten's shallow clone() with OpenCV's deep copy mat_clone()
// This restores the expected behavior where clone() performs a deep copy.
// Background: Emscripten 3.1.71+ added ClassHandle.clone() which only does shallow copy.
// See: https://github.com/opencv/opencv/pull/26643
// See: https://github.com/opencv/opencv/issues/27572
var _opencv_onRuntimeInitialized_backup = Module['onRuntimeInitialized'];
Module['onRuntimeInitialized'] = function() {
if (_opencv_onRuntimeInitialized_backup) {
_opencv_onRuntimeInitialized_backup();
}
if (typeof cv !== 'undefined' && cv.Mat &&
typeof cv.Mat.prototype.mat_clone === 'function') {
cv.Mat.prototype.clone = cv.Mat.prototype.mat_clone;
}
};

View File

@@ -173,7 +173,7 @@ QUnit.test('test_mat_creation', function(assert) {
// clone
{
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
let mat2 = mat.mat_clone();
let mat2 = mat.clone();
assert.equal(mat.channels, mat2.channels);
assert.equal(mat.size().height, mat2.size().height);