栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

OpenCV cv :: Mat到std :: ifstream用于base64编码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV cv :: Mat到std :: ifstream用于base64编码

为了能够通过HTTP发送图像,您还需要对其宽度,高度和类型进行编码。您需要将序列Mat化为流,并使用libb64对该流进行编码。另一方面,您需要解码该流并反序列化图像以检索它。

我实现了一个小型测试程序,该程序使用

std::stringstream
缓冲区来进行序列化和反序列化。我选择了它,因为它同时扩展
std::istream
std::ostream
其libb64用途。

serialize
函数将a序列cv::Mat化为a
std::stringstream
。在其中,我写了图像的宽度,高度,类型,缓冲区的大小以及缓冲区本身。

该deserialize功能相反。它读取缓冲区的宽度,高度,类型,大小以及缓冲区。它效率不高,因为它需要分配一个临时缓冲区以从字符串流中读取数据。此外,它需要克隆映像,以便它不依赖临时缓冲区,并且它将处理自己的内存分配。我敢肯定,通过进行一些修补,它可以变得更有效率。

主要功能加载图像,对其进行序列化,使用libb64对其进行编码,然后对其进行解码,对其进行反序列化并将其显示在窗口中。这应该模拟您要尝试执行的操作。

// Serialize a cv::Mat to a stringstreamstringstream serialize(Mat input){    // We will need to also serialize the width, height, type and size of the matrix    int width = input.cols;    int height = input.rows;    int type = input.type();    size_t size = input.total() * input.elemSize();    // Initialize a stringstream and write the data    stringstream ss;    ss.write((char*)(&width), sizeof(int));    ss.write((char*)(&height), sizeof(int));    ss.write((char*)(&type), sizeof(int));    ss.write((char*)(&size), sizeof(size_t));    // Write the whole image data    ss.write((char*)input.data, size);    return ss;}// Deserialize a Mat from a stringstreamMat deserialize(stringstream& input){    // The data we need to deserialize    int width = 0;    int height = 0;    int type = 0;    size_t size = 0;    // Read the width, height, type and size of the buffer    input.read((char*)(&width), sizeof(int));    input.read((char*)(&height), sizeof(int));    input.read((char*)(&type), sizeof(int));    input.read((char*)(&size), sizeof(size_t));    // Allocate a buffer for the pixels    char* data = new char[size];    // Read the pixels from the stringstream    input.read(data, size);    // Construct the image (clone it so that it won't need our buffer anymore)    Mat m = Mat(height, width, type, data).clone();    // Delete our buffer    delete[]data;    // Return the matrix    return m;}void main(){    // Read a test image    Mat input = imread("D:\test\test.jpg");    // Serialize the input image to a stringstream    stringstream serializedStream = serialize(input);    // base64 enpre the stringstream    base64::enprer E;    stringstream enpred;    E.enpre(serializedStream, enpred);    // base64 depre the stringstream    base64::deprer D;    stringstream depred;    D.depre(enpred, depred);    // Deserialize the image from the depred stringstream    Mat deserialized = deserialize(depred);    // Show the retrieved image    imshow("Retrieved image", deserialized);    waitKey(0);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/365604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号