使用opencv js抓取

时间:2019-02-18 03:24:38

标签: javascript opencv

我正在使用Javascript和opencv进行后台提取项目。

这是我想要的例子:

https://www.youtube.com/watch?v=kAwxLTDDAwU&t=143s

但是它是用python编写的,我在带有Javascript的opencv官方网站中找不到标记代码。这是python代码。

https://github.com/opencv/opencv/blob/master/samples/python/grabcut.py

这是我在js中有关第一个基本矩形函数的代码:

<script type="text/javascript">
    $(function(){ 
        let imgElement = document.getElementById('canvasInput');
        let inputElement = document.getElementById('fileInput');
        inputElement.addEventListener('change', (e) => {
          imgElement.src = URL.createObjectURL(e.target.files[0]);
        }, false);
    }); 

    function selectRect(x,y,width,height){
        let src = cv.imread('canvasInput');
        cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
        let mask = new cv.Mat();
        let bgdModel = new cv.Mat();
        let fgdModel = new cv.Mat();
        let rect = new cv.Rect(x, y, width, height);
        cv.grabCut(src, mask, rect, bgdModel, fgdModel, 1, cv.GC_INIT_WITH_RECT);
        // draw foreground
        for (let i = 0; i < src.rows; i++) {
            for (let j = 0; j < src.cols; j++) {
                if (mask.ucharPtr(i, j)[0] == 0 || mask.ucharPtr(i, j)[0] == 2) {
                    src.ucharPtr(i, j)[0] = 0;
                    src.ucharPtr(i, j)[1] = 0;
                    src.ucharPtr(i, j)[2] = 0;
                }
            }
        }
        // draw grab rect
        let color = new cv.Scalar(0, 0, 255);
        let point1 = new cv.Point(rect.x, rect.y);
        let point2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
        cv.rectangle(src, point1, point2, color);
        cv.imshow('canvasOutput', src);
        src.delete(); mask.delete(); bgdModel.delete(); fgdModel.delete();
    }

    function remask(){
        let src = cv.imread('canvasOutput');...
    }
</script>

我的问题是,如何完成重新遮罩功能,您可以在图片上绘制更多背景信息,从而为您提供更准确的结果。

0 个答案:

没有答案