围绕对象opencv.js的边界矩形在控制台中引发错误

时间:2019-03-26 15:25:22

标签: javascript opencv

我正在使用opencv.js库从图像中找到对象。无法在资助对象周围绘制矩形

我已经尝试了以下代码块

        var canvas = document.getElementById('canvasInput');
        canvas.renderImage(e.target.files[0]);
        let src = cv.imread("canvasInput");
        let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
        cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
        cv.threshold(src, src, 177, 200, cv.THRESH_BINARY);
        let contours = new cv.MatVector();
        let hierarchy = new cv.Mat();
        cv.findContours(src, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE);
        let cnt = contours.get(0);
        let rect = cv.boundingRect(cnt);
        let contoursColor = new cv.Scalar(255, 255, 255);
        let rectangleColor = new cv.Scalar(255, 0, 0);
        cv.drawContours(dst, contours, 0, contoursColor, 1, 8, hierarchy, 100);
        let point1 = new cv.Point(rect.x, rect.y);
        let point2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
        cv.rectangle(dst, point1, point2, rectangleColor, 2, cv.LINE_AA, 0);
        cv.imshow('canvasOutput', dst);
        src.delete(); dst.delete(); contours.delete(); hierarchy.delete(); 
         cnt.delete();

但找到了

opcv.js:21未捕获的TypeError:无法读取未定义的属性'$$'     在RegisteredPointer.nonConstNoSmartPtrRawPointerToWireType上[作为toWireType](opcv.js:21)     在Object.boundingRect(在new_处评估(opcv.js:21),:7:26)     在HTMLInputElement.inputElement.addEventListener(openCv.php:52) nonConstNoSmartPtrRawPointerToWireType @ opcv.js:21 boundingRect @ VM24025:7

inputElement.addEventListener @ openCv.php:52

控制台错误

2 个答案:

答案 0 :(得分:0)

findContours输出值contours具有0个元素,从而取消引用空指针时,将引发此错误。

答案 1 :(得分:0)

在您的代码中,

<br>
let cnt = contours.get(0);
<br>

cnt没有得到任何轮廓,那么opencv.js文件没有实现轮廓归零,然后给出了错误信息

<br>
(opcv.js:21 Uncaught TypeError: Cannot read property '$$' of undefined at RegisteredPointer.nonConstNoSmartPtrRawPointerToWireType [as toWireType] (opcv.js:21) at Object.boundingRect (eval at new_ (opcv.js:21), :7:26) at HTMLInputElement.inputElement.addEventListener (openCv.php:52) nonConstNoSmartPtrRawPointerToWireType @ opcv.js:21 boundingRect @ VM24025:7)

解决方案是使用其他图像或查找代码中返回的任何平面图像,而不使用平面图像