首页 > 代码库 > YV12转opencv IplImage

YV12转opencv IplImage

<pre name="code" class="cpp">    int bet(int min, int max, int val)
    {
        if (val > max)
            return max;
        else if (val < min)
            return min;
        else
            return val;
    }


    void Yv12ToImg(uchar *pin, IplImage* img)
    {
        uchar y, cb, cr;

        int ySize = img->width * img->height;
        int uSize = ySize / 4;

        uchar *pY = pin;
        uchar *pU = pY + ySize;
        uchar *pV = pU + uSize;

        uchar r, g, b;

        int iheight = img->height;
        int iwidth = img->width;

        int hwidth = iwidth / 2;

        uchar* p = (uchar*)(img->imageData);
        for (int h = 0; h < iheight; h++)
        {
            for (int w = 0; w < iwidth; w++)
            {
                y = pY[h*iwidth + w];

                int offuv = h/2 * hwidth + w/2;
                cb = pU[offuv];
                cr = pV[offuv];

                b = uchar(bet(0, 255,(y + 1.772*(cb - 128))));
                g = uchar(bet(0, 255, (y - 0.344*(cb - 128) - 0.714*(cr - 128))));
                r = uchar(bet(0, 255, (y + 1.402*(cr - 128))));

                int offw = w * 3;
                p[offw] = b;
                p[offw + 1] = g;
                p[offw + 2] = r;
            }
            p += img->widthStep;
        }

    }

YV12转opencv IplImage