Reputation: 1093
I have a varbinary(BLOB) data as a string (image data)
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. I get the string from another method. I tried to convert but i get OpenCV error.
Error I get,
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, >>file ........\opencv\modules\highgui\src\window.cpp
C++ code,
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. Any advice?
Thanks in advance
Upvotes: 0
Views: 495
Reputation: 41765
cv::imdecode(vct,CV_BGR2GRAY );
doesn't make any sense. Please use something meaningful like cv::imdecode(vct, cv2.IMREAD_GRAYSCALE );
.
Also you need to convert the hex string to an integer type. You can use strtol for this.
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);
Upvotes: 2