zoukankan      html  css  js  c++  java
  • 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 ", ex.what());

            return 1;

        }

     

        fprintf(stdout, "Saved PNG file with alpha data. ");

        return 0;

    }

  • 相关阅读:
    转载:渗透利器-余弦
    搜索引擎?
    Gartner:用自适应安全架构来应对高级定向攻击
    内网渗透测试思路-FREEBUF
    渗透测试常规思路分析-FREEBUF
    SQLMAP使用笔记
    如何打造一款优秀的产品管理系统?
    阿里的钉钉能干掉腾讯的微信么?
    下一个亿万市场:企业级SaaS服务谁能独领风骚
    如何注册iClap账号?
  • 原文地址:https://www.cnblogs.com/wujianming-110117/p/13140036.html
Copyright © 2011-2022 走看看