如何解决TypeError:cv.Mat不是opencv.js的构造函数? [英] how to solve TypeError: cv.Mat is not a constructor opencv.js?

查看:682
本文介绍了如何解决TypeError:cv.Mat不是opencv.js的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想从相机中检测到aruco标记时,我在使用opencv.js时遇到问题.每次我尝试使用该方法

I have a problem using opencv.js when I want to detect aruco markers from my camera. Every time I try to use the method

let image = new cv.imread('img');

我收到以下错误:

TypeError: cv.Mat is not a constructor
    at Object.matFromImageData (VM52998 opencv.js:55)

我尝试使用这个问题来尝试解决我的问题问题.该问题的解决方案是使用异步加载,在加载脚本时调用onOpenCvReady(),这将为cv的onRuntimeInitialized事件设置处理程序.处理程序应执行必要的操作.

I tried to use this question to try and solve my issue. The solution from the question was to use async load, call onOpenCvReady() when the script is loaded, which will set a handler for onRuntimeInitialized event of cv. The handler should perform the necessary operations.

<script>
        function onOpenCvReady() {
            cv['onRuntimeInitialized']=()=>{
                alert('OpenCV.js is ready.');
                // do all operations 
</script>
<script async src="<%= BASE_URL %>static/opencv.js" onload="onOpenCvReady();"></script>

但是,当我尝试使用问题的解决方案时,遇到了一个问题: 根本没有调用功能onOpenCvReady().

However, when I tried to use the solution from the question I encoutered a problem: the function onOpenCvReady() wasn't called at all.

此外,我等待了15分钟以上(请确保),然后尝试直接在控制台中使用javascript let image = new cv.imread('img');行.我的一举一动和采取的每一步都会导致相同的问题.

In addition, I waited for more than 15 minutes (just to be sure) and then tried to use the line javascript let image = new cv.imread('img'); directly in console. Every move I made, and every step I took results in the same issue.

推荐答案

回到方格1之后,我重试了最基本的解决方案: 我只是将其添加到我的HTML页面中,而不是在此处 .

After going back to square 1, I had retried the most basic of solutions: I just added this to my HTML page, instead of everything written here.

<script src="<%= BASE_URL %>static/opencv.js" id="opencvjs"></script>

这并没有直接起作用,但是在调试了我的代码的其他部分之后,我确保我的页面不包含其他错误(这些错误根本与该问题无关).由于某些原因,我尚未进行调查并完全理解,在加载opencv.js脚本时出现其他错误已中断了加载过程.尽管听起来无关紧要,但这对opencv命令产生了巨大影响.

This didn't directly worked, but after debugging some other areas of my code, I made sure that my page doesn't contain other errors (which are not related to this issue at all). For some reason I've yet to investigate and fully understand, having other errors while loading the opencv.js script has interrupted the loading process. Although it sounds unrelated, this had a huge effect on opencv commands.

最终结果非常壮观:

let image = new cv.imread('img');
console.log(image);

输出:

Mat {$$: {…}}

这篇关于如何解决TypeError:cv.Mat不是opencv.js的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆