IplImage 结构的 boost 序列化问题

2024-01-27

我无法让 boost 序列化模块与 OpenCV 的 IplImage 结构一起使用。 这是我用于序列化 IplImage 的代码(以及自定义结构中的一些 JSON 数据)

    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
    }

这是我反序列化相同结构的代码

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

反序列化后获得的结果图像在图像底部有像素噪声

序列化时:

反序列化时:


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

  • 您的数据可能是连续的,但相关部分(即显示的像素数据)不是连续的。
  • 数据来源不是frame->imageData。因此,一行的最后一个像素与下一行的第一个像素之间存在间隙(frame->imageData - frame->imageDataOrigin)
  • frame->widthStep and frame->imageSize是仅在特定对齐情况下才有意义的值。他们不应设置,并且对于使用创建的每个对象应该是不同的cvCreateImage

要序列化你需要做类似的事情

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

它仅保存缓冲区的可见部分(你关心的人)

要反序列化,您还需要使用相同的公式迭代水平线(步幅)。

您还可以转换为启用数据复制的 cv::Mat,这将给出大小为真实连续的数组width * height * nChannels然后将其序列化make_array.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IplImage 结构的 boost 序列化问题 的相关文章

随机推荐