javascript - 无法从函数访问 opencv.js 属性和方法
问题描述
html
我使用托管的 opencv.js有以下简单文件:
<!DOCTYPE html>
<html>
<head>
<script
async
src="https://docs.opencv.org/master/opencv.js"
onload="onOpenCvLoaded();"
type="text/javascript"
></script>
</head>
<body>
<script>
function onOpenCvLoaded() {
console.log(cv);
test(cv);
}
function test() {
console.log(new cv.Mat());
}
</script>
</body>
</html>
我希望在控制台中看到一个新的 Mat 实例,但我得到了:
Uncaught TypeError: cv.Mat is not a constructor
at test (test.html:18)
at onOpenCvLoaded (test.html:15)
at HTMLScriptElement.onload (test.html:9)
test @ test.html:18
onOpenCvLoaded @ test.html:15
onload @ test.html:9
但是new cv.Mat()
,如果我直接在控制台中输入,则可以。里面发生了什么,我怎样才能让 new cv.Mat() 在test
函数内工作?
解决方案
我找到了提供答案的链接:https ://dev.to/kjunichi/requireopencvjs-is-not-enough-for-using-opencvjs-8ff
cv
在完全初始化时执行一个函数:
cv['onRuntimeInitialized']=()=>{
let mat = new cv.Mat();
console.log(mat.size());
mat.delete();
};
推荐阅读
- javascript - 使用 get() 从 node.js 发送数据到 Angular
- batch-file - cmd forfiles 打印每个单词的文件路径不工作
- c# - DataTables 警告:table id=bootstrap-data-table - 无效的 JSON 响应 - DataTables JS 插件
- webpack - 安装 npm webpack-cli 导致无法理解的警告
- c - 使用 HCS12 微控制器防止读取损坏
- php - 使用 JQuery 向表中添加值。使用 PHP 循环创建的表
- csom - 从项目中获取版本
- c - 打印多个字符,格式化为整数
- xgboost - xgboost rank pairwise 什么是预测输入和输出
- ruby-on-rails - 路由在测试中不匹配,但在应用程序中被识别