opencv - 我下载了用于处理的 OpenCV 库,但无法运行示例

标签 opencv processing

我根据我在这篇文章中提出的问题的建议下载了用于处理的 OpenCV 库:How do I install the openCV library for the Arduino IDE?

但是,除了“LiveCamTest”示例之外,我无法运行任何示例程序。在诸如此类的任何其他示例中:

import gab.opencv.*;

PImage src, dst;
OpenCV opencv;

ArrayList<Contour> contours;
ArrayList<Contour> polygons;

void setup() {
  src = loadImage("test.jpg"); 
  size(src.width, src.height/2);
  opencv = new OpenCV(this, src);

  opencv.gray();
  opencv.threshold(70);
  dst = opencv.getOutput();

  contours = opencv.findContours();
  println("found " + contours.size() + " contours");
}

void draw() {
  scale(0.5);
  image(src, 0, 0);
  image(dst, src.width, 0);

  noFill();
  strokeWeight(3);

  for (Contour contour : contours) {
    stroke(0, 255, 0);
    contour.draw();

    stroke(255, 0, 0);
    beginShape();
    for (PVector point : contour.getPolygonApproximation().getPoints()) {
      vertex(point.x, point.y);
    }
    endShape();
  }
}

我收到错误消息:无法根据您的代码确定此草图的大小。 谁能告诉我如何使这些示例起作用?谢谢。

最佳答案

您不能使用 setup() 函数中的变量调用 size() 函数。所以这行不通:

void setup() {
  src = loadImage("test.jpg"); 
  size(src.width, src.height/2);

您不能根据这样的图像尺寸绘制草图。您需要改用硬编码值。

您可能还想查看处理 3 中添加的 settings() 函数。来自 the reference :

The settings() function is new with Processing 3.0. It's not needed in most sketches. It's only useful when it's absolutely necessary to define the parameters to size() with a variable. Alternately, the settings() function is necessary when using Processing code outside of the Processing Development Environment (PDE). For example, when using the Eclipse code editor, it's necessary to use settings() to define the size() and smooth() values for a sketch..

The settings() method runs before the sketch has been set up, so other Processing functions cannot be used at that point. For instance, do not use loadImage() inside settings(). The settings() method runs "passively" to set a few variables, compared to the setup() command that call commands in the Processing API.

顺便说一下,这些问题与 C++ 无关,因此您可能希望停止使用 标记它们标签。

关于opencv - 我下载了用于处理的 OpenCV 库,但无法运行示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878041/

相关文章:

python - VSCode v1.35 - 运行时错误 : CMake must be installed to build the following extensions: dlib

java - 处理:几个圆圈互相对合+变色

javascript - 错误: 'CanvasElement' is undefined

java - 如何从一个 x,y 坐标到另一个坐标进行动画处理? (Java/处理)

python - 如何设置具有 calysto 处理功能的 Jupyter 笔记本以在 Binder 中运行?

python - 显示处理后的 numpy 图像的 Webbots (OpenCV Python)

android - OpenCV 和 C++ : "Can' t resolve variable 'Mat` "

python - 使用 OpenCV 从谷歌街景检测路灯

当使用 OpenCV CameraBridgeViewBase 时,Android 总是在随机时间间隔后重新启动

c++ - Arduino 和处理。错误不匹配