Best method for limiting OpenCV resolution

Greg told me that limiting the resolution of an OpenCV instance is a good way to cut down on unnecessary computation. I see this as a good idea as well. However, I have had trouble setting the resolution while STILL accepting an image open the instance's construction.

In an ideal world I have a video coming in at 640 x 640. I would like to analyze this at 320 x 320 and then scale any drawing of contours back up to the 640 x 640 sketch.

In the reference:

OpenCV(PApplet theParent, int width, int height) 
          // Initialize OpenCV with a width and height.
OpenCV(PApplet theParent, PImage img) 
          // Initialize OpenCV with an image.

For instance, in the Find Contours example, where would you limit the resolution to say, half? It is using the version of "PApplet theParent, PImage img" so there is no room for a width or height argument? How do I mix these? Assign the image afterwards?

Any suggestions are appreciated.

Find Contours Example:

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

Answers

  • Thanks for the link GoToLoop. So it looks like one should use the method that defines it with the resolution at the top, and then give it the image in draw?

    This is very exciting, I must try this tonight!

  • I ended up modifying the example "FindContour" sketch. This worked very nicely.

    import gab.opencv.*;
    
    PImage src, dst;
    OpenCV opencv;
    
    ArrayList<Contour> contours;
    
    void setup() {
      src = loadImage("test.jpg"); 
      size(src.width, src.height/2);
      opencv = new OpenCV(this, src.width, src.height);
      opencv.loadImage(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);
    
        // Hard to get into a PGraphic?
        // Probably because it is a function defined elsewhere...
        contour.draw();
    
    
        stroke(255, 0, 0);
        beginShape();
        for (PVector point : contour.getPolygonApproximation().getPoints()) {
          vertex(point.x, point.y);
        }
        endShape();
      }
    
    }
    
Sign In or Register to comment.