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

Get RGB values from AVPicture and change to grey-scale in FFMPEG

本文关键字:FFMPEG 灰度 并在 AVPicture 获取 RGB      更新时间:2023-10-16

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

我已经能够通过以下文章获得图像数据"data[0]":http://blog.tomaka17.com/2012/03/libavcodeclibavformat-tutorial/

我想知道如何访问RGB格式的3字节pic.data[0]。我一直试图以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的新手,因此任何指导和帮助都将是非常可观的。

非常感谢

首先逐行提取每帧的行数据;迭代循环以查看框架的高度。

这是样品:

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);
        }