OpenCV-Mat笔记

Mat

  • https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5
    Mat类型是opencv2.0后的类型,使用此类型无需进行内存管理.Mat包含2个数据部分:矩阵和矩阵头。矩阵头包含matrix大小,存储方法,matrix存储地址等,矩阵头的内存大小固定.
    OpenCV使用的是引用计数系统:每个Mat对象都有着自己的header,但matrix可在两个实例中通过指向同一个matrix首地址的指针来共享数据。拷贝构造和赋值均只拷贝其header和指向matrix的指针,而非像素数据的本身。当拷贝一个Mat对象的header时,关于matrix的计数器数值增加,一旦header被清理了,counter减少,当counter减少到0的时候,matrix的内存空间会被释放。
    可创建关于全部数据的子部分的header。例如,可通过创建一个有着新边界的header来创建ROI(感兴趣区域)。

 

数据类型

+--------+----+----+----+----+------+------+------+------+
|        | C1 | C2 | C3 | C4 | C(5) | C(6) | C(7) | C(8) |
+--------+----+----+----+----+------+------+------+------+
| CV_8U  |  0 |  8 | 16 | 24 |   32 |   40 |   48 |   56 |
| CV_8S  |  1 |  9 | 17 | 25 |   33 |   41 |   49 |   57 |
| CV_16U |  2 | 10 | 18 | 26 |   34 |   42 |   50 |   58 |
| CV_16S |  3 | 11 | 19 | 27 |   35 |   43 |   51 |   59 |
| CV_32S |  4 | 12 | 20 | 28 |   36 |   44 |   52 |   60 |
| CV_32F |  5 | 13 | 21 | 29 |   37 |   45 |   53 |   61 |
| CV_64F |  6 | 14 | 22 | 30 |   38 |   46 |   54 |   62 |
+--------+----+----+----+----+------+------+------+------+
注:
U表示Unsigned,即无符号整数;
S表示Short,即整数;
F表示浮点数;
C表示通道数。

各数据类型的详细情况:

种类 索引 深度 数据范围 C++数据类型
CV_8U 0 8bits 0~255 unsigned char
CV_8S 1 8bits -128~127 char
CV_16U 2 16bits 0~65535 ushort,unsigned short int,unsigned short
CV_16S 3 16bits -32768~32767 short,short int
CV32S 4 32bits -2147483648~2147483647 int,long
CV32F 5 32bits 1.18e-38~3.40e38 float
CV_64F 6 64bits 2.23e-308~1.79e308 double
CV_USRTYPE1 7      

使用at方式进行访问,在at的时候需要指定的数据类型对照表如下:

种类 C1 C2 C3 C4 C6
uchar8U uchar cv::Vec2b cv::Vec3b cv::Vec4b  
char8S          
ushort16U          
short16S short cv::Vec2s cv::Vec3s cv::Vec4s  
int32S int cv::Vec2i cv::Vec3i cv::Vec4i  
float32F float cv::Vec2f cv::Vec3f cv::Vec4f cv::Vec6f
double64F double cv::Vec2d cv::Vec3d cv::Vec4d cv::Vec6d

 

成员变量

int cv::Mat::cols;     //返回矩阵的列数 
int cv::Mat::rows      // 返回矩阵行数 
uchar* cv::Mat::data   // 指向矩阵的数据单元的指针 
int cv::Mat::dims      // 返回矩阵维度,该维度≥2 
MatSize cv::Mat::size  // 返回矩阵大小

 

成员函数

 

 

图片加载

imread()

Mat cv::imread(const String& filename,int flags=IMREAD_COLOR)
Python:retval=cv.imread(filename[,flags])
  • 支持的图片格式

图片格式 后缀 是否支持
Windows bitmaps *.bmp, *.dib always supported
JPEG files *.jpeg, *.jpg, *.jpe see the Note section
JPEG 2000 files *.jp2 see the Note section
Portable Network Graphics *.png see the Note section
WebP *.webp see the Note section
Portable image format *.pbm, *.pgm, *.ppm *.pxm, *.pnm always supported
Sun rasters *.sr, *.ras always supported
TIFF files *.tiff, *.tif see the Note section
OpenEXR Image files *.exr see the Note section
Radiance HDR *.hdr, *.pic always supported
Raster and Vector geospatial data supported by GDAL   see the Note section

Note:

The function determines the type of an image by the content, not by the file extension.
In the case of color images, the decoded images will have the channels stored in B G R order.
When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available. Results may differ to the output of cvtColor()
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.
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.
In the case you set WITH_GDAL flag to true in CMake and IMREAD_LOAD_GDAL to load the image, then the GDAL driver will be used in order to decode the image, supporting the following formats: Raster, Vector.
If EXIF information is embedded in the image file, the EXIF orientation will be taken into account and thus the image will be rotated accordingly except if the flags IMREAD_IGNORE_ORIENTATION or IMREAD_UNCHANGED are passed.
By default number of pixels must be less than 2^30. Limit can be set using system variable OPENCV_IO_MAX_IMAGE_PIXELS
  • 读取类型

类型枚举:
cv::ImreadModes {
  cv::IMREAD_UNCHANGED = -1,
  cv::IMREAD_GRAYSCALE = 0,
  cv::IMREAD_COLOR = 1,
  cv::IMREAD_ANYDEPTH = 2,
  cv::IMREAD_ANYCOLOR = 4,
  cv::IMREAD_LOAD_GDAL = 8,
  cv::IMREAD_REDUCED_GRAYSCALE_2 = 16,
  cv::IMREAD_REDUCED_COLOR_2 = 17,
  cv::IMREAD_REDUCED_GRAYSCALE_4 = 32,
  cv::IMREAD_REDUCED_COLOR_4 = 33,
  cv::IMREAD_REDUCED_GRAYSCALE_8 = 64,
  cv::IMREAD_REDUCED_COLOR_8 = 65,
  cv::IMREAD_IGNORE_ORIENTATION = 128
}


按原样读取图片
IMREAD_UNCHANGED 
Python: cv.IMREAD_UNCHANGED
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.

读取图像时总是转换为单通道图像
IMREAD_GRAYSCALE 
Python: cv.IMREAD_GRAYSCALE

读取图像时总是转换为3通道的BGR图像
IMREAD_COLOR 
Python: cv.IMREAD_COLOR

IMREAD_ANYDEPTH 
Python: cv.IMREAD_ANYDEPTH
If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

IMREAD_ANYCOLOR 
Python: cv.IMREAD_ANYCOLOR
If set, the image is read in any possible color format.

IMREAD_LOAD_GDAL 
Python: cv.IMREAD_LOAD_GDAL
If set, use the gdal driver for loading the image.

IMREAD_REDUCED_GRAYSCALE_2 
Python: cv.IMREAD_REDUCED_GRAYSCALE_2
If set, always convert image to the single channel grayscale image and the image size reduced 1/2.

IMREAD_REDUCED_COLOR_2 
Python: cv.IMREAD_REDUCED_COLOR_2
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.

IMREAD_REDUCED_GRAYSCALE_4 
Python: cv.IMREAD_REDUCED_GRAYSCALE_4
If set, always convert image to the single channel grayscale image and the image size reduced 1/4.

IMREAD_REDUCED_COLOR_4 
Python: cv.IMREAD_REDUCED_COLOR_4
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.

IMREAD_REDUCED_GRAYSCALE_8 
Python: cv.IMREAD_REDUCED_GRAYSCALE_8
If set, always convert image to the single channel grayscale image and the image size reduced 1/8.

IMREAD_REDUCED_COLOR_8 
Python: cv.IMREAD_REDUCED_COLOR_8
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.

IMREAD_IGNORE_ORIENTATION 
Python: cv.IMREAD_IGNORE_ORIENTATION
If set, do not rotate the image according to EXIF's orientation flag.

 

图片保存

imwrite()

bool cv::imwrite(const String& filename,InputArray img,const std::vector<int>& params = std::vector<int>())		
Python:retval=cv.imwrite(filename,img[,params])
参数:
filename 保存路径
img	要保存的图片
params	格式参数对(paramId_1, paramValue_1, paramId_2, paramValue_2, ... .),后面有描述。
  • 描述
    该函数通常只支持CV_8UC1或CV_8UC3,但是也支持如下情况:

16-bit unsigned (CV_16U) images can be saved in the case of PNG, JPEG 2000, and TIFF formats
32-bit float (CV_32F) images can be saved in TIFF, OpenEXR, and Radiance HDR formats; 3-channel (CV_32FC3) TIFF images will be saved using the LogLuv high dynamic range encoding (4 bytes per pixel)
PNG images with an alpha channel can be saved using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below).
  • 保存CV_32F图片
    对于数据类型为CV_32F的图片,应保存为*.exr格式。然后读取的时候读取格式为cv::IMREAD_UNCHANGED或-1。例如:

cv::imwrite("A.exr",imgDepth);
cv::img=cv::imread("A.exr",-1);
  • 参数对

IMWRITE_JPEG_QUALITY 
Python: cv.IMWRITE_JPEG_QUALITY
For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95.

IMWRITE_JPEG_PROGRESSIVE 
Python: cv.IMWRITE_JPEG_PROGRESSIVE
Enable JPEG features, 0 or 1, default is False.

IMWRITE_JPEG_OPTIMIZE 
Python: cv.IMWRITE_JPEG_OPTIMIZE
Enable JPEG features, 0 or 1, default is False.

IMWRITE_JPEG_RST_INTERVAL 
Python: cv.IMWRITE_JPEG_RST_INTERVAL
JPEG restart interval, 0 - 65535, default is 0 - no restart.

IMWRITE_JPEG_LUMA_QUALITY 
Python: cv.IMWRITE_JPEG_LUMA_QUALITY
Separate luma quality level, 0 - 100, default is 0 - don't use.

IMWRITE_JPEG_CHROMA_QUALITY 
Python: cv.IMWRITE_JPEG_CHROMA_QUALITY
Separate chroma quality level, 0 - 100, default is 0 - don't use.

IMWRITE_PNG_COMPRESSION 
Python: cv.IMWRITE_PNG_COMPRESSION
For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting).

IMWRITE_PNG_STRATEGY 
Python: cv.IMWRITE_PNG_STRATEGY
One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.

IMWRITE_PNG_BILEVEL 
Python: cv.IMWRITE_PNG_BILEVEL
Binary level PNG, 0 or 1, default is 0.

IMWRITE_PXM_BINARY 
Python: cv.IMWRITE_PXM_BINARY
For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1.

IMWRITE_EXR_TYPE 
Python: cv.IMWRITE_EXR_TYPE
IMWRITE_WEBP_QUALITY 
Python: cv.IMWRITE_WEBP_QUALITY
override EXR storage type (FLOAT (FP32) is default)

For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.

IMWRITE_PAM_TUPLETYPE 
Python: cv.IMWRITE_PAM_TUPLETYPE
For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format.

IMWRITE_TIFF_RESUNIT 
Python: cv.IMWRITE_TIFF_RESUNIT
For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values.

IMWRITE_TIFF_XDPI 
Python: cv.IMWRITE_TIFF_XDPI
For TIFF, use to specify the X direction DPI.

IMWRITE_TIFF_YDPI 
Python: cv.IMWRITE_TIFF_YDPI
For TIFF, use to specify the Y direction DPI.

IMWRITE_TIFF_COMPRESSION 
Python: cv.IMWRITE_TIFF_COMPRESSION
For TIFF, use to specify the image compression scheme. See libtiff for integer constants corresponding to compression formats. Note, for images whose depth is CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified by this flag; LZW compression is the default.

 

 

参考文献

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章