Repository: opencv
Issue: opencv.js memory access out of bounds
Labels: incomplete,category: javascript (js),needs investigation

##### System information (version)

- OpenCV.js-webassembly == 4.5.1
- Operating System / Platform => Windows 64 Bit or MacOS 10.14.6
- Chrome => 87.0.4280.141

##### Detailed description

i use grabcut in js by opencv.js-webassembly == 4.5.1,when the canvas and mask bounds is big,it errors.

i use the bellow code and the canvas and mask bounds is 1000x3500（part of pic ）,it error

when it error,the opencv.js will collapse until fresh the page

[beer3.jpg.zip](https://github.com/opencv/opencv/files/5870829/beer3.jpg.zip)

error log:
```
getAiAssistDraw error RuntimeError: memory access out of bounds
    at malloc (<anonymous>:wasm-function[11613]:0x4ac738)
    at <anonymous>:wasm-function[11171]:0x49e768
    at <anonymous>:wasm-function[3554]:0x15dd85
    at <anonymous>:wasm-function[3548]:0x15cdc8
    at <anonymous>:wasm-function[545]:0x195b2
    at <anonymous>:wasm-function[1040]:0x2b9ca
    at dynCall_viiiiiiii (<anonymous>:wasm-function[11714]:0x4afe0a)
    at Module.dynCall_viiiiiiii (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js:17:7642371)
    at dynCall_viiiiiiii_781 (eval at makeDynCaller (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js), <anonymous>:4:12)
    at Object.grabCut (eval at new_ (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js), <anonymous>:14:1)
    at Object.proto.<computed> [as grabCut] (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js:17:7575575)
    at Object.predict (webpack-internal:///./src/views/tool/editor/ai/pic/grabcut.js:130:71)
    at eval (webpack-internal:///./src/views/tool/editor/ai/algorithm.js:362:29)
    at eval (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:338:22)
    at Engine.scopedRun (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:348:25)
    at Engine.tidy (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:337:21)
CanvasManager.js?4ec2:3439 拉框失败 RuntimeError: memory access out of bounds
    at malloc (<anonymous>:wasm-function[11613]:0x4ac738)
    at <anonymous>:wasm-function[11171]:0x49e768
    at <anonymous>:wasm-function[3554]:0x15dd85
    at <anonymous>:wasm-function[3548]:0x15cdc8
    at <anonymous>:wasm-function[545]:0x195b2
    at <anonymous>:wasm-function[1040]:0x2b9ca
    at dynCall_viiiiiiii (<anonymous>:wasm-function[11714]:0x4afe0a)
    at Module.dynCall_viiiiiiii (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js:17:7642371)
    at dynCall_viiiiiiii_781 (eval at makeDynCaller (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js), <anonymous>:4:12)
    at Object.grabCut (eval at new_ (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js), <anonymous>:14:1)
    at Object.proto.<computed> [as grabCut] (webpack-internal:///./node_modules/opencv.js-webassembly/opencv.js:17:7575575)
    at Object.predict (webpack-internal:///./src/views/tool/editor/ai/pic/grabcut.js:130:71)
    at eval (webpack-internal:///./src/views/tool/editor/ai/algorithm.js:362:29)
    at eval (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:338:22)
    at Engine.scopedRun (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:348:25)
    at Engine.tidy (webpack-internal:///./node_modules/@tensorflow/tfjs-core/dist/engine.js:337:21)
```


##### Steps to reproduce

```javascript
 let {canvas, pointList} = data
        let src = cv.imread("part-canvas");
        cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
        let roiRect = new cv.Rect(0, 0, canvas.width - 1, canvas.height - 1);
        let mask = cv.Mat.zeros(canvas.height, canvas.width, cv.CV_8UC1);
        let bgdModel = new cv.Mat();
        let fgdModel = new cv.Mat();
        let grayMask =undefined
        try {
            if (resultType === LABEL_TYPE_CONST.BOX) {
                cv.grabCut(src, mask, roiRect, bgdModel, fgdModel, 5, cv.GC_INIT_WITH_RECT);
            } else if (resultType === LABEL_TYPE_CONST.POLYGON) {
                $this.initMask(pointList, mask,new cv.Scalar(cv.GC_PR_FGD));
                cv.grabCut(src, mask, roiRect, bgdModel, fgdModel, 3, cv.GC_INIT_WITH_MASK);
            }
           
        }
        finally {
            src.delete()
            mask.delete()
            bgdModel.delete()
            fgdModel.delete()
            if(grayMask){
                grayMask.delete()
            }

        }
```