2

我正在处理人脸检测问题,我有使用 Androids FaceDetector 来查找人脸的工作代码,但我需要找出一种方法来实现 OpenCV/JavaCV 函数来检测人脸。这不是使用实时摄像机,它使用画廊中的图像,我能够检索该图像路径,但我似乎无法初始化 CvHaarClassifierCascade 分类器和 CvMemStorage 存储,如果有人不能指出我正确的方向或提供一些在 Java 中正确初始化这些变量的源代码。谢谢

4

2 回答 2

1

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

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

于 2013-02-13T11:05:45.397 回答
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();
于 2012-04-10T08:24:02.297 回答