Linux系統做音影片開發,很多時候要用到opencv,就需要把圖片Buffer資料轉換成Mat物件來使用
Buffer 轉 Mat
Mat Buffer2Mat(unsigned char* buffer, int len) { vector<unsigned char> img_data(buffer, buffer+len); Mat img = imdecode(img_data, IMREAD_ANYCOLOR); return img; }
Mat 轉 Buffer
Mat src = imread("image.bmp"); std::vector<unsigned char> src_data; cv::imencode(".jpg", src, src_data);
unsigned char* buffer = src_data.data(); int len = src_data.size();