c++ opencv imencode imdecode string转换

本文详细介绍了在C++中使用OpenCV的imencode和imdecode函数进行图像的字符串编码与解码操作,包括如何将图像转换为字符串并存储,以及如何从字符串还原图像。同时,讨论了这两种方法在实际应用中的优缺点和常见应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mat mat(pCodecCtx->height, pCodecCtx->width, CV_8UC3, pFrameYUV->data[0], pFrameYUV->linesize[0]);

imshow("frame", mat);

if (!mat.empty()) {

std::vector<uchar> data_encode;

int res = imencode(".jpg", mat, data_encode);

std::string str_encode(data_encode.begin(), data_encode.end());

//char *char_r = new char[str_encode.size()];
 
char* char_r=(char *)malloc(sizeof(char) * (str_encode.size()));

memcpy(char_r, str_encode.data(), sizeof(char) * (str_encode.size()));

//strcpy(char_r, str_encode.data());

//char_r = const_cast<char*>(str_encode.data());

//char_r[str_encode.size() - 1] = '\0';

Mat img_decode;

std::string str_tmp(char_r, str_encode.size());
if (str_tmp.compare(str_encode) == 0) {
printf("ok");
}

std::vector<uchar> data(str_tmp.begin(), str_tmp.end());
img_decode = imdecode(data, CV_LOAD_IMAGE_COLOR);
imshow("pic", img_decode);
waitKey(10);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值