OpenCV读写图像文件解析
OpenCV讀寫圖像文件解析
imdecode
從內(nèi)存中的緩沖區(qū)讀取圖像。
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.
參數(shù):
buf–輸入字節(jié)數(shù)組或向量。
flags–與imread()中的標(biāo)志相同。
dst–解碼矩陣的可選輸出占位符。當(dāng)對相同大小的圖像重復(fù)調(diào)用該函數(shù)時(shí),它可以保存圖像重新分配。
函數(shù)從內(nèi)存中指定的緩沖區(qū)讀取圖像。如果緩沖區(qū)太短或包含無效數(shù)據(jù),則返回空矩陣/圖像。
有關(guān)支持的格式和標(biāo)志說明的列表,請參見imread()。
注:在彩色圖像中,解碼圖像將以B G R順序存儲信道。
Imencode
將圖像編碼到內(nèi)存緩沖區(qū)中。
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()
參數(shù):
ext–定義輸出格式的文件擴(kuò)展名。
img–待寫圖像。
buf–調(diào)整輸出緩沖區(qū)大小以適應(yīng)壓縮圖像。
params–格式化特定參數(shù)。請參見imwrite()。
函數(shù)壓縮圖像并將其存儲在內(nèi)存緩沖區(qū)中,該緩沖區(qū)的大小將根據(jù)結(jié)果調(diào)整。有關(guān)支持的格式和標(biāo)志說明的列表,請參見imwrite()。
注意:cvEncodeImage返回CV_8UC1類型的單行矩陣,其中包含作為字節(jié)數(shù)組的編碼圖像。
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).
參數(shù):
?file Name–要加載的文件的名稱。
?flags–指定加載圖像顏色類型的標(biāo)志:
CV_LOAD_IMAGE_ANYDEPTH-如果已設(shè)置,則在輸入具有相應(yīng)深度時(shí)返回16位/32位圖像,否則將其轉(zhuǎn)換為8位。 CV_LOAD_IMAGE_COLOR-如果設(shè)置,請始終將圖像轉(zhuǎn)換為彩色圖像
CV_LOAD_IMAGE_GRAYSCALE-如果設(shè)置,始終將圖像轉(zhuǎn)換為灰度圖像
0返回3通道彩色圖像。
注意,在當(dāng)前實(shí)現(xiàn)中,alpha通道(如果有)從輸出圖像中剝離。如果需要alpha通道,請使用負(fù)值。
=0返回灰度圖像。
<0按原樣返回加載的圖像(使用alpha通道)。
函數(shù)imread從指定文件加載圖像并返回它。如果無法讀取圖像(由于缺少文件、權(quán)限不正確、格式不受支持或無效),函數(shù)將返回空矩陣(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)
注意
函數(shù)根據(jù)內(nèi)容而不是文件擴(kuò)展名來確定圖像的類型。
在Microsoft WindowsOS和MacOSX上,默認(rèn)情況下會使用OpenCV圖像(libjpeg、libpng、libtiff和libjasper)附帶的編解碼器。因此,OpenCV始終可以讀取jpeg、png和tiff。在MacOSX上,還可以選擇使用本機(jī)MacOSX圖像讀取器。但請注意,由于MacOSX中嵌入了顏色管理,目前這些本地圖像加載程序提供的圖像具有不同的像素值。
在Linux*、BSD風(fēng)格和其他類似Unix的開源操作系統(tǒng)上,OpenCV查找隨OS映像提供的編解碼器。安裝相關(guān)軟件包(不要忘記開發(fā)文件,例如Debian和Ubuntu中的“l(fā)ibjpeg dev”)以獲得編解碼器支持,或者在CMake中打開OPENCV_BUILD_3RDPARTY_LIBS標(biāo)志。
注:在彩色圖像的情況下,解碼圖像將以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.
參數(shù)
?file Name–文件名。
?image–要保存的圖像。
?params-格式特定的存儲參數(shù)編碼為對paramId_1、paramValue_1、paramId_2、paramValue_2。當(dāng)前支持以下參數(shù):
對于JPEG,它可以是0到100之間的質(zhì)量(CV_IMWRITE_JPEG_quality)(越高越好)。默認(rèn)值為95。
對于PNG,它可以是從0到9的壓縮級別(CV_IMWRITE_PNG_compression)。較高的值意味著較小的大小和較長的壓縮時(shí)間。默認(rèn)值為3。
對于PPM、PGM或PBM,它可以是二進(jìn)制格式標(biāo)志(CV_IMWRITE_PXM_binary),0或1。默認(rèn)值為1。
imwrite函數(shù)將圖像保存到指定的文件中。
根據(jù)文件擴(kuò)展名選擇圖像格式(有關(guān)擴(kuò)展名列表,請參見imread())。使用此功能只能保存8位(或16位無符號(CV_16U)(對于PNG、JPEG 2000和TIFF)單通道或3通道(具有“BGR”通道順序)圖像。如果格式、深度或通道順序不同,請?jiān)诒4嬷笆褂肕at::convertor()和cvtColor()進(jìn)行轉(zhuǎn)換。或者,使用通用文件存儲I/O函數(shù)將圖像保存為XML或YAML格式。
使用此函數(shù)可以使用alpha通道存儲PNG圖像。
為此,創(chuàng)建8位(或16位)4通道圖像BGRA,其中alpha通道位于最后。完全透明像素的alpha設(shè)置為0,完全不透明像素的alpha設(shè)置為255/65535。下面的示例演示如何創(chuàng)建這樣的BGRA圖像并存儲到PNG文件。它還演示了如何設(shè)置自定義壓縮參數(shù)。
#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;
}
總結(jié)
以上是生活随笔為你收集整理的OpenCV读写图像文件解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV读写视频文件解析
- 下一篇: OpenCV读写视频文件解析(二)