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

时间:2016-01-13 08:12:30

标签: java opencv image-processing processing video-processing

我希望每5秒使用Processing OpenCV库获取视频(或图像)的轮廓。我有以下代码,但对于行opencv = new OpenCV(this, cam);,它告诉我:宽度(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();
    }

  }
}

1 个答案:

答案 0 :(得分:0)

有一些事情,包括每5秒重新初始化一次OpenCV。

这是一个非常基本的草图,通过合并LiveCamTestFindContours然后添加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维)
  2. 使用空图像初始化OpenCV
  3. 应用于您的代码的代码如下所示:

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