使用cv :: imdecode后,cv :: Mat外部数据未被修改 [英] cv::Mat external data not being modified after using cv::imdecode

查看:345
本文介绍了使用cv :: imdecode后,cv :: Mat外部数据未被修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

edit:在试图给出一个直接的问题示例时,我似乎忽略了造成真正问题的原因.我已经修改了示例以说明问题.

edit: In trying to give a straight forward example of the problem it appears I left out what was causing the real issue. I have modified the example to illustrate the problem.

我正在尝试使用opencv在由外部数据组成的cv::Mat上执行操作.

I am trying to use opencv to perform operations on a cv::Mat that is composed of external data.

请考虑以下示例:

unsigned char *extern_data = new unsigned char[1280*720*3];
cv::Mat mat = cv::Mat(1280, 720, CV_8UC3, extern_data); //Create cv::Mat external

//Edit - Added cv::imdecode
mat = cv::imdecode(mat,1);

//In real implementation it would be mat = cv::imdecode(image,'1')
// where image is a cv::Mat of an image stored in a mmap buffer

mat.data[100] = 99;

std::cout << "External array: " << static_cast<int>(extern_data[100]) << std::endl;
std::cout << "cv::Mat array: " << static_cast<int>(mat.data[100]) << std::endl;

其结果是:

> External array: 0
> cv::Mat array: 100

很明显,该外部阵列未在修改,因此正在为cv::Mat阵列分配新的内存.据我了解,这是不可能发生的!这应该不会引起复制操作,并且mat.data应该是指向extern_data[0]的指针.

It is clear this external array is not being modified, therefore new memory is being allocated for the cv::Mat array. From my understanding this was not suppose to happen! This should have caused no copy operation, and mat.data should be a pointer to extern_data[0].

我误会什么?

推荐答案

到目前为止,让我的程序正常工作的方法是使用std :: copy.我仍然想知道是否有一种方法可以将cv :: imdecode()的结果直接分配给外部数据.

So far the way I have got my program to work is to use std::copy. I am still wondering if there is a way to assign the result of cv::imdecode() directly to the external data.

当前我正在使用

unsigned char *extern_data = new unsigned char[1280*720*3];
cv::Mat mat = cv::Mat(1280, 720, CV_8UC3, extern_data); //Create cv::Mat external

mat = cv::imdecode(mat,1);

std::copy(mat.data, mat.data + 1280*720*3, extern_data);

我只是希望我能弄清楚如何将cv::imdecode()的结果直接分配给extern_data而无需附加的std::copy行!

I just wish i could figure out how to assign the result of cv::imdecode() directly to extern_data without the additional std::copy line!

这篇关于使用cv :: imdecode后,cv :: Mat外部数据未被修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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