【问题标题】:Issue with boost serialization of IplImage structIplImage 结构的 boost 序列化问题
【发布时间】:2016-06-20 11:03:41
【问题描述】:

我无法让 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);
    }

反序列化后得到的图像底部有像素噪点

在序列化时:

反序列化时:

【问题讨论】:

    标签: c++ opencv serialization boost iplimage


    【解决方案1】:

    Spambot 分析是正确的,但还有更多。

    • 您的数据可能是连续的,但相关部分(即显示的像素数据)不是。
    • 数据来源不是frame-&gt;imageData。因此,在一行的最后一个像素和下一行的第一个像素之间存在间隙(frame-&gt;imageData - frame-&gt;imageDataOrigin)
    • frame-&gt;widthStep 和 frame-&gt;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 对其进行序列化。

    【讨论】:

    • 这解决了问题。也感谢您对 cv::Mat 转换的建议。我知道 IplImage 相当旧,但由于我还链接到使用旧 opencv 实现的其他库,我将坚持使用 IplImage
    【解决方案2】:

    您的图像的下半部分似乎是随机存储器。最有可能的是,char 数组的长度必须更长。 IplImage 支持 widthStep 大于 width*nChannels 的对齐数据。 您的计算只能处理没有对齐的 8Bit 图像。

    使用 make_array 和 imageSize。

    ar & boost::serialization::make_array<char>(frame->imageData, imageSize);
    

    【讨论】:

    • 不起作用:我在 streambuf 类中得到一个 EXC_BAD_ACCESS
    • 在保存或加载期间? (当然,两者都应该使用相同的尺寸。)
    • 还要注意 IplImage 很老了,cv::Mat 是较新的数据结构。 (序列化应该类似。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多