Decode PNG from a buffer into ofImage with cv imdecode?

#1

Hi
I’m having a hard time decoding a PNG image i’ve got stored in a string - i’ve been round the houses several times, but still can’t get anywhere near having pixels : I’m grabbing images from an http server

The data’s base64 encoded, in a Json wrapper if i’ve understood whats going on

auto remoteImg = (ofHttpResponse(ofLoadURL("http://192.168.0.77:7777/data")));
string imgData_b64 = remoteImg.data;    

imgData_b64 = imgData_b64.substr(data.find(",")+1);      // find where the PNG header starts
imgData_b64 = imgData.substr(0, data.size()-2);              // strip off the end quotes and curly brackets

imgDecoded = (ofxIO::Base64Encoding::decode(data)); // works ok

int bufLen = imgDecoded.length();                                                           
char* imgBuf = new char [bufLen+1]; // get a plain array to pass into open cv
strcpy(imgBuf, b64_decoded.c_str());

cv::mat pngMat = cv::imdecode(*imgBuf, CV_8UC4); // decode the PNG string in to a cv::mat

ofxCv::toOf(pngMat, ofImage pngImage);      // convert the mat to an ofImage

From what i can tell, all’s well til the decoding in cv

cv::mat pngMat = cv::imdecode(*imgBuf, CV_8UC4)    // nothing happens

but nothing is ever allocated to the mat -
I;ve also tried

cv::mat pngMat = (cv::imdecode(cv::Mat(1, bufLen, CV_8UC4, *imgBuf), CV_LOAD_IMAGE_ANYDEPTH));

I feel like i’m missing something obvious here - the png is ok -i can paste the data into a website and get the image back out no problems…

Rich

#2

It’s ok. I’m actually an idiot - the cv::imdecode function isn’t in fact defined. I could have saved quite a bit of time if i’d looked in there sooner…

r