简体   繁体   English

来自表示RGB图像的浮点数组的初始CV :: MAT

[英]Initial CV::MAT from a float array representing RGB image

Introduction to my problem: 我的问题简介:
I have serialized myself a CV::MAT object in c++ and sent in to a python program (more accurately, I have a python program that uses a c++ .exe as subprocess..), I deserialize it in python back into a 2D array of (R,G,B) ints 我已经用C ++序列化了一个CV::MAT对象,并发送给python程序(更准确地说,我有一个使用c ++ .exe作为子进程的python程序。),我在python中将其反序列化为2D数组(R,G,B)个整数

Now I have a different method in my python service that on demand sends this 2D array back to the c++ process and have it do something I DONT want to use boost serializer because its basically hell I have serialized it myself from python and manage to give the c++ process a flattened array along with the dimentions of the 2D array (rows and cols) 现在我在python服务中有一个不同的方法,可以按需将2D数组发送回c ++进程,让它做一些我不想使用boost serializer的事情,因为它基本上是我自己从python进行序列化并设法提供给C ++处理扁平化的数组以及2D数组的尺寸(行和列)

Now I have no idea how I construct a CV::MAT object back from this data i know there's Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP) which might do the job but no idea what goes insize the step in case its what I think it is 现在我不知道如何从该数据构造一个CV::MAT对象,我知道有Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP)可以完成工作,但不知道是什么加大步骤以防万一我认为是

Any help would be highly appreciated! 任何帮助将不胜感激!

This is the c++ code I have so far 这是我到目前为止的C ++代码

    vector<string> imgVector;
    int imgRows = stoi(kp.at(7));
    int imgCols = stoi(kp.at(8));
    split(kp.at(2), ' ', back_inserter(imgVector)); // kp.at(2) is a ' ' seperated array of integers
    int* pixels = (int*)malloc(sizeof(int) * imgVector.size());
    for (int j = 0; (unsigned)j < imgVector.size(); i++){
        pixels[j] = stoi(imgVector[j]);
    }
    Mat image(imgRows, imgCols, CV_32F, pixels);

But I think it will make a 2D array of integers and not of RGB tuples like I want 但我认为它将构成一个2D整数数组,而不是我想要的RGB元组

The corrected items: 更正的项目:

  • You need the type to be CV_8UC3 to store a 3-channel int array. 您需要类型为CV_8UC3来存储3通道int数组。
  • The size of the mat should be rows*cols*3. 垫子的大小应为行*列* 3。
  • The data type per pixel should be uchar . 每个像素的数据类型应为uchar

Following those, the following code should work. 在这些之后,以下代码应该起作用。

vector<string> imgVector;
int imgRows = stoi(kp.at(7));
int imgCols = stoi(kp.at(8));
split(kp.at(2), ' ', back_inserter(imgVector)); // kp.at(2) is a ' ' seperated array of integers
uchar* pixels = (uchar*)malloc(sizeof(uchar) * imgRows*imgCols*3);
for (int j = 0; (unsigned)j < imgVector.size(); i++){
    pixels[j] = uchar(stoi(imgVector[j]));
}
Mat image(imgRows, imgCols, CV_8UC3, pixels);

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

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