使用openCv进行口腔检测只能在一个面上返回很多圆圈

时间:2015-09-03 12:05:51

标签: java android c++ opencv

我使用OpenCv与android studio一起检测图像中的面部以及每个面部的眼睛和嘴巴。但是,问题是每当我试图检测到嘴巴时,它会在一个错误的脸上返回多个圆圈。

以下是我为口腔检测添加的代码:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
...
InputStream iserM = getResources().openRawResource(
                                R.raw.haarcascade_mcs_mouth);
                        File cascadeDirERM = getDir("cascadeERM",
                                Context.MODE_PRIVATE);
                        File cascadeFileERM = new File(cascadeDirERM,
                                "haarcascade_mcs_mouth.xml");
                        FileOutputStream oserM = new FileOutputStream(cascadeFileERM);

                        byte[] bufferERM = new byte[4096];
                        int bytesReadERM;
                        while ((bytesReadERM = iserM.read(bufferERM)) != -1) {
                            oserM.write(bufferERM, 0, bytesReadERM);
                        }
                        iserM.close();
                        oserM.close();
...
//here begins
                        mJavaDetectorMouth = new CascadeClassifier(
                                cascadeFileERM.getAbsolutePath());
                        if (mJavaDetectorMouth.empty()) {
                            Log.e(TAG, "Failed to load cascade classifier");
                            mJavaDetectorMouth = null;
                        } else
                            Log.i(TAG, "Loaded cascade classifier from "
                                    + mCascadeFile.getAbsolutePath());
                        //here ends
...
}

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
...
Rect r = facesArray[i];
            MatOfRect mouths = new MatOfRect();
            Mat faceROI = mRgba.submat(facesArray[i]);
            mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());
            Rect[] mouthArray = mouths.toArray();

            for (int j = 0; j < mouthArray.length; j++){
                Point center1 = new Point(facesArray[i].x + mouthArray[j].x + mouthArray[j].width * 0.5,
                        facesArray[i].y + mouthArray[j].y + mouthArray[j].height * 0.5);
                int radius = (int) Math.round(mouthArray[j].width / 2);
                Imgproc.circle(mRgba, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
            }
...
}

1 个答案:

答案 0 :(得分:0)

我一直在四处寻找,因为我发布了这个问题并尝试了很多东西。但最后我找到了解决方案: 我改变了:

 mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());

要:

 mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1, 2,
                    Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                            | Objdetect.CASCADE_SCALE_IMAGE, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());

我解决了这个问题。