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<uchar>& buf, const vector<int>& params=vector<int>())

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 a loaded image:

o   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.

o   CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one

o   CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

o   >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 Windows*OS和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<int>& params=vector<int>() )

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:

o   For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.

o   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.

o   For 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 <vector>

#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<Vec4b>(i, j);

            bgra[0] = UCHAR_MAX; // Blue

            bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green

            bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red

            bgra[3] = saturate_cast<uchar>(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<int> 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;

}

 


免責聲明!

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



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