Opencv 图像读取与保存问题

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

1 图像读取

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

c 复制代码
// 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版本号不断更新, Opencv的C++版本号数据结构和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源代码枚举类型中也能够看到上述标识符含义:

cpp 复制代码
// 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读取结果是有差异的:

c 复制代码
// 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 复制代码
// 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标识符值相应的兴许參数设置:
c 复制代码
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的编码方式进行了特别声明:

c 复制代码
// 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中:

PNG原图

c 复制代码
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 G R A

通道分离保存结果

附上 Opencv文档源代码:

c 复制代码
#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;
}

执行结果为:

相关推荐
2501_9419820514 分钟前
结合 AI 视觉:使用 OCR 识别企业微信聊天记录中的图片信息
人工智能·ocr·企业微信
事变天下30 分钟前
肾尚科技完成新一轮融资,加速慢性肾脏病(CKD)精准化管理闭环渗透
大数据·人工智能
GEO AI搜索优化助手31 分钟前
范式革命——从“关键词”到“意图理解”,搜索本质的演进与重构
人工智能·搜索引擎·生成式引擎优化·ai优化·geo搜索优化
大刘讲IT33 分钟前
2025年企业级 AI Agent 标准化落地深度年度总结:从“对话”到“端到端价值闭环”的范式重构
大数据·人工智能·程序人生·ai·重构·制造
一个没有感情的程序猿40 分钟前
前端实现人体骨架检测与姿态对比:基于 MediaPipe 的完整方案
机器学习·计算机视觉·前端框架·开源
2301_8234380241 分钟前
【无标题】解析《采用非对称自玩实现强健多机器人群集的深度强化学习方法》
数据库·人工智能·算法
沛沛老爹42 分钟前
Web开发者快速上手AI Agent:提示词应用优化实战
人工智能·ai·agent·提示词·rag·入门知识
中国胖子风清扬44 分钟前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain
Dev7z1 小时前
基于Stanley算法的自动驾驶车辆路径跟踪控制研究
人工智能·机器学习·自动驾驶
Java后端的Ai之路1 小时前
【分析式AI】-过拟合(含生活案例说明)
人工智能·aigc·生活·过拟合·分析式ai