使用openCv的嘴巴检测只在一张脸上返回很多圆圈

Mouth detection using openCv returns a lot of circles in only one face

本文关键字:一张 返回 openCv 检测 使用      更新时间:2023-10-16

我使用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);
            }
...
}

自从我发布这个问题以来,我一直在四处寻找,并尝试了很多事情。但最后我找到了解决办法:我改了:

 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());
我解决了这个问题。