Incorporate Processing OpenCV contour in video capture: : Width(0) and height(0) cannot be <= 0

扶醉桌前 提交于 2019-12-12 03:16:17

问题


I want to get the contour of video (or image) every 5 second, using Processing OpenCV library. I have the following code, but for the line opencv = new OpenCV(this, cam);, it tells me: Width(0) and height(0) cannot be <= 0. I think the program is that the second parameter in new OpenCV should be an image, as opposed to a camera capture, but what I should do to put them together?

import processing.video.*;
import gab.opencv.*;

OpenCV opencv;

ArrayList<Contour> contours;

Capture theCap; 
Capture cam; 

boolean recording = false; 

int imageIndex = 0;

int time = millis();
int wait = 5000;

void setup(){
  //size(640, 480);
  size(1280, 680);
  frameRate(30);
  background(0);
  String[] cameras = Capture.list();
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
    cam = new Capture(this, cameras[0]);
    cam.start();  
  }
}

void draw(){

  cam.read();
  if (millis() - time >= wait){
    time = millis(); 
    image(cam, 0, 0);
    opencv = new OpenCV(this, cam);
    opencv.gray();
    opencv.threshold(70); 
    contours = opencv.findContours();
    image(cam, 0, 0);

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

  }
}

回答1:


There are a few things a bit of, including re-initializing OpenCV every 5 seconds.

Here's a very basic sketch done by merging the LiveCamTest and FindContours then adding the 5s timer:

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

Capture video;
OpenCV opencv;

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

int thresh = 70;

int time = millis();
int wait = 5000;

void setup() {
  size(640, 480);
  noFill();

  video = new Capture(this, 640/2, 480/2);
  //initialize OpenCV only once
  opencv = new OpenCV(this, 640/2, 480/2);

  video.start();
}

void draw() {
  scale(2);

  if (millis() - time >= wait){
    //update OpenCV with video feed
    opencv.loadImage(video);
    image(video, 0, 0 );

    time = millis();
    opencv.gray();
    opencv.threshold(thresh);
    contours = opencv.findContours();
    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();
    }
  }
}
void mouseDragged(){
  thresh = (int)map(mouseX,0,width,0,255);
}
void captureEvent(Capture c) {
  c.read();
}

The other potential issues:

  1. Using the cam instance before pixels are available and loaded (hence 0,0 dimensions initially)
  2. Initialising OpenCV with an empty image

These applied to your code would look like this:

import processing.video.*;
import gab.opencv.*;

OpenCV opencv;

ArrayList<Contour> contours;

Capture theCap; 
Capture cam; 

boolean recording = false; 

int imageIndex = 0;

int time = millis();
int wait = 5000;

void setup(){
  size(640, 480);
  frameRate(30);
  background(0);
  noFill();

  String[] cameras = Capture.list();
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
    cam = new Capture(this, cameras[0]);
    cam.start();  
  }
  //initialize OpenCV once, with size
  opencv = new OpenCV(this,640,480);
}

void draw(){
  if (millis() - time >= wait){
    time = millis(); 
    image(cam, 0, 0);
    if(cam.width > 0 && cam.height > 0){//check if the cam instance has loaded pixels
      opencv.loadImage(cam);//send the cam
      opencv.gray();
      opencv.threshold(70); 
      contours = opencv.findContours();

      for (Contour contour : contours) {
        stroke(0, 255, 0);
        contour.draw();
      }
    }
  }
}
void captureEvent(Capture c){
  c.read();
}


来源:https://stackoverflow.com/questions/34761403/incorporate-processing-opencv-contour-in-video-capture-width0-and-height0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!