0

我的处理代码如下。

import hypermedia.video.*;
import processing.video.*;
import java.awt.Rectangle;

OpenCV opencv;

int width = 320;
int height = 240;

void setup() {

size( 320, 240 ); //set window size

opencv = new OpenCV( this ); //setup openCV
opencv.capture( width, height ); // open video stream
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
}

void draw(){
opencv.read();
image(opencv.image(), 0, 0);
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
noFill();
  stroke(255,0,0);
  for( int i=0; i<faces.length; i++ ) {
    rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
  }
}

此代码工作几秒钟,然后发生异常。

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d961b22, pid=232, tid=4008
#
# JRE version: 6.0_33-b03
# Java VM: Java HotSpot(TM) Client VM (20.8-b03 mixed mode windows-x86 )
# Problematic frame:
# V  [jvm.dll+0xa1b22]
#
# An error report file with more information is saved as:
# C:\Documents and Settings\Administrator\Desktop\processing-2.0b7\hs_err_pid232.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#
4

1 回答 1

0

我认为这是 opencv_core.CvMemStorage 的内存分配问题。这是一个未在您的库中公开的类。我有同样的问题。我正在使用 javacv(直接,而不是 javacvpro),因为我想运行多个 haar 级联(您只能使用 javacvpro 或较旧的 hypermedia.video.* 加载一个)。如果我在每个帧上都运行它们,我很好。如果我在每一帧上运行不同的检测器(然后再次循环通过检测器),我会在几个循环后得到这个错误。

下面的片段失败,但这是我想要做的(用不同的检测器处理每个后续帧):

// Using JavaCV directly, not JavaCVPro!
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
String cascades[] = {
  "haarcascade_eye.xml",            // 0
  "haarcascade_eye_tree_eyeglasses.xml",    // 1
  "haarcascade_frontalface_alt.xml"      // 2
}
int detectors[] = {1,3};
// haar detectors to use

String cascPath = "C:/opencv/data/haarcascades/";
// preload multiple classifiers. can do this with javacvpro, not with javacv or hypermedia.video.*

void haarSetup() {
    for (int i = 0; i < detectors.length; i++) {
      String classifierFile = cascPath+cascades[detectors[i]];
      classifier[i] = 
        new opencv_objdetect.CvHaarClassifierCascade(opencv_core.cvLoad(classifierFile));
    }
    storage = opencv_core.CvMemStorage.create();
    opencv_core.cvClearMemStorage(storage); // is this needed? couldn't hurt, right?
}

// contains list of preloaded haar cascades. code not included here...
opencv_core.CvSeq features[] = new opencv_core.CvSeq[detectors.length];
int whichHaar = 0;

// run one cascade per frame, then cycle through them.
void processHaars(PImage piz) {
  // convert to IplImage...  
  BufferedImage imgBuf = (BufferedImage) piz.getNative();    
  opencv_core.IplImage iplImgOut=opencv_core.IplImage.createFrom(imgBuf);
  // do one haar cascade per invocation.
  int ii = whichHaar;
  features[ii] = opencv_objdetect.cvHaarDetectObjects(iplImgOut, classifier[ii], storage, 1.1, 3, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);
  whichHaar++;
  if (whichHaar >= detectors.length){
    whichHaar = 0;
    // is THIS causing the problem??
    opencv_core.cvClearMemStorage(storage);
  }
}

这个片段永远有效,但我不想要它的作用(在一个帧上运行所有检测器):

void processHaars(PImage piz) {
// convert to IplImage...  
BufferedImage imgBuf = (BufferedImage) piz.getNative();    
opencv_core.IplImage iplImgOut=opencv_core.IplImage.createFrom(imgBuf);

for (ii=0; ii<detectors.length; ii++)
    faces[ii] = opencv_objdetect.cvHaarDetectObjects(iplImgOut, classifier[ii], storage, 1.1, 3, opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING);

opencv_core.cvClearMemStorage(storage);
}

如果我找到完整的解决方案,我会发布它。

于 2013-02-03T22:51:40.450 回答