在影像處理時,常常會讀取影像資料(binary data)後轉成影像格式(cv::Mat),在進行影像操作。
那我們如何轉成可用的OpenCV影像格式呢?
首先讀取檔案後得到binary data,在進行解碼轉成cv::Mat。
當然如果單純讀檔後轉成OpenCV影像格式,OpenCV是有提供cv::imread這個API可以使用。
這裡會詳細解釋是有機會透過Web傳送影像或者存取資料庫時會用到。
編碼格式Decode Format:
- Windows bitmaps - *.bmp, *.dib (always supported)
- JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
- JPEG 2000 files - *.jp2 (see the Notes section)
- Portable Network Graphics - *.png (see the Notes section)
- Portable image format - *.pbm, *.pgm, *.ppm (always supported)
- Sun rasters - *.sr, *.ras (always supported)
- TIFF files - *.tiff, *.tif (see the Notes section)
OpenCV編碼參數:
- CV_LOAD_IMAGE_COLOR = Color
- CV_LOAD_IMAGE_GRAYSCALE = Gray Level
- CV_LOAD_IMAGE_ANYCOLOR = Source Color。
- CV_LOAD_IMAGE_ANYDEPTH = return 16-bit/32-bit image
C API
CvMat* buf = cvCreateMat(1, nBinaryDataLength, CV_8UC1);
buf->data.ptr = (unsigned char *)sBinaryData;
if(buf->data.ptr == NULL){
cvReleaseMat(&buf);
return NULL;
}
IplImage* image = cvDecodeImage(buf, CV_LOAD_IMAGE_ANYCOLOR);
cvReleaseMat(&buf);C++ API
std::vector< char > data(sBinaryData, nBinaryDataLength);
cv::Mat image = cv::imdecode( cv::Mat(data), CV_LOAD_IMAGE_ANYCOLOR );
data~vector();