Opencv(1)---图像读取、显示、保存

来源:互联网 发布:nba体测数据最差 编辑:程序博客网 时间:2024/05/21 22:45

一 图像处理基本概念

1. 图像处理的一般过程

图像处理的一般过程
2. 图像的基本操作

图像的基本操作
3.图像的基本属性

1).empty() 判断文件读取是否正确2).rows 获取图像行数(高度)3).cols 获取图像列数(长度)4).channels() 获取图像通道数5).depth() 获取图像位深度

实例1

Mat img = imread(“1.jpg”);If(img.empty())  //异常处理{    std::cout<<“read image error!”}

实例2

Mat img = imread(“2.jpg”);std::cout<<“图像长度为”<<img.cols;  //获取图像大小std::cout<<“图像宽度为”<<img.rows;

二 代码

1. mainwindow.cpp

#include "mainwindow.h"#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>using namespace cv;MainWindow::MainWindow(QWidget *parent)    : QMainWindow(parent){    Mat srcImg = imread("1.png"); //读取图片    if(srcImg.empty())        return;    cvtColor(srcImg, srcImg, COLOR_BGR2GRAY); //转为灰度图    namedWindow("img", WINDOW_AUTOSIZE); //创建窗口    imshow("img", srcImg); //显示图片    imwrite("1.bmp", srcImg); //保存结果图片    waitKey(0); //暂停按键等待}MainWindow::~MainWindow(){}

2. 运行结果
exe文件运行结果
这里写图片描述
生成bmp文件
这里写图片描述

三 知识点详解

1.头文件
opencv的标配头文件为

#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>

可以使用

#include "opencv2/opencv.hpp"

这一行代码进行代替

原因:
在D:\InstallOpencv\opencvBinary\include\opencv2\core.cpp中
定义了如下代码

#include "opencv2/core/cvdef.h"#include "opencv2/core/version.hpp"#include "opencv2/core/base.hpp"#include "opencv2/core/cvstd.hpp"#include "opencv2/core/traits.hpp"#include "opencv2/core/matx.hpp"#include "opencv2/core/types.hpp"#include "opencv2/core/mat.hpp"#include "opencv2/core/persistence.hpp"

2.命名空间
opencv中的C++类和函数都是定义在命名空间cv之内的,所以有如下opencv函数的两种调用方法:

ex1:Mat srcImg = cv::imread("1.jpg");

或者

ex2:using namespace cv;Mat srcImg = imread("1.jpg");

注:一般推荐使用

using namespace cv;

因而简单的opencv程序头文件为下列代码

#include "opencv2/core/core.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"using namespace cv;

3.imread函数

Mat srcImg = imread("1.png");

imread函数原型

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

1)第一个参数,需要填入图片路径名,支持格式:
Windows位图: .bmp, .dib
JPEG文件: .jpeg, .jpg, *.jpe
JPEG2000文件: *.jp2
PNG图片: *.png
便携文件格式: .pbm, .pgm, *.ppm
Sun rasters光栅文件: .sr, .ras
TIFF文件: .tiff, .tif
2)第二个参数:指定加载图像的颜色类型,默认为1
图像类型如下

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.     };

4. namedwindow
namedwindow函数原型

CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);

1)第一个参数
设置窗口名称
2)第二个参数
设置窗口的显示类型

enum WindowFlags {       WINDOW_NORMAL     = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.       WINDOW_AUTOSIZE   = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.       WINDOW_OPENGL     = 0x00001000, //!< window with opengl support.       WINDOW_FULLSCREEN = 1,          //!< change the window to fullscreen.       WINDOW_FREERATIO  = 0x00000100, //!< the image expends as much as it can (no ratio constraint).       WINDOW_KEEPRATIO  = 0x00000000  //!< the ratio of the image is respected.     };

5. imshow函数
函数原型

void imshow(const String& winname, const ogl::Texture2D& tex);

1)第一个参数,设置需要显示的窗口名称
2)第二个参数,填写需要显示的图像

6. imwrite函数

bool imwrite( const String& filename, InputArray img,              const std::vector<int>& params = std::vector<int>());

1)第一个参数,设置保存的文件名,需填写后缀,如”1.bmp”
2)第二个参数,要保存的Mat类型图像数据
3)第三个参数,表示特定格式保存的参数编码,一般采用默认值不填写
7. waitKey()函数

/** @brief Waits for a pressed key.The function waitKey waits for a key event infinitely (when \f$\texttt{delay}\leq 0\f$ ) or for delaymilliseconds, when it is positive. Since the OS has a minimum time between switching threads, thefunction will not wait exactly delay ms, it will wait at least delay ms, depending on what else isrunning on your computer at that time. It returns the code of the pressed key or -1 if no key waspressed before the specified time had elapsed.@noteThis function is the only method in HighGUI that can fetch and handle events, so it needs to becalled periodically for normal event processing unless HighGUI is used within an environment thattakes care of event processing.@noteThe function only works if there is at least one HighGUI window created and the window is active.If there are several HighGUI windows, any of them can be active.@param delay Delay in milliseconds. 0 is the special value that means "forever". */CV_EXPORTS_W int waitKey(int delay = 0);

1)参数,如果delay>0, 表示等待delay毫秒之后结束
如果delay=0, 表示无限等待,直到有按键按下结束
2)返回值为对应按下按键的ASCII码值,如Esc的ASCII码为27
ASCII表

0 0
原创粉丝点击