数组到OpenCV矩阵

时间:2013-05-15 21:37:22

标签: c++ opencv

我有一个数组double dc[][],想要将其转换为IplImage* image并进一步转换为视频帧。 我必须做的是给了一个视频,我提取了一些功能,然后制作了一个新的视频,提取了所提取的功能。 我的方法是将视频划分为从每帧中提取特征的帧,然后像这样进行更新,并在帧的每次迭代中得到一个新的直流

double dc[48][44];
for(int i=0;i<48;i++)
{
  for(int j=0;j<44;j++)
  {
     dc[i][j]=max1[i][j]/(1+max2[i][j]);
  }
}

现在我需要以可以重建视频的方式保存此DC。任何人都可以帮助我。 提前致谢

1 个答案:

答案 0 :(得分:1)

如果您可以使用Mat,那么您可以为现有的用户分配内存创建Mat。其中一个Mat构造函数具有签名:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

参数是:

rows: the memory height, 
cols: the width, 
type: one of the OpenCV data types (e.g. CV_8UC3), 
data: pointer to your data, 
step: (optional) stride of your data

我们建议您查看Mat here

的文档

编辑:只是为了使事情更具体,这里是一个用一些用户分配数据制作Mat的例子

int main()
{
    //allocate and initialize your user-allocated memory
    const int nrows = 10;
    const int ncols = 10;
    double data[nrows][ncols];
    int vals = 0;
    for (int i = 0; i < nrows; i++)
    {
        for (int j = 0; j < ncols; j++)
        {
            data[i][j] = vals++;
        }
    }
    //make the Mat from the data (with default stride)
    cv::Mat cv_data(nrows, ncols, CV_64FC1, data);
    //print the Mat to see for yourself
    std::cout << cv_data << std::endl;
} 

您可以通过OpenCV VideoWriter class将Mat保存到视频文件中。您只需创建一个VideoWriter,打开一个视频文件,然后编写您的帧(如Mat)。您可以看到使用VideoWriter here

的示例

以下是使用VideoWriter类的简短示例:

//fill-in a name for your video 
const std::string filename = "...";
const double FPS = 30;
VideoWriter outputVideo;
//opens the output video file using an MPEG-1 codec, 30 frames per second, of size height x width and in color 
outputVideo.open(filename, CV_FOURCC('P','I','M,'1'), FPS, Size(height, width));

Mat frame;
//do things with the frame
// ...

//writes the frame out to the video file
outputVideo.write(frame);

VideoWriter的棘手部分是打开文件,因为你有很多选择。您可以看到different codecs here

的名称