处理OpenCV和IPCapture库错误:宽度(0)和高度(0)不能<= 0

时间:2016-06-06 16:16:32

标签: opencv processing ip-camera

我希望通过IPCapture库从Ip相机获取视频,然后使用OpenCV库进行处理,在此视频中进行人脸识别。如果我只从IP摄像机获得视频,则代码可以正常工作,但是如果我还添加了面部识别它不起作用,并且在行opencv.loadImage(cam);它告诉我:宽度(0)和高度(0)不能是&lt; = 0。

 
import ipcapture.*;
import gab.opencv.*;
import java.awt.*;

IPCapture cam;
OpenCV opencv;

void setup() {
  size(512, 384);
  cam = new IPCapture(this, "http://192.168.1.74:90/cam_pic_new.php?  fullscreen", "", "");
  cam.start();
  opencv = new OpenCV(this, 512, 384);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void draw() {
  if (cam.isAvailable()) {
    cam.read();
    image(cam, 0, 0);
    opencv.loadImage(cam);
    noFill();
    stroke(0, 255, 0);
    strokeWeight(3);
    Rectangle[] faces = opencv.detect();
    println(faces.length);

    for (int i = 0; i < faces.length; i++) {
      println(faces[i].x + "," + faces[i].y);
      rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    }
  }
}

更新 谢谢你的回答,我试过的代码是:

import ipcapture.*;
import gab.opencv.*;
import java.awt.*;
IPCapture cam;
OpenCV opencv;

void setup() {
  size(512, 384);
  cam = new IPCapture(this, "http://192.168.1.74:90/cam_pic_new.php", "", "");
  cam.start();
  opencv = new OpenCV(this, 512, 384);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void draw() {
  if (cam.isAvailable()) {
    cam.read();
    image(cam, 0, 0);
    if (cam.width > 0 && cam.height > 0) {
      opencv.loadImage(cam);
    }
  }
}

错误位于第opencv.loadImage(cam);行,它告诉我: 宽度(0)和高度(0)不能是<= 0

1 个答案:

答案 0 :(得分:0)

您需要检查图像是否实际加载了任何像素。

由处理专家this answer查看George Profenza。特别是这一点:

 
if(cam.width > 0 && cam.height > 0){//check if the cam instance has loaded pixels
      opencv.loadImage(cam);//send the cam