简体   繁体   English

使用cv :: imdecode后未修改cv :: Mat外部数据

[英]cv::Mat external data not being modified after using cv::imdecode

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. 我修改了示例以说明问题。

I am trying to use opencv to perform operations on a cv::Mat that is composed of external data. 我正在尝试使用opencv在由外部数据组成的cv::Mat上执行操作。

Consider this example: 考虑以下示例:

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;

The result of this is: 结果是:

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

It is clear this external array is not being modified, therefore new memory is being allocated for the cv::Mat array. 显然,该外部阵列未在修改,因此正在为cv::Mat阵列分配新的内存。 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] . 这应该不会引起复制操作,并且mat.data应该是指向extern_data[0]的指针。

What am I misunderstanding? 我有什么误会?

So far the way I have got my program to work is to use std::copy. 到目前为止,使程序正常工作的方法是使用std :: copy。 I am still wondering if there is a way to assign the result of cv::imdecode() directly to the external data. 我仍然想知道是否有一种方法可以将cv :: imdecode()的结果直接分配给外部数据。

Currently I am using 目前我正在使用

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

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()的结果直接分配给extern_data而无需附加的std::copy行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM