创建垫子后,从字节向量将图像加载到垫子上
Loading an image on a mat from a vector of bytes after the mat is created
我的情况是下一个:我正在开发一个多线程应用程序,其中不同的线程从套接字接收字节形式的图像,将它们存储在vector<char>
中,并应该使用它生成cv::Mat
以便能够使用 OpenCV 进行处理。好吧,复杂性主要是因为为了将每个线程的私有数据与其他线程的私有数据分开,我使用了一个结构数组,我用每个线程可能需要的所有数据结构声明了这些结构。因此,我还必须声明vector<char>
和其中的cv::Mat
,一旦向量接收到所有图像字节,我必须找到一种方法在cv::Mat
内适当地"加载"它们,而无需创建任何额外的字节,因为为每个线程保留的内存空间是固定的。
到目前为止我尝试过:
struct thread_data { //private data for threads
std::vector<char> buf_img;
Mat img_scene;
Mat img_temp;
//...
thread_data() {
buf_img.reserve(65000), // initialization with enough space
img_scene.create(700,500, CV_8U),
img_temp.create(700,500, CV_8U);
}
};
thread_data *tdata;
// declare array of structs
tdata = (thread_data * ) calloc(nthreads, sizeof(thread_data));
//...
// And the thread function, once received the image:
private_tm->img_temp = cv::Mat(private_tm->buf_img,true).clone(); //from vector to Mat
if( !private_tm->img_temp.data ) {
std::cout<< " --(!) Image could not be read " << std::endl;
private_tm->answer = "error";
}
else {
// decode image and call the function passing the pointer to the struct:
private_tm->img_scene = cv::Mat(cv::imdecode(private_tm->temp,1)).clone();
private_tm->answer = OCV_func((void*)private_tm);
}
<小时 />收到一张图像后的结果:
> Thread 2:
> ==12142== Invalid write of size 4
> ==12142== at 0x804B869: cv::Mat::release() (mat.hpp:369)
> ==12142== by 0x804EEDD: cv::Mat::operator=(cv::Mat const&) (mat.hpp:287)
> ==12142== by 0x804E8A2: thread_main(void*) (threads.cpp:408)
> ==12142== by 0x45A4D4B: start_thread (pthread_create.c:308)
> ==12142== by 0x46A7DDD: clone (clone.S:130)
> ==12142== Address 0x0 is not stack'd, malloc'd or (recently) free'd
> ==12142==
> ==12142==
> ==12142== Process terminating with default action of signal 11 (SIGSEGV)
> ==12142== Access not within mapped region at address 0x0
> ==12142== at 0x804B869: cv::Mat::release() (mat.hpp:369)
> ==12142== by 0x804EEDD: cv::Mat::operator=(cv::Mat const&) (mat.hpp:287)
> ==12142== by 0x804E8A2: thread_main(void*) (threads.cpp:408)
> ==12142== by 0x45A4D4B: start_thread (pthread_create.c:308)
> ==12142== by 0x46A7DDD: clone (clone.S:130)
我真的需要帮助,因为我觉得可能存在一个简单的解决方案,我无法看到。
<小时 />一些研究:
-我在 OpenCV 文档中的 cv::Mat 声明中搜索直到筋疲力尽,寻找可以解决问题的功能,但我没有运气。我找不到一个函数,如果不创建新的 Mat 可能会获取图像(从矢量、字符* 或文件(。垫子文档
-这是一个与我需要的类似的问题(答案非常不令人满意(:链接
提前谢谢。
你不能以你的方式使用calloc
。它只会为thread_data
对象分配内存,但不会调用其构造函数或其子对象的构造函数。
更改以下内容...
thread_data *tdata;
// declare array of structs
tdata = (thread_data * ) calloc(nthreads, sizeof(thread_data));
自。。。
thread_data *tdata = new thread_data();
我强烈建议使用智能指针,例如std::unique_ptr
这样您就不必担心调用delete
。
std::unique_ptr<thread_data> tdata(new thread_data());
相关文章:
- 无法将图像加载到纹理矢量 (SFML)
- 如何将硬编码图像加载到 OpenGL 纹理中
- C++ SDL2 图像加载格式与.bmp不同
- 土壤图像加载库参数
- 示例文件中的CImg图像加载错误
- OpenGL图像加载64位深度
- SFML图像加载在发布版本上失败 - 文件名损坏
- 如何将图像加载到painter.drawimage函数
- 将图像加载到Mat-vvector<向量<垫子>>
- OpenCV图像加载程序退出
- 从字节数组将图像加载到图形视图中
- 将图像加载到现有Mat中
- 处理文件和图像加载异常的最佳方法是什么?
- 将PNG或JPG图像加载到CMFCToolBarImages中
- stb图像加载库返回白色纹理
- 创建垫子后,从字节向量将图像加载到垫子上
- 图像加载功能不工作
- 将UTF-16图像加载到内存中
- 我怎么能得到像素的颜色在我的图像(加载灰色)
- 正在将位图图像加载到按钮