【问题标题】:Get RGB values from AVPicture and change to grey-scale in FFMPEG从 AVPicture 获取 RGB 值并在 FFMPEG 中更改为灰度
【发布时间】:2014-10-15 19:51:18
【问题描述】:

我的代码的主要目的是改变 FFMPEG 中 AVPicture 的 RGB 值。

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

我想知道如何访问 RGB 格式的 pic.data[0] 的 3 个字节。我一直在尝试通过 for-loop 以 2D 矩阵方式访问 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 还很陌生,因此任何指导和帮助都将非常重要。

非常感谢

【问题讨论】:

  • 为什么不使用 swscale 进行颜色转换,而不是自己做呢?
  • 我打算把这个函数并行移植到CUDA上。
  • 嗯,然后也许检查一下 - stackoverflow.com/questions/16223315/…。但是,如果您正在解码 H.264 或 MPEG-4,如果您已经计划使用 CUDA,我建议您跳过 FFmpeg 并使用 NVENC 或其他基于 GPU 的解码技术。
  • 刚刚发布了答案,希望对其他人也有帮助。

标签: c++ ffmpeg


【解决方案1】:

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

示例如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多