OpenCV / JavaCV Android人脸检测初始化

时间:2012-04-07 18:20:13

标签: java android xml opencv javacv

我正在研究人脸检测问题,我有工作代码使用Androids FaceDetector来查找面,但我需要找到一种方法来实现OpenCV / JavaCV函数来检测面。这不是使用现场摄像头,它使用图库中的图像,我能够检索到图像路径,但我似乎无法获得CvHaarClassifierCascade分类器,并且CvMemStorage存储器初始化,如果有人不能指向我正确的方向或者提供一些在Java中正确初始化这些变量的源代码。 感谢

2 个答案:

答案 0 :(得分:1)

你可以这样做:只提供一个BufferedImage。

或者使用cvLoadImage(..)直接使用图像路径加载原始IplImage。

// provide an BufferedImage
BufferedImage image;

// Preload the opencv_objdetect module to work around a known bug.
Loader.load(opencv_objdetect.class);

// Path to the cascade file provided by opencv
String cascade = "../haarcascade_frontalface_alt2.xml"

CvHaarClassifierCascade cvCascade = new CvHaarClassifierCascade(cvLoad(cascade));

// create storage for face detection
CvMemStorage tempStorage = CvMemStorage.create();

// create IplImage from BufferedImage
IplImage original = IplImage.createFrom(image);

IplImage grayImage = null;

if (original.nChannels() >= 3) {
  // We need a grayscale image in order to do the recognition, so we
  // create a new image of the same size as the original one.
  grayImage = IplImage.create(image.getWidth(), image.getHeight(),
                IPL_DEPTH_8U, 1);
  // We convert the original image to grayscale.
  cvCvtColor(original, grayImage, CV_BGR2GRAY);
} else {
  grayImage = original.clone();
}

// We detect the faces with some default params
CvSeq faces = cvHaarDetectObjects(grayImage, cvCascade,
            tempStorage, 1.1, 3,
            0;

// Get face rectangles
CvRect[] fArray = new CvRect[faces.total()];
for (int i = 0; i < faces.total(); i++) {
            fArray[i] = new CvRect(cvGetSeqElem(faces, i));
        }
// print them out
for(CvRect f: fArray){
   System.out.println("x: " + f.x() + "y: " + f.y() + "width: " + f.width() +  "height: " + f.height());
}

tempStorage.release();

答案 1 :(得分:1)

类定义基本上是C中原始头文件的Java端口,以及仅由OpenCV的C ++ API公开的缺失功能。您可以参考此链接,其中包含http://code.google.com/p/javacv/

http://geekoverdose.wordpress.com/tag/opencv-javacv-android-haarcascade-face-detection/