我有一个 varbinary(BLOB) 数据作为字符串(图像数据)
例如,
std::string ByteStr = "FF-D8-E0-FF-85 ... " ;
我想将此字符串转换为字节数组(或有用的东西),然后用作 cv::Mat
格式。我从另一种方法获取字符串。我尝试转换,但出现 OpenCV 错误。
我得到的错误,
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, >>file ........\opencv\modules\highgui\src\window.cpp
C++代码,
std::string ByteStr = obj->GetBinaryStr(); // this provide varbinary string
std::vector<uchar> vct;
std::string delimiter = "-";
size_t pos = 0;
std::string token;
while ((pos = ByteStr.find(delimiter)) != std::string::npos) {
token = ByteStr.substr(0, pos);
vct.push_back((uchar)(token.c_str()));
ByteStr.erase(0, pos + delimiter.length());
}
cv::Mat img = cv::imdecode(vct,CV_BGR2GRAY );
cv::namedWindow("MyWindow");
cv::imshow("MyWindow",img);
如何将此字符串转换为 cv::Mat
格式。有什么建议吗?
提前致谢
最佳答案
cv::imdecode(vct,CV_BGR2GRAY );
没有任何意义。请使用一些有意义的东西,比如 cv::imdecode(vct, cv2.IMREAD_GRAYSCALE );
。
您还需要将十六进制字符串转换为整数类型。您可以使用 strtol为此。
代码变成这样:
std::string ByteStr = obj->GetBinaryStr(); // this provide varbinary string
std::vector<uchar> vct;
std::string delimiter = "-";
size_t pos = 0;
std::string token;
while ((pos = ByteStr.find(delimiter)) != std::string::npos) {
token = ByteStr.substr(0, pos);
uchar ucharToken = uchar(strtol(token.c_str(), NULL, 16));
vct.push_back(ucharToken);
ByteStr.erase(0, pos + delimiter.length());
}
cv::Mat img = cv::imdecode(vct, cv::IMREAD_GRAYSCALE);
cv::namedWindow("MyWindow");
cv::imshow("MyWindow",img);
关于c++ - 如何将 varbinary 数据(作为字符串)转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107726/