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;

}

OpenCV读写图像文件解析的更多相关文章

  1. OpenCV读写视频文件解析

    OpenCV读写视频文件解析 一.视频读写类 视频处理的是运动图像,而不是静止图像.视频资源可以是一个专用摄像机.网络摄像头.视频文件或图像文件序列. 在 OpenCV 中,VideoCapture ...

  2. OpenCV读写视频文件解析(二)

    OpenCV读写视频文件解析(二) VideoCapture::set 设置视频捕获中的属性. C++:bool VideoCapture::set(int propId, double value) ...

  3. 光流算法:关于OpenCV读写middlebury网站给定的光流的代码

    Middlebury是每个研究光流算法的人不可能不使用的网站,Middlebury提供了许多标准的测试库,这极大地推进了光流算法的进展.Middlebury提供的标准库,其计算出的光流保存在后缀名为. ...

  4. OpenCV源码解析

    OpenCV K-means源码解析 OpenCV 图片读取源码解析 OpenCV 视频播放源码解析 OpenCV 追踪算法源码解析 OpenCV SIFT算法源码解析 OpenCV 滤波源码分析:b ...

  5. 立体匹配:关于OpenCV读写middlebury网站的给定的视差并恢复三维场景的代码

    Middlebury是每个研究立体匹配算法的人不可能不使用的网站,Middlebury提供了许多标准的测试库,这极大地推进了立体匹配算法的进展.Middlebury提供的标准库,其计算出的视差保存在后 ...

  6. Hadoop学习总结之二:HDFS读写过程解析

    一.文件的打开 1.1.客户端 HDFS打开一个文件,需要在客户端调用DistributedFileSystem.open(Path f, int bufferSize),其实现为: public F ...

  7. Hadoop源码分析(1):HDFS读写过程解析

    一.文件的打开 1.1.客户端 HDFS打开一个文件,需要在客户端调用DistributedFileSystem.open(Path f, int bufferSize),其实现为: public F ...

  8. OpenCV ——IplImage应用解析

    由于OpenCV主要针对的是计算机视觉方面的处理,因此在函数库中,最重要的结构体是IplImage结构.IplImage结构来源于Intel的另外一个函数库Intel Image Processing ...

  9. Opencv读写文件

    HSV也是用和RGB差不多的方式来表达像素,每个整形(integer) 向量分别表示一个B,G,R通道,其他的色彩空间,也用同样的方式来表示像素,只是取值范围和通道数目不同HSV的色彩空间的色度值范围 ...

随机推荐

  1. 编译android4.4刷到nexus 5

    操作系统:ubuntu14.4tls android源码版本:4.4 手机:nexus5 1 获得手机的驱动程序(跟硬件平台有关):Binaries for Nexus Device 查到nexus ...

  2. LA2965侏罗纪(异或和为0的最大数字个数)

    题意:       给你n个字符串,让你在里面找到一个字符串集合使得这些字符串中所有的字母出现的次数和为偶数,输出集合的最大个数,和ASCII最小的解. 思路:       考虑到每个字符串中所有的字 ...

  3. hdu 5063 不错的小想法题(逆向处理操作)

    题意:       刚开始的时候给你一个序列,长度为n,分别为a[1]=1,a[2]=2,a[3]=3,a[4]=4...a[n]=n,然后有4种操作如下: Type1: O 1 call fun1( ...

  4. hdu4941 map交换行列

    题意:      有一个大矩阵,某些格子上有数字,然后有三种操作, 1 交换行 2 交换列 3 询问当前坐标数值 思路:      直接用map去映射行列,用二维的map去存数字就行了,水题,想不通的 ...

  5. UVA10341解方程(二分)

    题意:       给你一个方程 F[x] = pe^-x + qsin(x) + rcos(x) + stan(x) + tx^2 + u = 0(0<=p,r<=20,-20<= ...

  6. XCTF-i-get-id-200

    i-get-id-200 题目描述 嗯..我刚建好了一个网站 解题过程 一共有三个页面 Hello World 告诉了页面是perl写的 Forms 输入name和age会返回渲染后的字符串 搜了一下 ...

  7. Wampserver-删除虚拟主机

    对hosts操作 到目录C:\Windows\System32\drivers\etc中修改hosts 比如你想删除iwh2.com 选中这2行进行删除,保存退出 对httpd-vhosts操作 到目 ...

  8. [bug] eclipse 点击new菜单后没有新建java project或class选项

    参考 https://blog.csdn.net/u013001763/article/details/54405758

  9. Spring Cloud(Dalston.SR1)

    Spring Cloud 示例项目地址:https://github.com/Yanshaoshuai/microservicecloud Eureka 集群搭建 microservicecloud- ...

  10. 自动做bond的脚本

    #!/bin/bash # 先备份原来的网卡配置文件 find /etc/sysconfig/network-scripts/ -type f -name "ifcfg*"|xar ...