想了多种方法解决这个问题,还是百度的力量大。
在很多应用中,经常会直接把图片的二进制数据进行交换,比如说利用 socket 通信传送图片二进制数据,或者直接用内存数据库(例如 Redis)来传递图片二进制数据。
这个时候,当你的应用程序读到内存里的二进制图片数据时,怎么样直接转为 OpenCV 可以使用的图片格式呢,答案是用 cv::imdecode 这个函数。
即先构造一个 char 字符串序列的 vector,用来存储图片的二进制数据,然后再转为 cv::Mat 成为可以被 cv::imdecode 使用的数据格式,然后直接类型转换为 IplImage 数据格式。
同样,如果你需要把 IplImage 或 cv::Mat 压缩并写到一段内存块里时,就需要使用 cv::imencode 这个函数,使用方法类似。
具体的实现代码参考:
string fname = "D:/image.jpg"; //! 以二进制流方式读取图片到内存 FILE* pFile = fopen(fname.c_str(), "rb"); fseek(pFile, 0, SEEK_END); long lSize = ftell(pFile); rewind(pFile); char* pData = new char[lSize]; fread(pData, sizeof(char), lSize, pFile); fclose(pFile); //! 解码内存数据,变成cv::Mat数据 cv::Mat img_decode; vector<uchar> data; for (int i = 0; i < lSize; ++i){ data.push_back(pData[i]); } img_decode = cv::imdecode(data, CV_LOAD_IMAGE_COLOR); cv::flip(img_decode, img_decode, -1); img_decode.channels(); //! 将cv::Mat数据编码成数据流 vector<unsigned char> img_encode; cv::imencode(".jpg", img_decode, img_encode); unsigned char *encode_data = new unsigned char[lSize]; for (int i = 0; i<lSize; i++){ encode_data[i] = img_encode[i]; }如果需要解码的图像格式特殊,如unit16的深度图,请在调用imdecode时选择CV_LOAD_IMAGE_UNCHANGED
Specific color type of the loaded image: CV_LOAD_IMAGE_COLOR the loaded image is forced to be a 3-channel color image CV_LOAD_IMAGE_GRAYSCALE the loaded image is forced to be grayscale CV_LOAD_IMAGE_UNCHANGED the loaded image will be loaded as is.