user6233283
user6233283

Reputation:

I downloaded the OpenCV library for Processing, but can't run the examples

I downloaded the OpenCV library for Processing as per the advice of the question I asked on this post: How do I install the openCV library for the Arduino IDE?

However, I can't run any of the example programs except the "LiveCamTest" example. On any other example such as this:

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();
  }
}

I get the error: The size of this sketch could not be determined from your code. Can anyone tell me how to get these to examples to work? Thank you.

Upvotes: 1

Views: 676

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

You can't call the size() function with variables from the setup() function. So this won't work:

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

You can't base a sketch off of an image size like that. You need to use hard-coded values instead.

You might also want to look into the settings() function that was added in Processing 3. From 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.

By the way, these questions have nothing to do with C++, so you might want to stop tagging them with the tag.

Upvotes: 0

Related Questions