• Opencv 图像读取与保存问题



    本系列文章由 @yhl_leo 出品,转载请注明出处。


    文章链接: http://blog.csdn.net/yhl_leo/article/details/49737357


    本文仅对 Opencv图像读取与保存进行阐述,重在探讨图像读取与保存过程中应注意的细节问题。

    1 图像读取

    首先看一下,imread函数的声明:

    // C++: Mat based
    Mat imread(const string& filename, int flags=1 );
    
    // C: IplImage based
    IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR );
    
    // C: CvMat based
    CvMat* cvLoadImageM(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR );

    此处,就不列出python的函数声明。随着2.x和3.x版本号不断更新, OpencvC++版本号数据结构和C版本号有较大差异,前者降低了指针的大量使用。使用方法更加便捷,因此建议多使用前者。以C++版本号函数进行分析,形參列表包含:

    • filename : 待载入图像(包含:文件路径和文件名称。图像在project默认路径下的可省略文件路径);
    • flags : 标志符,指定图像载入颜色类型。默认值为1:

      • IMREAD_UNCHANGED / CV_LOAD_IMAGE_UNCHANGED :不加改变的载入原图。
      • IMREAD_GRAYSCALE / CV_LOAD_IMAGE_GRAYSCALE :图像转为灰度图(GRAY,1通道)。

      • IMREAD_COLOR / CV_LOAD_IMAGE_COLOR :图像转为彩色图(BGR,3通道)。

      • IMREAD_ANYDEPTH / CV_LOAD_IMAGE_ANYDEPTH :不论什么位深度。假设载入的图像不是16-bit位图或者32-bit位图。则转化为8-bit位图。
      • IMREAD_ANYCOLOR / CV_LOAD_IMAGE_ANYCOLOR :不论什么彩色。单独使用的时候等价于 IMREAD_UNCHANGED / CV_LOAD_IMAGE_UNCHANGED
      • > 0 :返回3通道的彩色图,可是假设是4通道(RGBA)。当中Alpha须要保留的话,不建议这么使用。由于一旦这么使用。就会导致Alpha通道被剥离掉,此时建议使用负值。

      • = 0 :返回灰度图像。
      • < 0 :返回具有Alpha通道的图像。

    假设你喜欢使用imread("file.jpg")缺省參数的形式载入图像。务必要留意你所载入后的图像可能已经不是你原本想要的图像了。

    Opencv源代码枚举类型中也能够看到上述标识符含义:

    // highgui.hpp
    enum
    {
        // 8bit, color or not
        IMREAD_UNCHANGED  =-1,
        // 8bit, gray
        IMREAD_GRAYSCALE  =0,
        // ?, color
        IMREAD_COLOR      =1,
        // any depth, ?
        IMREAD_ANYDEPTH   =2,
        // ?, any color
        IMREAD_ANYCOLOR   =4
    };
    
    // highui_c.h
    enum
    {
    /* 8bit, color or not */
        CV_LOAD_IMAGE_UNCHANGED  =-1,
    /* 8bit, gray */
        CV_LOAD_IMAGE_GRAYSCALE  =0,
    /* ?

    , color */ CV_LOAD_IMAGE_COLOR =1, /* any depth, ? */ CV_LOAD_IMAGE_ANYDEPTH =2, /* ?, any color */ CV_LOAD_IMAGE_ANYCOLOR =4 };

    Opencv已经支持眼下非常多图像格式,可是并不是所有。

    主要包含:

    • Windows bitmaps    ->    *.bmp, *.dib (always supported)
    • JPEG files    ->    *.jpeg, *.jpg, *.jpe (see the Notes section)
    • JPEG 2000 files    ->    *.jp2,*.jpf,*.jpx (see the Notes section)
    • Portable Network Graphics    ->    *.png (see the Notes section)
    • WebP    ->    *.webp (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)

      Notes

      • 1 The function determines the type of an image by the content, not by the file extension.
      • 2 On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
      • 3 On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, “libjpeg-dev”, in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
      • 4 In the case of color images, the decoded images will have the channels stored in B G R order.

    对于常见的支持4通道的图像格式来说, Opencv读取结果是有差异的:

    // 1.tif, 1.jp2 and 1.png are color images with 4 channels: R, G, B, A
    cv::Mat imageTif = cv::imread("E:\1.tif"); // the default flags is 1
    cv::Mat imageJp2 = cv::imread("E:\1.jp2"); // the default flags is 1
    cv::Mat imagePng = cv::imread("E:\1.png"); // the default flags is 1
    std::cout << imageTif.channels() << std::endl; // prints 3
    std::cout << imageJp2.channels() << std::endl; // prints 3
    std::cout << imagePng.channels() << std::endl; // prints 3
    
    cv::Mat imageTif2 = cv::imread("E:\1.tif", -1); // flags = -1
    cv::Mat imageJp22 = cv::imread("E:\1.jp2", -1);
    cv::Mat imagePng2 = cv::imread("E:\1.png", -1);
    std::cout << imageTif2.channels() << std::endl; // prints 3
    std::cout << imageJp22.channels() << std::endl; // prints 3
    std::cout << imagePng2.channels() << std::endl; // prints 4

    由此可见,眼下 Opencv能够直接读取4通道图像并保留Alpha通道的貌似仅仅有PNG格式,对于非PNG格式数据,须要保留Alpha通道的应用,假设坚持使用 Opencv库,建议转格式吧~

    2 图像存储

    首先来看,imwrite函数的声明:

    // c++: Mat based
    bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() );
    
    // C: CvMat and IplImage based
    int cvSaveImage(const char* filename, const CvArr* image, const int* params=0 );

    仍旧以C++版本号为例。其形參列表为:

    • filename:待保存图像名(包含:文件路径和文件名称,图像在project默认路径下的可省略文件路径);
    • img:待保存的图像对象。
    • params :特定图像存储编码參数设置。以相似pairs类型的方式。(paramId_1, paramValue_1)(paramId_2, paramValue_2),当中paramId_1就是标志符值。paramValue_1标识符值相应的兴许參数设置:
    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); // paramId_1, png compression
    compression_params.push_back(9);                          // paramValue_2, compression level is 9 

    Opencv中。主要对JPEG,PNG和PXM的编码方式进行了特别声明:

    // highgui.hpp
    enum
    {
        IMWRITE_JPEG_QUALITY =1,         // quality from 0 to 100, default value is 95. (The higher is the better)
        IMWRITE_PNG_COMPRESSION =16,     // compression level from 0 to 9, default value is 3. (A higher value means a smaller size and longer compression time. Default value is 3.)
        IMWRITE_PNG_STRATEGY =17,
        IMWRITE_PNG_BILEVEL =18,
        IMWRITE_PNG_STRATEGY_DEFAULT =0,
        IMWRITE_PNG_STRATEGY_FILTERED =1,
        IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2,
        IMWRITE_PNG_STRATEGY_RLE =3,
        IMWRITE_PNG_STRATEGY_FIXED =4,
        IMWRITE_PXM_BINARY =32          // binary format flag: 0 or 1, default value is 1.
    };
    
    // highui_c.h
    enum
    {
        CV_IMWRITE_JPEG_QUALITY =1,
        CV_IMWRITE_PNG_COMPRESSION =16,
        CV_IMWRITE_PNG_STRATEGY =17,
        CV_IMWRITE_PNG_BILEVEL =18,
        CV_IMWRITE_PNG_STRATEGY_DEFAULT =0,
        CV_IMWRITE_PNG_STRATEGY_FILTERED =1,
        CV_IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2,
        CV_IMWRITE_PNG_STRATEGY_RLE =3,
        CV_IMWRITE_PNG_STRATEGY_FIXED =4,
        CV_IMWRITE_PXM_BINARY =32
    };

    上述的标识符含义,显而易见,就不累述。

    值得强调的是,imwrite函数支持存储的图像类型是有限的仅仅包含:1。3,4通道的图像,可是对于不同的图像格式。也是有差异的:

    • 对于单通道8-bit位图(或者16-bit位图( CV_16U/CV_16UC1 的PNG,JPEG 2000 和TIFF))或者3通道(通道顺序为:B G R )的图像,imwrite函数是都支持的。

      对于格式,或者位深或者通道顺序与上面不一致的。能够使用函数Mat::convertTo()cvtColor()函数进行转换后,再保存。当然,也能够使用通用的方法利用FileStorageI/O操作。将图像存为XML或YAML格式

    • 对于PNG图像,能够保存其Alpha通道,创建一个8-bit或者16-bit 4通道的位图(通道顺序为:B G R A )。假设是全透明的Alpha通道设置为0,反之不透明设置为255/65535。

    对于多通道图像,假设想对其每一个通道单独进行保存,当然也是可行的。一方面自己能够依据图像的信息和图层信息写出相应的存储函数,还有一方面 Opencv也提供了专门的函数split能够将图像的每一个通道提取出保存到vector中:


    Panda
    PNG原图

    cv::Mat img = imread( "C:\Users\Leo\Desktop\Panda.png", CV_LOAD_IMAGE_UNCHANGED );
    
    std::vector<cv::Mat> imageChannels;
    cv::split( img, imageChannels );
    cv::imwrite("E:\0.jpg", imageChannels[0]);
    cv::imwrite("E:\1.jpg", imageChannels[1]);
    cv::imwrite("E:\2.jpg", imageChannels[2]);
    cv::imwrite("E:\3.jpg", imageChannels[3]);


    B Panda0 G Panda1 R Panda2 A Panda3
    通道分离保存结果

    附上 Opencv文档源代码:

    #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;
    }

    执行结果为:

  • 相关阅读:
    D django 用户认证系统
    vim 跳到指定行
    django 的auth.authenticate返回为None
    git fetch 的简单用法:更新远程代码到本地仓库
    sql语句查询出表里符合条件的第二条记录的方法
    你一定喜欢看的 Webpack 2.× 入门实战
    webpack 从入门到工程实践
    入门Webpack,看这篇就够了
    教程
    常用浏览器如何设置代理服务器上网(图文教程)
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7253722.html
Copyright © 2020-2023  润新知