在视频捕获中合并处理OpenCV轮廓::宽度(0)和高度(0)不能<= 0 [英] Incorporate Processing OpenCV contour in video capture: : Width(0) and height(0) cannot be &lt;= 0

查看:817
本文介绍了在视频捕获中合并处理OpenCV轮廓::宽度(0)和高度(0)不能<= 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  }
}


推荐答案

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

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

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

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

其他潜在问题:


  1. 在像素可用并加载之前使用cam实例(因此最初为0维)

  2. 使用空图像初始化OpenCV

这些适用于您的代码的内容如下所示:

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

这篇关于在视频捕获中合并处理OpenCV轮廓::宽度(0)和高度(0)不能<= 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆