opencv 读写图像文件
转自:https://www.cnblogs.com/haiyang21/p/9392399.html
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]; }
桂棹兮兰桨,击空明兮溯流光。