opencv显示一张图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/withlonger/article/details/81191967

今天开始学习opencv,简单记录一下学习过程。学习opencv就由显示一张图片开始吧。保存一张图片用数据结构Mat,读入一张图片用函数imread。详细定义如下,一参为图片的路径,二参为枚举常量,枚举常量可以取的值有13个。

Mat imread( const String& filename, int flags = IMREAD_COLOR );
//! Imread flags
enum ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image.
       IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
       IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
       IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
       IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
       IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
       IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
       IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
       IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
     };

显示一张图片,用函数imshow。在opencv源码中函数的声明如下,InputArray可以传入一个Mat的类型。

void imshow(const String& winname, InputArray mat);

 以下是一个用法的示例。

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

void main() {
	//读入一张图片,一参为图片的绝对路径,二参为枚举常量,有 IMREAD_COLOR、IMREAD_GRAYSCALE等
	Mat srcImg = imread("d:/data/test.png",IMREAD_LOAD_GDAL);
	imshow("源图像", srcImg);
	waitKey();//暂停窗口
}

猜你喜欢

转载自blog.csdn.net/withlonger/article/details/81191967