OpenCV学习笔记(二)

来源:互联网 发布:淘宝网手机版登录 编辑:程序博客网 时间:2024/06/05 01:11

接下来实现第一个功能——翻转图像

在写代码之前,先介绍一个函数——flip


flip函数的定义:

void cv::flip(InputArray src,OutputArray dst,int flipCode)
其中,  
src:要处理的原始图像
dst:和原始图像具有同样大小、类型的目标图像(我的程序中就不新建图像了,而是用处理过得图像覆盖原图像
flipCode:旋转类型。(0代表x轴翻转,任意正数代表y轴翻转,任意负数代表x轴、y轴同时翻转)

#include <iostream>  #include <opencv2/core/core.hpp>  #include <opencv2/highgui/highgui.hpp>  using namespace cv;  int main()  {      // 读入一张图片(lena灰度图)      Mat img=imread("lena灰度图.jpg");     //翻转图像     flip(img,img,-1);   //x轴、y轴同时翻转    // 这里也可以写成 flip(img,img,0);    //x轴翻转       //或者flip(img,img,1);    //y轴翻转       // 创建一个名为 "图像"窗口      cvNamedWindow("图像");      // 在窗口中显示图像     imshow("图像",img);      // 等待2000 ms后窗口自动关闭      waitKey(2000);  }  

程序执行结果分别由三种情况:

第一种:  flip(img,img,-1);   x轴、y轴同时翻转 


第二种:flip(img,img,0);    //x轴翻转


第三种:flip(img,img,1);    //y轴翻转



搞定!!!