从AVPicture获取RGB值并在FFMPEG中更改为灰度

时间:2014-10-15 19:51:18

标签: c++ ffmpeg

我的代码的主要动机是在FFMPEG中更改AVPicture中的RGB值。

我可以通过以下文章获取图像数据“data [0]”:http://blog.tomaka17.com/2012/03/libavcodeclibavformat-tutorial/

我想知道如何访问RGB格式的pic.data [0]的3个字节。我一直试图以2D矩阵方式通过for循环访问pic.data [i] [j],但第j个元素> 3。

这方面的任何指导都会有所帮助。

代码在这里:

AVPicture pic;
        avpicture_alloc(&pic, PIX_FMT_RGB24, mpAVFrameInput->width,mpAVFrameInput->height);
        auto ctxt = sws_getContext(mpAVFrameInput->width,mpAVFrameInput->height,static_cast<PixelFormat>(mpAVFrameInput->format),
            mpAVFrameInput->width, mpAVFrameInput->height, PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr);

        if (ctxt == nullptr)
            throw std::runtime_error("Error while calling sws_getContext");
        sws_scale(ctxt, mpAVFrameInput->data, mpAVFrameInput->linesize, 0, mpAVFrameInput->height, pic.data,
            pic.linesize);


    for (int i = 0; i < (mpAVFrameInput->height-1); i++) {

        for (int j = 0;  j < (mpAVFrameInput->width-1); j++) {
        printf("\n value: %d",pic.data[0][j]);

        }

    }

我想到的伪代码是:

For each pixel in image {
Red = pic.data[i][j].pixel.RED;
Green = pic.data[i][j].pixel.GREEN;
Blue = pic.data[i][j].pixel.BLUE;
GRAY = (Red+Green+Blue)/3;
Red = GRAY;
Green = GRAY;
Blue = GRAY;
Save Frame;}

我对FFMPEG很新,因此任何指导和帮助都会非常值得注意。

非常感谢

1 个答案:

答案 0 :(得分:0)

首先逐行提取每行的行数据;迭代循环保持在框架的高度。

以下是样本:

int FrameHeight = FrameInput->height;
        int FrameWidth = FrameInput->width;
        for(int Counter=0; Counter<FrameHeight; Counter++)
        {
            int RowSize = FrameWidth*sizeof(uint8_t)*3;
            uint8_t* RowData = (uint8_t*) malloc(RowSize);

            memset(RowData, 0, RowSize);

            memcpy(RowData, AVFrameInput->data[0]+Counter*AVFrameInput->linesize[0], RowSize);

            for(int k=0;k<AVFrameInput->linesize[0];++k)
            {

                if(RowData[k]> 200)
                {
                    RowData[k] = RowData[k]/3;

                }
                else
                {
                    if(RowData[k] > 150)
                    {
                        RowData[k] = RowData[k]/3;


                    }
                    else
                    {
                        RowData[k] = RowData[k]/3;

                    }
                }


            } 

            memcpy(AVFrameInput->data[0]+Counter*AVFrameInput->linesize[0], RowData, RowSize);
        }