【问题标题】:Android RGB to YCbCr Conversion and output to imageViewAndroid RGB 到 YCbCr 转换并输出到 imageView
【发布时间】:2018-11-03 15:52:26
【问题描述】:

我正在进行图像处理,需要将 RGB 位图图像转换为 YCbCr 颜色空间。我检索了每个像素的 RGB 值并将转换矩阵应用到它。

public void convertRGB (View v) {
    if (imageLoaded) {
        int width = inputBM.getWidth();
        int height = inputBM.getHeight();

        int pixel;
        int alpha, red, green, blue;
        int Y,Cb,Cr;

        outputBM = Bitmap.createBitmap(width, height, inputBM.getConfig());

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                pixel = inputBM.getPixel(x, y);
                alpha = Color.alpha(pixel);
                red = Color.red(pixel);
                green = Color.green(pixel);
                blue = Color.blue(pixel);

                Y  =  (int) (0.299     *  red + 0.587  * green + 0.114 * blue);
                Cb =  (int) (128-0.169 *   red-0.331   * green + 0.500 * blue);
                Cr =  (int) (128+0.500 *   red - 0.419 * green - 0.081 * blue);

                int p = (Y << 24) | (Cb << 16) | (Cr<<8);

                outputBM.setPixel(x,y,p);

            }
        }
        comImgView.setImageBitmap(outputBM);
    }
}

问题是他输出的颜色与原始颜色不同。我尝试使用 BufferedImage 但它在 Android 中不起作用

原文:

转换后:

我可以知道在android java中处理YCbCr图像的正确方法是什么。

【问题讨论】:

    标签: android image


    【解决方案1】:

    尝试使用以下代码进行设置

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuvImage = new YuvImage(your_yuv_data, ImageFormat.NV21, width, height, null);
    yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
    iv.setImageBitmap(image);
    

    查看文档以获取YuvImage 类的详细说明。

    【讨论】:

    • 我试过这个设置并得到 D/skia: --- SkImageDecoder::Factory 返回空错误。我可以知道 your_yuv_data 到底是什么吗?
    • 它将是您的相机预览中的 byte[]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多