OpenCV读写图像文件解析
OpenCV讀寫圖像文件解析
imdecode
從內存中的緩沖區讀取圖像。
C++: Mat imdecode(InputArray buf, int flags)
C++: Mat imdecode(InputArray buf, int flags, Mat* dst)
C: IplImage* cvDecodeImage(const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR)
C: CvMat* cvDecodeImageM(const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR)
Python: cv2.imdecode(buf, flags) → retval
Parameters:
· buf – Input array or vector of bytes.
· flags – The same flags as in imread() .
· dst – The optional output placeholder for the decoded matrix. It can save the image reallocations when the function is called repeatedly for images of the same size.
參數:
buf–輸入字節數組或向量。
flags–與imread()中的標志相同。
dst–解碼矩陣的可選輸出占位符。當對相同大小的圖像重復調用該函數時,它可以保存圖像重新分配。
函數從內存中指定的緩沖區讀取圖像。如果緩沖區太短或包含無效數據,則返回空矩陣/圖像。
有關支持的格式和標志說明的列表,請參見imread()。
注:在彩色圖像中,解碼圖像將以B G R順序存儲信道。
Imencode
將圖像編碼到內存緩沖區中。
C++: bool imencode(const string& ext, InputArray img, vector& buf, const vector& params=vector())
C: CvMat* cvEncodeImage(const char* ext, const CvArr* image, const int* params=0 )
Python: cv2.imencode(ext, img[, params]) → retval, buf
Parameters:
· ext – File extension that defines the output format.
· img – Image to be written.
· buf – Output buffer resized to fit the compressed image.
· params – Format-specific parameters. See imwrite()
參數:
ext–定義輸出格式的文件擴展名。
img–待寫圖像。
buf–調整輸出緩沖區大小以適應壓縮圖像。
params–格式化特定參數。請參見imwrite()。
函數壓縮圖像并將其存儲在內存緩沖區中,該緩沖區的大小將根據結果調整。有關支持的格式和標志說明的列表,請參見imwrite()。
注意:cvEncodeImage返回CV_8UC1類型的單行矩陣,其中包含作為字節數組的編碼圖像。
Imread
從文件加載圖像。
C++: Mat imread(const string& filename, int flags=1 )
Python: cv2.imread(filename[, flags]) → retval
C: IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
C: CvMat* cvLoadImageM(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
Python: cv.LoadImage(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Python: cv.LoadImageM(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Parameters:
· filename – Name of file to be loaded.
· flags – Flags specifying the color type of loaded image
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit
image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).
參數:
?file Name–要加載的文件的名稱。
?flags–指定加載圖像顏色類型的標志:
CV_LOAD_IMAGE_ANYDEPTH-如果已設置,則在輸入具有相應深度時返回16位/32位圖像,否則將其轉換為8位。 CV_LOAD_IMAGE_COLOR-如果設置,請始終將圖像轉換為彩色圖像
CV_LOAD_IMAGE_GRAYSCALE-如果設置,始終將圖像轉換為灰度圖像
0返回3通道彩色圖像。
注意,在當前實現中,alpha通道(如果有)從輸出圖像中剝離。如果需要alpha通道,請使用負值。
=0返回灰度圖像。
<0按原樣返回加載的圖像(使用alpha通道)。
函數imread從指定文件加載圖像并返回它。如果無法讀取圖像(由于缺少文件、權限不正確、格式不受支持或無效),函數將返回空矩陣(Mat::data==NULL)。目前,支持以下文件格式:
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)
注意
函數根據內容而不是文件擴展名來確定圖像的類型。
在Microsoft WindowsOS和MacOSX上,默認情況下會使用OpenCV圖像(libjpeg、libpng、libtiff和libjasper)附帶的編解碼器。因此,OpenCV始終可以讀取jpeg、png和tiff。在MacOSX上,還可以選擇使用本機MacOSX圖像讀取器。但請注意,由于MacOSX中嵌入了顏色管理,目前這些本地圖像加載程序提供的圖像具有不同的像素值。
在Linux*、BSD風格和其他類似Unix的開源操作系統上,OpenCV查找隨OS映像提供的編解碼器。安裝相關軟件包(不要忘記開發文件,例如Debian和Ubuntu中的“libjpeg dev”)以獲得編解碼器支持,或者在CMake中打開OPENCV_BUILD_3RDPARTY_LIBS標志。
注:在彩色圖像的情況下,解碼圖像將以B G R順序存儲信道。
Imwrite
將圖像保存到指定文件。
C++: bool imwrite(const string& filename, InputArray img, const vector& params=vector() )
Python: cv2.imwrite(filename, img[, params]) → retval
C: int cvSaveImage(const char* filename, const CvArr* image, const int* params=0 )
Python: cv.SaveImage(filename, image) → None
Parameters:
· filename – Name of the file.
· image – Image to be saved.
· params –
Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, … . The following
parameters are currently supported:
For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.
For PNG, it can be the compression level ( CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9.
A higher value means a smaller size and longer compression time. Default value is 3.
or PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1.
Default value is 1.
參數
?file Name–文件名。
?image–要保存的圖像。
?params-格式特定的存儲參數編碼為對paramId_1、paramValue_1、paramId_2、paramValue_2。當前支持以下參數:
對于JPEG,它可以是0到100之間的質量(CV_IMWRITE_JPEG_quality)(越高越好)。默認值為95。
對于PNG,它可以是從0到9的壓縮級別(CV_IMWRITE_PNG_compression)。較高的值意味著較小的大小和較長的壓縮時間。默認值為3。
對于PPM、PGM或PBM,它可以是二進制格式標志(CV_IMWRITE_PXM_binary),0或1。默認值為1。
imwrite函數將圖像保存到指定的文件中。
根據文件擴展名選擇圖像格式(有關擴展名列表,請參見imread())。使用此功能只能保存8位(或16位無符號(CV_16U)(對于PNG、JPEG 2000和TIFF)單通道或3通道(具有“BGR”通道順序)圖像。如果格式、深度或通道順序不同,請在保存之前使用Mat::convertor()和cvtColor()進行轉換。或者,使用通用文件存儲I/O函數將圖像保存為XML或YAML格式。
使用此函數可以使用alpha通道存儲PNG圖像。
為此,創建8位(或16位)4通道圖像BGRA,其中alpha通道位于最后。完全透明像素的alpha設置為0,完全不透明像素的alpha設置為255/65535。下面的示例演示如何創建這樣的BGRA圖像并存儲到PNG文件。它還演示了如何設置自定義壓縮參數。
#include
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void createAlphaMat(Mat &mat)
{
CV_Assert(mat.channels() == 4);
for (int i = 0; i < mat.rows; ++i) {
for (int j = 0; j < mat.cols; ++j) {
Vec4b& bgra = mat.at(i, j);
bgra[0] = UCHAR_MAX; // Blue
bgra[1] = saturate_cast((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
bgra[2] = saturate_cast((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
bgra[3] = saturate_cast(0.5 * (bgra[1] + bgra[2])); // Alpha
}
}
}
int main(int argv, char **argc)
{
// Create mat with alpha channel
Mat mat(480, 640, CV_8UC4);
createAlphaMat(mat);
vector compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
try {
imwrite(“alpha.png”, mat, compression_params);
}
catch (runtime_error& ex) {
fprintf(stderr, “Exception
converting image to PNG format: %s\n”, ex.what());
return 1;
}
fprintf(stdout, “Saved PNG
file with alpha data.\n”);
return 0;
}
總結
以上是生活随笔為你收集整理的OpenCV读写图像文件解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV读写视频文件解析
- 下一篇: OpenCV读写视频文件解析(二)