首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在opencv.js中使用goodFeaturesToTrack

尝试在opencv.js中使用goodFeaturesToTrack
EN

Stack Overflow用户
提问于 2021-01-24 23:53:36
回答 1查看 93关注 0票数 0

我正在阅读Tutorial,并试图将示例重新编码为javascript,以尝试goodFeaturesToTrack(),但收效甚微,我就是找不到任何有文档记录的javascript示例进行比较。

我正在尝试找出最终需要什么来检测一张纸上的照片上的4个角,甚至不知道goodFeaturesToTrack是否是正确的方向,但是我无法在javascript中使用这个基本的样本。

python示例如下所示:

代码语言:javascript
复制
    import numpy as np
    import cv2 as cv
    from matplotlib import pyplot as plt
    img = cv.imread('blox.jpg')
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    corners = cv.goodFeaturesToTrack(gray,25,0.01,10)
    corners = np.int0(corners)
    for i in corners:
       x,y = i.ravel()
       cv.circle(img,(x,y),3,255,-1)
    plt.imshow(img),plt.show()

要将此代码转换为javascript运行示例,我有以下代码:

代码语言:javascript
复制
    <html>
    <head>
    <script src='https://docs.opencv.org/3.4.0/opencv.js'></script>
    </head>
    <body>
       <canvas id="canvas" style='border:solid 1px red'></canvas>

       <script>
       const canvas = document.querySelector('#canvas');
       var context = canvas.getContext('2d');
       var img = new Image();
       img.src = 'images/shitomasi_block1.jpg'; // taken from https://docs.opencv.org/4.0.0- 
       beta/shitomasi_block1.jpg

       img.onload = function() {
          canvas.width = 200;
          canvas.height = 200;
          var hRatio = canvas.width / img.width;
          var vRatio = canvas.height / img.height;
          var ratio = Math.min(hRatio, vRatio);
          context.drawImage(img, 0, 0, img.width, img.height,
            0, 0, img.width * ratio, img.height * ratio);

          console.log('grayscaling the image...');
          var src = cv.imread(canvas.id);
          let dst = new cv.Mat();
          var grayscale = cv.cvtColor(src, dst, cv.COLOR_RGB2GRAY, );
          cv.imshow(canvas.id, dst);

          console.log('doing good features....');
          var green = new cv.Scalar(0, 255, 0);
          console.log('24');
          // xx the code then fails on the next line....
          var corners = cv.goodFeaturesToTrack(grayscale, 25, 0.01, 10)

          // the following lines are not tested and are likely to need some work...
          console.log('28');
          console.log(corners);
          ?? for (var i = 0; i < corners.length; i++) {
             ?? var x, y = i.ravel();
             ?? cv.circle(img, (x, y), 3, green, -1);
          ??}
          ??plt.imshow(img), plt.show();
    }

    </script>
    </body>
    </html>

我得到的错误是:

代码语言:javascript
复制
    Uncaught BindingError {name: "BindingError", message: "Function 'goodFeaturesToTrack' called with an inva… arg5Wired, arg6Wired, arg7Wired, arg8Wired);↵})!", stack: "BindingError: Function 'goodFeaturesToTrack' calle…p://localhost/test/public_html/test2.html:37:22)"} 
    message: "Function 'goodFeaturesToTrack' called with an invalid number of arguments (4) - expects one of (,,,,,function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4) {↵if (arguments.length !== 5) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 5 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5) {↵if (arguments.length !== 6) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 6 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6) {↵if (arguments.length !== 7) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 7 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {↵if (arguments.length !== 8) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 8 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {↵if (arguments.length !== 9) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 9 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵var arg8Wired = argType8.toWireType(null, arg8); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired, arg8Wired);↵})!"
    name: "BindingError" 
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* stack: "BindingError: Function 'goodFeaturesToTrack' called with an invalid number of arguments (4) - expects one of (,,,,,function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4) {↵if (arguments.length !== 5) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 5 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5) {↵if (arguments.length !== 6) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 6 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6) {↵if (arguments.length !== 7) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 7 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {↵if (arguments.length !== 8) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 8 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {↵if (arguments.length !== 9) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 9 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵var arg8Wired = argType8.toWireType(null, arg8); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired, arg8Wired);↵})!↵if (arguments.length !== 5) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 5 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5) {↵if (arguments.length !== 6) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 6 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6) {↵if (arguments.length !== 7) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 7 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {↵if (arguments.length !== 8) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 8 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired);↵},function goodFeaturesToTrack(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {↵if (arguments.length !== 9) {↵throwBindingError('function goodFeaturesToTrack called with ' + arguments.length + ' arguments, expected 9 args!');↵}↵var arg0Wired = argType0.toWireType(null, arg0); // Mat↵var arg1Wired = argType1.toWireType(null, arg1); // Mat↵var arg2Wired = argType2.toWireType(null, arg2); // int↵var arg3Wired = argType3.toWireType(null, arg3); // double↵var arg4Wired = argType4.toWireType(null, arg4); // double↵var arg5Wired = argType5.toWireType(null, arg5); // Mat↵var arg6Wired = argType6.toWireType(null, arg6); // int↵var arg7Wired = argType7.toWireType(null, arg7); // bool↵var arg8Wired = argType8.toWireType(null, arg8); // double↵invoker(fn, arg0Wired, arg1Wired, arg2Wired, arg3Wired, arg4Wired, arg5Wired, arg6Wired, arg7Wired, arg8Wired);↵})!↵    at BindingError.<anonymous> (http://localhost/test/public_html/javascript/opencv.3.4.js:30:6819009)↵    at new BindingError (eval at createNamedFunction (http://localhost/test/public_html/javascript/opencv.3.4.js:30:6818718), <anonymous>:4:34)↵    at throwBindingError (http://localhost/test/public_html/javascript/opencv.3.4.js:30:6824136)↵    at Object.proto.<computed> [as goodFeaturesToTrack] (http://localhost/test/public_html/javascript/opencv.3.4.js:30:6829116)↵    at Image.img.onload (http://localhost/test/public_html/test2.html:37:22)"
*/
    __proto__: Error

我想要问的第一个问题是,这是否是正确的方向,然后如果有人看到goodFeaturesToTrackjavascript中实际工作-使用示例代码片段来显示我的错误所在。

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

发布于 2021-07-09 17:20:52

正确的方法是

代码语言:javascript
复制
let corners = new cv.Mat();
cv.goodFeaturesToTrack(grayscale, corners, 4, 0.5, 50);

它给了你一堆“角”,但我不知道如何利用它^^

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65872699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档