【问题标题】:Android: Fast rotate CameraPreviewAndroid:快速旋转CameraPreview
【发布时间】:2016-10-14 12:56:29
【问题描述】:

我正在使用 OpenCv 构建人脸检测应用程序。我正在处理回调 onPreviewFrame 上收到的预览帧数据。我在纵向模式下使用相机,而 onPreviewFrame 在横向模式下返回数据。我正在使用此代码旋转帧数据。

public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
    byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
    // Rotate the Y luma
    int i = 0;
    for (int x = 0; x < imageWidth; x++) {
        for (int y = imageHeight - 1; y >= 0; y--) {
            yuv[i] = data[y * imageWidth + x];
            i++;
        }
    }
    // Rotate the U and V color components
    i = imageWidth * imageHeight * 3 / 2 - 1;
    for (int x = imageWidth - 1; x > 0; x = x - 2) {
        for (int y = 0; y < imageHeight / 2; y++) {
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
            i--;
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
            i--;
        }
    }
    return yuv;
}

旋转数据后,我将字节数组转换为 OpenCv Mat。转换后,我传入 openCv 本机代码。

在横向模式下(不旋转预览数据),在处理相机预览后,我可以获得近 20 FPS。但是在纵向模式下,使用上述方法,FPS 会降低到 3 FPS。在测量rotateYUV420Degree90所花费的时间时,这种方法是罪魁祸首。

我是 OpenCv 的新手。有没有其他方法可以使用 java 代码或本机代码快速旋转预览数据。由于我的应用程序的复杂性,我不能使用 OpenCV 提供的JavaCameraView

【问题讨论】:

    标签: android opencv android-camera


    【解决方案1】:

    问题:不要按列顺序遍历图像。由于您没有受益于引用的局部性,因此代码运行速度会非常慢。

    对于opencv,可以结合使用转置和翻转命令。

    switch (angle) {
            case 0:
                srcImage.copyTo(dstImage);
                break;
            case 90:
                Core.transpose(srcImage, dstImage);
                Core.flip(dstImage, dstImage, 1);
                break;
            case 180:
                Core.flip(srcImage, dstImage, -1);
                break;
            case 270:
                Core.transpose(srcImage, dstImage);
                Core.flip(dstImage, dstImage, 0);
                break;
            default:
                Logger.error(
                    "ROTATE_IMAGE_CLOCKWISE: Incorrect rotation value received: {}", 
                    angle);
                return srcImage;
        }
    

    这与 Image(jpeg) 具有方向元数据时用于读取输入图像的代码相同。参见 opencv 源代码here。以上代码是java中的。

    查看我的回答here,了解定向的工作原理。把opencv和我回答的图片放在一起,很简单。

    【讨论】:

      【解决方案2】:

      我想你已经有了答案:

      Android: How to rotate a bitmap on a center point

      在您的情况下,您的旋转只是 mRotation = 90 度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-07
        • 1970-01-01
        • 2010-09-12
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多