数组到OpenCV矩阵

Array to OpenCV matrix

本文关键字:矩阵 OpenCV 数组      更新时间:2023-10-16

我有一个数组double dc[][],并希望将其转换为IplImage* image并进一步转换为视频帧。我要做的就是给我一个视频,我提取出一些特征,然后用提取出来的特征制作一个新的视频。我的方法是将视频分成几帧从每一帧中提取特征然后像这样进行更新在每一帧的迭代中,我得到一个新的dc

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以便我可以重建视频。谁来帮我一下。提前感谢

如果您同意使用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文档

编辑:为了使事情更具体,这里有一个从一些用户分配的数据制作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类将Mat保存到视频文件中。你只需要创建一个VideoWriter,打开一个视频文件,然后写你的帧(就像Mat一样)。你可以在这里看到一个使用VideoWriter的例子

下面是使用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最棘手的部分是打开文件,因为你有很多选项。您可以在这里看到不同编解码器的名称