与发行IplImage的结构的系列化提振 [英] Issue with boost serialization of IplImage struct

查看:162
本文介绍了与发行IplImage的结构的系列化提振的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦升压序列化模块的OpenCV的IplImage结构结构的工作。
这里是我的code代表(在自定义的结构与一些JSON数据一起)序列化的IplImage

I'm having trouble getting the boost serialization module to work with OpenCV's IplImage struct. Here is my code for serializing an IplImage (along with some JSON data in a custom struct)

    template <class Archive>
    void save(Archive & ar, const unsigned int version) const
    {
        // First things first; save the essential variables
        // These are needed as input to the create function of IplImage
        ar & frame->width;
        ar & frame->height;
        ar & frame->depth;
        ar & frame->nChannels;
        ar & frame->widthStep;
        ar & frame->imageSize;

//            ar & frame->nSize;
//            ar & frame->ID;
//            ar & frame->dataOrder;
//            ar & frame->origin;
//            ar & frame->align;
//            ar & frame->widthStep;
//            ar & frame->imageSize;

        // Then save the actual image data
        ar & boost::serialization::make_array<char>(frame->imageData, (frame->width * frame->height * frame->nChannels) + 1);

        std::string metaString = meta.dump();
        ar & metaString; // ...and don't forget the meta data
    }

下面是我的code为反序列化相同的结构。

Here is my code for deserializing the same struct

    template <class Archive>
    void load(Archive & ar, const unsigned int version)
    {
        int width;
        int height;
        int depth;
        int nChannels;
        int widthStep;
        int imageSize;

        ar & width;
        ar & height;
        ar & depth;
        ar & nChannels;
        ar & widthStep;
        ar & imageSize;

        // Create the image header with this knowledge
        frame = cvCreateImage(cvSize(width, height), depth, nChannels);
        frame->widthStep = widthStep;
        frame->imageSize = imageSize;

        // And grab the rest of the data
//            ar & frame->nSize;
//            ar & frame->ID;
//            ar & frame->dataOrder;
//            ar & frame->origin;
//            ar & frame->align;
//            ar & frame->widthStep;
//            ar & frame->imageSize;

        // Now we have all the variables, we can load the actual image data
        ar & boost::serialization::make_array<char>(frame->imageData, (width * height * nChannels) + 1);

        // Deserialize the json data
        std::string metaString;
        ar & metaString;
        meta = json(metaString);
    }

生成的图像反序列化后,我获得了像素的噪声在图像的底部

The resulting image I obtain after deserializing has pixel noise at the bottom of the image

在序列化:

在这里输入的形象描述

在反序列化:

在这里输入的形象描述

推荐答案

垃圾邮件机器人的分析是正确的,但还有更多。

Spambot analysis is correct, but there is more.


  • 您的数据也许连续的,但相关的部分(即显示的像素数据)都没有。

  • 数据起源不是框架&GT;的imageData 。由于那有一个行的最后一个像素和大小的下一行的第一像素之间的间隙(框架&GT;的imageData - 框架&GT; imageDataOrigin)

  • 框架&GT; widthStep 框架&GT; IMAGESIZE 只给出了具体的调整值,其意义。他们的不应该设置,应该有望成为与创建的每个对象不同的 cvCreateImage

  • Your data maybe contiguous, but the relevant parts (ie the pixel data displayed) aren't.
  • The data origin is not frame->imageData. Because of that there is a gap between the last pixel of a row and the first pixel of the next row of size (frame->imageData - frame->imageDataOrigin)
  • frame->widthStep and frame->imageSize are values which make sense only given a specific alignment. They should not be set, and should be expected to be different for each object created with cvCreateImage

要序列化,你需要做这样的事情。

To serialize you need to do something like

for(int i = 0; i < height; ++i)
{
   ar & boost::serialization::make_array<char>(
         frame->imageData+i*widthStep, 
         width * nChannels);
}

它只保存缓冲的可见部分(你关心的)

反序列化还需要遍历用同样的公式水平线(步)。

to deserialize you also need to iterate over horizontal lines (strides) using the same formula.

您也可以转换为CV ::垫与数据复制启用,这将给大小的真正连续数组宽*高* N沟道,然后用序列化 make_array

You can also convert to a cv::Mat with data copy enabled, which will give a real contiguous array of size width * height * nChannels and then serialize it with make_array.

这篇关于与发行IplImage的结构的系列化提振的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆