opencv 將內存中圖片讀入到mat矩陣


opencv 將內存中圖片讀入到mat矩陣 - 雲+社區 - 騰訊雲 (tencent.com)

 

 

平常用的比較多的是 imread函數,直接將一個.jpg或者.bmp或者其他格式圖片文件,讀入到mat矩陣中。

本博文記錄的是,如何將一段內存,或者文件流,讀入到mat矩陣中。

有兩個例子,相信看了之后,應該知道該怎么做了。

開發環境 opencv2413+vs2013

1、mat與文件流相互轉換

Mat src = imread("1.jpg"); vector<uchar> buff;//buffer for coding vector<int> param = vector<int>(2); param[0] = CV_IMWRITE_JPEG_QUALITY; param[1] =95;//default(95) 0-100 imencode(".jpg", src, buff, param); cout << "coded file size(jpg)" << buff.size() << endl;//fit buff size automatically. Mat jpegimage = imdecode(Mat(buff), CV_LOAD_IMAGE_COLOR);

2、將圖片文件讀入到文件流,再解析成mat矩陣

std::ifstream file("1.jpg", std::ios::binary); std::vector<char> data; file >> std::noskipws; std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data)); Mat jpegimage = imdecode(Mat(data), CV_LOAD_IMAGE_COLOR); file.close();

據說,imread函數實際就是如此步驟。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM