OpenCV4萌新之路——詳解圖像讀取函數 「imread」


處理圖像第一步固然是要讀取一張圖像,OpenCV給出的方法也很簡單:

Mat cv::imread(const String& filename, int flags = IMREAD_COLOR);

1、函數簡析

頭文件 #include <opencv2/imgcodecs.hpp>html

imread 返回類型 命名空間 函數名 參數1 參數2
詳細 Mat cv imread const String& filename int flags = IMREAD_COLOR
詳細 - - - 文件路徑+文件名 圖像讀取模式

2、參數詳解

1.String& filename

輸入可爲相對路徑也可爲絕對路徑,運用方法參見測試代碼。ios

注:
imread函數支持讀取的圖像格式有
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)
PFM files - *.pfm (see the Note section)
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)












web

2.flags = IMREAD_COLOR

枚舉名 定義 解釋
-1 IMREAD_UNCHANGED If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation. 若是設置,則按原樣返回加載的圖像(使用Alpha通道,不然會被裁剪)
0 IMREAD_GRAYSCALE If set, always convert image to the single channel grayscale image (codec internal conversion). 若是設置,則始終將圖像轉換爲單通道灰度圖像(編解碼器內部轉換)
1 IMREAD_COLOR If set, always convert image to the 3 channel BGR color image. 若是設置,請始終將圖像轉換爲3通道BGR彩色圖像
2 IMREAD_ANYDEPTH If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. 若是設置,則在輸入具備相應深度時返回16位/ 32位圖像,不然將其轉換爲8位
4 IMREAD_ANYCOLOR If set, the image is read in any possible color format. 若是設置,則以任何可能的顏色格式讀取圖像
8 IMREAD_LOAD_GDAL If set, use the gdal driver for loading the image. 若是設置,使用gdal驅動程序加載圖像
16 IMREAD_REDUCED_GRAYSCALE_2 If set, always convert image to the single channel grayscale image and the image size reduced 1/2. 若是設置,則始終將圖像轉換爲單通道灰度圖像,圖像尺寸減少1/2
17 IMREAD_REDUCED_COLOR_2 If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. 若是設置,則始終將圖像轉換爲3通道BGR彩色圖像,圖像尺寸減少1/2
32 IMREAD_REDUCED_GRAYSCALE_4 If set, always convert image to the single channel grayscale image and the image size reduced 1/4. 若是設置,則始終將圖像轉換爲單通道灰度圖像,圖像尺寸減少1/4
33 IMREAD_REDUCED_COLOR_4 If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. 若是設置,則始終將圖像轉換爲3通道BGR彩色圖像,圖像尺寸減少1/4
64 IMREAD_REDUCED_GRAYSCALE_8 If set, always convert image to the single channel grayscale image and the image size reduced 1/8. 若是設置,則始終將圖像轉換爲單通道灰度圖像,圖像尺寸減少1/8
65 IMREAD_REDUCED_COLOR_8 If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8. 若是設置,則始終將圖像轉換爲3通道BGR彩色圖像,圖像尺寸減少1/8
128 IMREAD_IGNORE_ORIENTATION If set, do not rotate the image according to EXIF’s orientation flag. 若是設置,請不要根據EXIF的方向標誌旋轉圖像

3、測試代碼

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;

#define IMAGE_ABSOLUTE_PATH "F:/Leraning_OpenCV4/Sakai_Izumi.jpg" //絕對路徑
#define IMAGE_RELATIVE_PATH "./Sakai_Izumi.jpg" //相對路徑

int main()
{

	//讀取圖片
	Mat src = imread(IMAGE_ABSOLUTE_PATH, IMREAD_COLOR);
	//Mat src = imread(IMAGE_RELATIVE_PATH, 1);

	//判讀是否成功讀取圖片
	if (src.empty())
	{
		std::cout << "Load img failed!" << endl;
		return 0;
	}
	else
	{
		std::cout << "Load img success!" << endl;
	}

	//顯示圖片
	imshow("src", src);
	waitKey(0);

	return 1;
}

1. 輸入圖像參數

在這裏插入圖片描述

2. 輸出圖像顯示

泉水姐姐!

3. 圖像參數

在這裏插入圖片描述

4. 其餘測試

ImreadModes 0 讀取灰度圖
在這裏插入圖片描述
ImreadModes 16 讀取灰度圖,寬、高都爲原圖的1/2
在這裏插入圖片描述
你們能夠參考IMREAD_COLOR中不一樣的圖像讀取模式,對比一下顯示圖像,體會一下差別在哪。



函數

4、參考文獻

  1. Image file reading and writing.
相關文章
相關標籤/搜索