博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV学习(一)基础篇
阅读量:4681 次
发布时间:2019-06-09

本文共 4020 字,大约阅读时间需要 13 分钟。

OpenCV 2 计算机视觉编程手册读书笔记1

矩阵创建

  Mat类是OpenCV中非常有用类,用来创建和操作多维矩阵。可以有很多方法构造它。

1 // 构造函数 2 //! constructs 2D matrix of the specified size and type 3 // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.) 4 Mat(int rows, int cols, int type); 5 Mat(Size size, int type); 6 //! constucts 2D matrix and fills it with the specified value _s. 7 Mat(int rows, int cols, int type, const Scalar& s); 8 Mat(Size size, int type, const Scalar& s); 9 10 // 感兴趣区域,部分区域,子区块,新的矩阵和m共同使用内存11 //! creates a matrix header for a part of the bigger matrix12 Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());13 Mat(const Mat& m, const Rect& roi);14 Mat(const Mat& m, const Range* ranges);15 16 // create()函数17 Mat mat(2, 2, CV_8UC3); //构造函数创建矩阵18 M.create(3, 2, CV_8UC2); //释放内存重新创建19 20 // Matlab风格的创建方法21 Mat zero = Mat::zeros(2, 3, CV_8UC1);22 Mat one = Mat::ones(2, 3, CV32F);23 Mat eye = Mat::eye(2, 3, CV_64F);

  其中 type参数的值CV_8UC3,表示的是CV_8U类型,3通道,CV_8U即无符号8为数据。3通道相当于BGR通道。

图像读取

cv::imread( const string& filename, int flags);

读取指定的图片,flags可以有以下的值:

enum{/* 8bit, color or not */    CV_LOAD_IMAGE_UNCHANGED  =-1,/* 8bit, gray */    CV_LOAD_IMAGE_GRAYSCALE  =0,/* ?, color */    CV_LOAD_IMAGE_COLOR      =1,/* any depth, ? */    CV_LOAD_IMAGE_ANYDEPTH   =2,/* ?, any color */    CV_LOAD_IMAGE_ANYCOLOR   =4};

图像显示

1 // 先命名一个窗口,再使用2 namedWindow( "output", CV_WINDOW_AUTOSIZE );3 imshow("input", img);

  窗口可以有以下Type:  

1 enum 2 { 3     //These 3 flags are used by cvSet/GetWindowProperty 4     CV_WND_PROP_FULLSCREEN = 0, //to change/get window's fullscreen property 5     CV_WND_PROP_AUTOSIZE   = 1, //to change/get window's autosize property 6     CV_WND_PROP_ASPECTRATIO= 2, //to change/get window's aspectratio property 7     CV_WND_PROP_OPENGL     = 3, //to change/get window's opengl support 8  9     //These 2 flags are used by cvNamedWindow and cvSet/GetWindowProperty10     CV_WINDOW_NORMAL       = 0x00000000, //the user can resize the window (no constraint)  / also use to switch a fullscreen window to a normal size11     CV_WINDOW_AUTOSIZE     = 0x00000001, //the user cannot resize the window, the size is constrainted by the image displayed12     CV_WINDOW_OPENGL       = 0x00001000, //window with opengl support13 14     //Those flags are only for Qt15     CV_GUI_EXPANDED         = 0x00000000, //status bar and tool bar16     CV_GUI_NORMAL           = 0x00000010, //old fashious way17 18     //These 3 flags are used by cvNamedWindow and cvSet/GetWindowProperty19     CV_WINDOW_FULLSCREEN   = 1,//change the window to fullscreen20     CV_WINDOW_FREERATIO    = 0x00000100,//the image expends as much as it can (no ratio constraint)21     CV_WINDOW_KEEPRATIO    = 0x00000000//the ration image is respected.22 };

图像存储

1 cv::imrwite("output.jpg", imgOut);

 

图像像素操作

  at函数

1 void salt(cv::Mat &image, int n) 2 { 3     for(int k =0; k
(j, i) = 255;10 }11 else if(image.channels() == 3) // color image12 {13 image.at
(j, i)[0] = 255;14 image.at
(j, i)[1] = 255;15 image.at
(j, i)[2] = 255;16 }17 }18 }

  其中Vec3b的定义为:typedef Vec<uchar, 3> Vec3b。上述代码是在一个图片上随机产生盐点。

  迭代器

1 cv::Mat Iterator_
it; 2 // 定义在Mat_内部的迭代器 3 cv::Mat_
::iterator it; 4 5 // 迭代器使用 6 void colorReduce(cv::Mat &image, int div=64) 7 { 8 // 得到初始位置的迭代器 9 cv::Mat_
::iterator it = image.begin
();10 // 得到种植位置的迭代器11 cv::Mat_
::iterator itend = image.edn
();12 // 遍历所有像素13 for(; it!=itend; ++it)14 {15 // 处理每个像素16 (*it)[0] = (*it)[0]/div*div+div/2;17 (*it)[1] = (*it)[0]/div*div+div/2;18 (*it)[2] = (*it)[0]/div*div+div/2;19 }20 }

  指针操作

// 得到image图像的第index行的首地址image.ptr
(index);

 

图像算术运算

  图像的算术运算可以有加减乘除,可以对图片有亮度有影响,具体的后续再了解。

图像感兴趣区域

  图像感兴趣区域指的是,对一个图片的部分区域感兴趣,则可以只操作感兴趣的部分区域,也即子区域。

转载于:https://www.cnblogs.com/zhanghang-BadCoder/p/7593273.html

你可能感兴趣的文章
HTML属性的应用
查看>>
HEAP CORRUPTION DETECTED
查看>>
Android URI简单介绍
查看>>
蒙板 模态对话框
查看>>
【POJ - 3009】Curling 2.0 (dfs+回溯)
查看>>
Windows下载安装良心教程
查看>>
Android上下文菜单ContextMenu
查看>>
【bzoj4543】Hotel加强版(thr)
查看>>
[stm32] 中断
查看>>
RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
查看>>
两个时间相差多少 .net中的timespan应用
查看>>
递归 换零钱问题——由打靶子问题引申
查看>>
Python-函数基础
查看>>
Extensible Messaging and Presence Protocol (XMPP) 简介
查看>>
Farm Irrigation
查看>>
windows平板的开发和选型
查看>>
无平方因子的数(数论初步) By ACReaper
查看>>
C语言截取字符串
查看>>
构建之法阅读笔记06
查看>>
CentOS minimal新装配置笔记
查看>>