[英]How to convert varbinary data (as string) to byte array?
I have a varbinary(BLOB) data as a string (image data) 我有一个varbinary(BLOB)数据作为字符串(图像数据)
for example, 例如,
std::string ByteStr = "FF-D8-E0-FF-85 ... " ;
I want to convert this string to byte array(or something usefull) then use as cv::Mat
format. 我想将此字符串转换为字节数组(或有用的东西),然后用作cv::Mat
格式。 I get the string from another method. 我从另一种方法得到字符串。 I tried to convert but i get OpenCV error. 我尝试转换,但出现OpenCV错误。
Error I get, 我明白了
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, >>file ........\\opencv\\modules\\highgui\\src\\window.cpp OpenCV错误:cv :: imshow中的断言失败(size.width> 0 && size.height> 0)>> file ........ \\ opencv \\ modules \\ highgui \\ src \\ window.cpp
C++ code, 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);
How can i convert this string to cv::Mat
format. 我如何将该字符串转换为cv::Mat
格式。 Any advice? 有什么建议吗?
Thanks in advance 提前致谢
cv::imdecode(vct,CV_BGR2GRAY );
doesn't make any sense. 没有任何意义。 Please use something meaningful like cv::imdecode(vct, cv2.IMREAD_GRAYSCALE );
请使用有意义的东西,例如cv::imdecode(vct, cv2.IMREAD_GRAYSCALE );
. 。
Also you need to convert the hex string to an integer type. 另外,您需要将十六进制字符串转换为整数类型。 You can use strtol for this. 您可以为此使用strtol 。
The code becomes something like: 该代码变为:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.