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(感興趣區域)。
數據類型
-
文檔:https://docs.opencv.org/master/d1/d1b/group__core__hal__interface.html#ga88c4cd9de76f678f33928ef1e3f96047
下表是OpenCV的數據類型和對應的枚舉索引:
+--------+----+----+----+----+------+------+------+------+ | | 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 // 返回矩陣大小
成員函數
-
獲取圖像位深度
mat_name.depth()
-
獲取矩陣通道的數目
mat_name.channels()
-
獲取存儲的矩陣元素的數據類型
mat_name.type()
-
拷貝圖像
matA.copyto(matB)
-
轉換數據格式
matA.convertTo(matB,int rtype,double alpha = 1,double beta = 0)
-
太多了列不下:https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5
圖片加載
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.
參考文獻
-
初雲亭.C++下的OpenCV中Mat類型存儲的圖像格式.https://segmentfault.com/a/1190000015653101
-
Capricorn_zhang.openCV學習(一)Mat數據類型.https://blog.csdn.net/qq_34963853/article/details/79746166