#include<opencv2/opencv.hpp> #include<iostream> #include <vector> #include "opencv2/imgcodecs/legacy/constants_c.h" int main(int argc, char** argv) { FILE* fp; errno_t err=fopen_s(&fp, "D:/bb/tu/sansui.jpg", "rb");//以二进制流方式读取图片到内存 if (err != 0) { std::cerr << "cuowu" << std::endl;//提示文件打开错误 } fseek(fp, 0, SEEK_END); long lSize = ftell(fp); //返回指针位置 rewind(fp);//设置读写指针到文件的开头 char* pData = new char[lSize]; fread(pData, sizeof(char), lSize, fp);//读取数据 fclose(fp); cv::Mat img_decode; std::vector<uchar> data; for (int i = 0; i < lSize; ++i) { data.push_back(pData[i]); } std::cerr << data.size() << std::endl; img_decode = cv::imdecode(data, CV_LOAD_IMAGE_COLOR);//把vector转换成Mat //需要 #include "opencv2/imgcodecs/legacy/constants_c.h" /* 参数2: CV_LOAD_IMAGE_UNCHANGED (<0),以原始图像读取(包括alpha通道), CV_LOAD_IMAGE_GRAYSCALE ( 0),以灰度图像读取 CV_LOAD_IMAGE_COLOR (>0),以BGR格式读取 */ std::cerr << lSize << std::endl; cv::waitKey(0); return 0; }