我正在尝试使用带有Java的OpenCV 2.4.8使用detectMultiScale检测视频流中的人脸。在网上搜索时,我注意到它的两个版本带有不同的参数列表。像here:
detectMultiScale(image, faceDetections) in [here][1] and face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) )
当我在代码中使用它时会出错:
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat
当我调试代码时,我发现错误发生在以下行:
face_cascade.detectMultiScale(frame_gray, faces);
我对这两个参数方法感到困惑,因为在OpenCV文档站点中,他们只有一个方法有七个参数。
detectMultiScale(const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
任何人都可以请描述这两个参数方法是什么,并且它与我的代码中的错误有任何关联。以下是我正在检查的代码:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetectSVM {
/**
* @param args
*/
public static void main(String[] args) {
//load opencv native library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier face_cascade = new CascadeClassifier("haarcascade_frontalface_alt.xml");
CascadeClassifier eyes_cascade = new CascadeClassifier("haarcascade_eye.xml");
String window_name = "Capture - Face detection.jpg";
System.out.println("capture through camera "+Core.VERSION);
//load the face xml cascade
if(!face_cascade.load("haarcascade_frontalface_alt.xml"))
{
System.out.println("Error loading face cascade");
}
else
{
System.out.println("Success loading face cascade");
}
//load the eyes xml cascade
if(!eyes_cascade.load("haarcascade_eye.xml"))
{
System.out.println("Error loading eyes cascade");
}
else
{
System.out.println("Success loading eyes cascade");
}
//detect default camera
VideoCapture capture = new VideoCapture(0);
if(!capture.isOpened())
{
System.out.println("Did not connected to camera.");
}
else
{
System.out.println("Conected to camera: "+capture.toString());
}
//create new Mat image
Mat frame = new Mat();
while(true){
capture.retrieve(frame);
Mat frame_gray = new Mat();
Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGRA2GRAY);
Imgproc.equalizeHist(frame_gray, frame_gray);
MatOfRect faces = new MatOfRect();
face_cascade.detectMultiScale(frame_gray, faces);
Rect[] facesArray = faces.toArray();
for(int i=0; i<facesArray.length; i++)
{
Point center = new Point(facesArray[i].x + facesArray[i].width * 0.5, facesArray[i].y + facesArray[i].height * 0.5);
Core.ellipse(frame, center, new Size(facesArray[i].width * 0.5, facesArray[i].height * 0.5), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray.submat(facesArray[i]);
MatOfRect eyes = new MatOfRect();
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0,new Size(30,30), new Size());
Rect[] eyesArray = eyes.toArray();
for (int j = 0; j < eyesArray.length; j++)
{
Point center1 = new Point(facesArray[i].x + eyesArray[i].x + eyesArray[i].width * 0.5, facesArray[i].y + eyesArray[i].y + eyesArray[i].height * 0.5);
int radius = (int) Math.round((eyesArray[i].width + eyesArray[i].height) * 0.25);
Core.circle(frame, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
}
Highgui.imwrite(window_name, frame);
capture.release();
}
}
}
答案 0 :(得分:1)
OpenCV是用C / C ++编写的。 Java包装器允许通过JNI访问本机代码。与C ++不同,Java不支持默认参数值。该文档适用于C ++,因此具有一个具有5个默认参数值的函数。为了解决这个问题,OpenCV Java包装器有6种方法。你得到的错误,理想情况下不应该是因为这个。
请尝试以下。
capture.release()
。释放应该在循环之外发生。使用如while( !done ) { ... }
capture.retrieve(frame);
之后,添加if( frame.empty() ) { continue; }