捕捉摄像头preVIEW使用在OpenCV中。转换为RGB和灰色垫的。 Java语言。安卓转换为、摄像头、灰色、语言

2023-09-05 09:01:18 作者:訣別ㄨ

我想检测相机previews面孔。 我看到这个例子OpenCV的样本:

I want to detect faces on camera previews. I saw this example in OpenCV samples:

@Override
protected Bitmap processFrame(VideoCapture capture) {
    capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
    capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);

    if (mCascade != null) {
        int height = mGray.rows();
        int faceSize = Math.round(height * FdActivity.minFaceSize);
        List<Rect> faces = new LinkedList<Rect>();
        mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                , new Size(faceSize, faceSize));

        for (Rect r : faces)
            Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
    }

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    if (Utils.matToBitmap(mRgba, bmp))
        return bmp;

    bmp.recycle();
    return null;
}

我改写了这个code为我的项目(​​输入字节[]由previewCallback从previewFrame()数据):

I rewrote this code for my project (input byte[] data from onPreviewFrame() from PreviewCallback):

public Highlighting[] get(byte[] data) {

    matYuv = new Mat(480, 320, CvType.CV_8UC1);

    matYuv.put(0, 0, data);
    Imgproc.cvtColor(matYuv, matRgb, Imgproc.COLOR_YUV420sp2RGB, 4);

    Highlighting[] hl = null;

    Imgproc.cvtColor(matRgb, matGray, Imgproc.COLOR_RGB2GRAY, 0);

    if (cascade != null) {
        int faceSize = 50;
        List<Rect> faces = new LinkedList<Rect>();
        cascade.detectMultiScale(matGray, faces, 1.1, 2, 2, new Size(
                faceSize, faceSize));

        hl = new Highlighting[faces.size()];

        int i = 0;
        for (Rect r : faces) {
            hl[i] = new Highlighting((int) r.tl().x, (int) r.tl().y,
                    (int) r.br().x, (int) r.br().y, "");
            i++;
        }

        Log.i("FACES", String.valueOf(faces.size()));

    }

    return hl;
}

但我有问题,我的code不工作,原来的 - 它不检测脸部。难道是问题转换字节数组?

But i have problem, my code doesn't work as original - it doesn't detect faces. Could it be problem in converting byte array?

推荐答案

480 + 240(身高)的结果。

480+240 (as height) results from YUV 420 format.

这种格式具有Y型平面与小480x320,U和V平面,每个0.5 * Y平面(查找YUV格式的详细信息)的分辨率。作为一帧的所有3架飞机都存储在一个图片,你必须要分配足够的空间。

this format has Y-plane with 480x320, U and V plane with each 0.5*resolution of Y plane (lookup YUV formats for details). As all 3 Planes of one Frame are stored in one Image, you have to allocate enough Space.