c++ - 在 OpenCV 中显式复制像素值时出现空白帧

标签 c++ video opencv video-processing

我一直在使用 OpenCV 2.4.3 将一些视频处理代码移植到 C++。以下测试程序非常模拟我的代码如何从视频中读取每一帧,对其内容进行操作,然后将新帧写入新视频文件。

奇怪的是,当像素被单独设置时,输出帧完全是黑色的,但是当克隆整个帧时,输出帧是正确的。

在实践中,我会使用这两个宏来访问和分配所需的值,但示例中使用的顺序扫描更清楚地说明了这个想法。

有谁知道我哪里出错了?

test.cpp:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;

#define RGB_REF(PR,NC,R,C,CH) (*((PR) + ((3*(NC)*(R)+(C))+(CH))))
#define GRAY_REF(PR,NC,R,C) (*((PR) + (NC)*(R)+(C)))

int main(int argc, char* argv[])
{
    string video_path(argv[1]);
    cerr << "Video path is " + video_path + "\n";
    VideoCapture capture(video_path);
    if ( !capture.isOpened() )
    {
        cerr << "Input file could not be opened\n";
        return 1;
    } else
    {
        string output_path(argv[2]);
        VideoWriter output;
        int ex = (int)capture.get(CV_CAP_PROP_FOURCC);
        Size S = Size((int) capture.get(CV_CAP_PROP_FRAME_WIDTH),
                (int) capture.get(CV_CAP_PROP_FRAME_HEIGHT));
        output.open(output_path,ex,capture.get(CV_CAP_PROP_FPS),S,true);
        if ( !output.isOpened() )
        {
            cerr << "Output file could not be opened\n";
            return 1;
        }
        unsigned int numFrames = (unsigned int) capture.get(CV_CAP_PROP_FRAME_COUNT);
        unsigned int m = (unsigned int) capture.get(CV_CAP_PROP_FRAME_HEIGHT);
        unsigned int n = (unsigned int) capture.get(CV_CAP_PROP_FRAME_WIDTH);
        unsigned char* im = (unsigned char*) malloc(m*n*3*sizeof(unsigned char));
        unsigned char* bw = (unsigned char*) malloc(m*n*3*sizeof(unsigned char));
        Mat frame(m,n,CV_8UC3,im);
        Mat outputFrame(m,n,CV_8UC3,bw);
        for (size_t i=0; i<numFrames; i++)
        {
            capture >> frame;
            for (size_t x=0;x<(3*m*n);x++)
            {
                bw[x] = im[x];
            }
            output << outputFrame; // blank frames
//            output << frame;  // works
//            output << (outputFrame = frame); // works
        }
    }
}

最佳答案

当您从 VideoCapture 查询一个帧作为 capture >> frame; 时,frame 被修改。比如说,它有一个新的数据缓冲区。所以im不再指向frame的缓冲区。
试试
bm[x] = frame.ptr()[x];

关于c++ - 在 OpenCV 中显式复制像素值时出现空白帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579156/

相关文章:

c++ - 访问静态超出范围的未定义行为吗?

python - py2exe在导入opencv时由于缺少DLL而无法创建EXE

python - cv2.VideoCapture 帧速率的差异取决于初始化参数

c++ - 为什么由于标量删除而调用 vector 删除析构函数?

c++ - 构造函数参数默认值的链接器问题

python - 使用 PyTorchVideo 加载用于训练视频分类模型的动力学数据集时出错

iphone - IOS 版本和 HTML5 视频支持

video - 查找实际的 RTMP 流 URL?

c++ - 如何从 Mat 变量编辑/读取 OpenCv 中的像素值?

c++ - 在不使用任何循环的情况下动态分配二维数组?