C++ 将std::string转换回使用std::stringstream生成的cv::Mat<&书信电报;cv::Mat

C++ 将std::string转换回使用std::stringstream生成的cv::Mat<&书信电报;cv::Mat,c++,opencv,stringstream,mat,C++,Opencv,Stringstream,Mat,我正在为立体视觉保存校准数据,而不是opencv中给定的YAML数据结构,它采用了一种特殊的数据格式,这使我更加灵活 因此,我使用了一些技巧将cv::Mat转换为std::字符串: cv::Mat mat; // some code std::stringstream sstrMat; sstrMat << mat; saveFoo(sstrMat.str()); // something like this to save the matrix 我的问题是与之相反的操作:将这个s

我正在为立体视觉保存校准数据,而不是opencv中给定的YAML数据结构,它采用了一种特殊的数据格式,这使我更加灵活

因此,我使用了一些技巧将cv::Mat转换为std::字符串:

cv::Mat mat;
// some code
std::stringstream sstrMat;
sstrMat << mat;
saveFoo(sstrMat.str()); // something like this to save the matrix
我的问题是与之相反的操作:将这个std::字符串转换回cv::Mat

我尝试过这样的代码:

cv::Mat mat;
std::stringstream sstrStr;
sstrStr << getFoo() // something like this to get the saved matrix

sstrStr >> mat;  // idea-1: generate compile error
mat << sstrStr;  // idea-2: does also generate compile error
cv::Mat-Mat;
std::stringstream sstrStr;
sstrStr>mat;//idea-1:生成编译错误

mat您是否实现了
操作符(std::istream&,double)
来获取您的值

它的酷之处在于,您可以像在标准库容器上一样迭代
cv::Mat
s。因此,如果您使用的是C++11,它可能如下所示(未经测试):


同样,我没有广泛使用OpenCV,因此请参阅文档以检查所有内容是否正确。

我非常确定旧的C接口可以为您提供使用CvMat加载/保存或读/写所需的内容,尽管该文档非常神秘
cv::Mat mat;
std::stringstream sstrStr;
sstrStr << getFoo() // something like this to get the saved matrix

sstrStr >> mat;  // idea-1: generate compile error
mat << sstrStr;  // idea-2: does also generate compile error
int size[2] = {x, y}; // matrix cols and rows
cv::Mat mat(3, size, CV_F64); // 3-dimensional matrix

for(auto& elem : mat)
{
    cv::Vec3d new_elem; // a 3D vector with double values
    // read your 3 doubles into new_elem
    // ...

    elem = new_elem; // assign the new value to the matrix element
}