简体   繁体   English

Android Yuv420sp到OpenCV中的ARGB

[英]Android Yuv420sp to ARGB in OpenCV

I am trying to send a raw image from the previewcallback from the phone to PC. 我正在尝试将预览回拨中的原始图像从手机发送到PC。 The PC will then process the image. 然后PC将处理图像。 I am use OpenCV library to do the image processing. 我使用OpenCV库来进行图像处理。 At the moment i just write a function in previewcallback to save the byte array to a file, and copy the file to pc and I wrote a simple program to first read in the file and save it into memeory and then directly copy the data to the IplImage.imageData. 目前我只是在previewcallback中编写一个函数来将字节数组保存到文件中,然后将文件复制到pc上,我写了一个简单的程序,首先读入文件并将其保存到文件中,然后直接将数据复制到文件中。 IplImage.imageData。 But I always get a garbage image. 但我总是得到一个垃圾图像。

I use this function I found on net to do the converstion between YUV420SP to ARGB. 我使用我在网上找到的这个函数来完成YUV420SP到ARGB之间的转换。 Thanks for your help. 谢谢你的帮助。

void decodeYUV420SP(int* rgb, char* yuv420sp, int width, int height) {
    int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0) y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }
}

您可以使用OpenCV4Android转换器:

Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGBA);

YUV420 images are an odd shape, the Y (luminescence) part is half the width and heigth of the image but this is followed by the Cr and Cb parts at half width but quarter height. YUV420图像是奇怪的形状,Y(发光)部分是图像的宽度和高度的一半,但是接着是半宽但四分之一高度的Cr和Cb部分。
Normally they aren't displayed directly so nobody cares about the odd packing. 通常他们不会直接显示,所以没有人关心奇怪的包装。

The result is a color image with only 1.5bytes/pixel instead of for rgb 结果是一个只有1.5bytes /像素而不是rgb的彩色图像

Based on my understanding, if an image that is 640*480. 根据我的理解,如果图像是640 * 480。 In the YUV420 byte buffer, the first 640*480 bytes will be correspond to the Y part. 在YUV420字节缓冲区中,前640 * 480字节将对应于Y部分。 And followed by 320*480 bytes of Cb and Cr interleavely, so first 640*480 byte of Y and the following 320*480 byte of CbCr, which are down sampled in a 2-by-2 grid 接着是320 * 480字节的Cb和Cr交错,因此首先是640 * 480字节的Y和随后的320 * 480字节的CbCr,它们在2×2网格中进行下采样

Did you manage to do this? 你设法做到了吗? If not take a look at the android-opencv project - they have a method for android yuv to bgr. 如果不看看android-opencv项目 - 他们有一个android yuv到bgr的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM