Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图象矩阵到字符串OpenCV C++_C++_String_Opencv_Matrix_Type Conversion - Fatal编程技术网

图象矩阵到字符串OpenCV C++

图象矩阵到字符串OpenCV C++,c++,string,opencv,matrix,type-conversion,C++,String,Opencv,Matrix,Type Conversion,我必须在字符串中转换C++中的图像矩阵。 我找到了两种方法: const char *inputD = (const char*)(img.data); Size imageSiz = img.size(); int w = imageSiz.width; int h = imageSiz.height; int ch = img.channels(); _return.append(inputD, w*h*ch); 其中_return为: std::string& _return

我必须在字符串中转换C++中的图像矩阵。 我找到了两种方法:

const char *inputD = (const char*)(img.data);
Size imageSiz = img.size();
int w = imageSiz.width;
int h = imageSiz.height;
int ch = img.channels();
_return.append(inputD, w*h*ch);
其中_return为:

std::string& _return
这一个一直在工作。但我也找到了另一种方法:

string matAsStringL (imgL.begin<unsigned char>(), imgL.end<unsigned char>());
_return.push_back(matAsStringL);
当然,如果返回值不同:

std::vector<std::string> & _return
但是第二种方法不适用于彩色图像,而只适用于灰度图像。为什么?我想了解

第二个错误是: 在未知函数中,断言失败elemSize==sizeof_Tp。在mat.hpp中

例如:

img1 = imread(filenameL , 0); //Gray_scale
string matAsStringI(img1.begin<unsigned char>(), img1.end<unsigned char>());
//now in matAsString I have the info I need
cvtColor(img1, coloredImage1, CV_GRAY2RGB);
string matAsStringC(coloredImage1.begin<unsigned char>(), coloredImage1.end<unsigned char>()); //crash here with the assertion error

不适用于彩色图像1,但适用于img1。如果我更改为coloredImage1,并使用第一种方法,效果很好。

您可以在字符串构造函数中直接使用mat的迭代器,但您将只获得每个像素的第一个无符号字符,而不是所有数据

您必须对第一个和第二个解决方案进行折衷,例如

#include <string>
#include <vector>
#include <opencv2/opencv.hpp>

int main()
{
  const char *path = "/home/gerard_gaudeau/Downloads/image.jpg";
  cv::Mat mat = cv::imread(path);
  cv::Size size = mat.size();

  int total = size.width * size.height * mat.channels();
  std::cout << "Mat size = " << total << std::endl;

  std::vector<uchar> data(mat.ptr(), mat.ptr() + total);
  std::string s(data.begin(), data.end());                   
  std::cout << "String size = " << s.length() << std::endl;
  return 0;
}

您可以在字符串构造函数中直接使用mat的迭代器,但只会得到每个像素的第一个无符号字符,而不是所有数据

您必须对第一个和第二个解决方案进行折衷,例如

#include <string>
#include <vector>
#include <opencv2/opencv.hpp>

int main()
{
  const char *path = "/home/gerard_gaudeau/Downloads/image.jpg";
  cv::Mat mat = cv::imread(path);
  cv::Size size = mat.size();

  int total = size.width * size.height * mat.channels();
  std::cout << "Mat size = " << total << std::endl;

  std::vector<uchar> data(mat.ptr(), mat.ptr() + total);
  std::string s(data.begin(), data.end());                   
  std::cout << "String size = " << s.length() << std::endl;
  return 0;
}

您想要文本表示吗?不使用彩色图像是什么意思?你有错误吗?或者显示错误的数据?只是部分数据?我在未知函数中得到一个断言失败elemSize==sizeof_Tp。在mat.hppdid中,您想要文本表示?不使用彩色图像是什么意思?你有错误吗?或者显示错误的数据?只是部分数据?我在未知函数中得到一个断言失败elemSize==sizeof_Tp。在mat.hppSo中,对于彩色图像,我只得到一种颜色,而不是所有的矩阵,对吗?这就是它失败的原因?我在问题中添加了一个示例和错误事实上,我用RGB图像尝试了你的代码,但我没有断言维度的数量。。。奇怪的最后的字符串大小是图像大小的1/3。所以对于彩色图像,我只得到一种颜色,而不是所有的矩阵,对吗?这就是它失败的原因?我在问题中添加了一个示例和错误事实上,我用RGB图像尝试了你的代码,但我没有断言维度的数量。。。奇怪的最终字符串大小是图像大小的1/3。