在视频捕获中合并处理 OpenCV 轮廓::宽度 (0) 和高度 (0) 不能 <= 0

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

我想每 5 秒获取一次视频(或图像)的轮廓,使用 Processing OpenCV 库。我有以下代码,但对于行 opencv = new OpenCV(this, cam);,它告诉我:Width(0) 和 height(0) 不能 <= 0。我认为程序是 new OpenCV 中的第二个参数应该是图像,而不是相机捕获,但是我应该如何将它们放在一起?

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

  }
}

有几样东西,包括re-initializing OpenCV 每 5 秒。

这是一个非常基本的草图,通过合并 LiveCamTest and FindContours 然后添加 5s 计时器完成:

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

其他潜在问题:

  1. 在像素可用和加载之前使用 cam 实例(因此最初为 0,0 维)
  2. 用空图像初始化 OpenCV

应用于您的代码的这些将如下所示:

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