OpenCV学习(一)基础篇

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 matrix
12 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/GetWindowProperty
10     CV_WINDOW_NORMAL       = 0x00000000, //the user can resize the window (no constraint)  / also use to switch a fullscreen window to a normal size
11     CV_WINDOW_AUTOSIZE     = 0x00000001, //the user cannot resize the window, the size is constrainted by the image displayed
12     CV_WINDOW_OPENGL       = 0x00001000, //window with opengl support
13 
14     //Those flags are only for Qt
15     CV_GUI_EXPANDED         = 0x00000000, //status bar and tool bar
16     CV_GUI_NORMAL           = 0x00000010, //old fashious way
17 
18     //These 3 flags are used by cvNamedWindow and cvSet/GetWindowProperty
19     CV_WINDOW_FULLSCREEN   = 1,//change the window to fullscreen
20     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<n; ++k)
 4     {
 5         int i=rand()%image.cols;
 6         int j=rand()%image.rows;
 7         if(image.channels() == 1) // gray image
 8         {
 9             image.at<uchar>(j, i) = 255;
10         }
11         else if(image.channels() == 3) // color image
12         {
13             image.at<cv::Vec3b>(j, i)[0] = 255;
14             image.at<cv::Vec3b>(j, i)[1] = 255;
15             image.at<cv::Vec3b>(j, i)[2] = 255;
16         }
17     }
18 }

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

  迭代器

 1 cv::Mat Iterator_<cv::Vec3b> it;
 2 // 定义在Mat_内部的迭代器
 3 cv::Mat_<cv::Vec3b>::iterator it;
 4 
 5 // 迭代器使用
 6 void colorReduce(cv::Mat &image, int div=64)
 7 {
 8     // 得到初始位置的迭代器
 9     cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>();
10     // 得到种植位置的迭代器
11     cv::Mat_<cv::Vec3b>::iterator itend = image.edn<cv::Vec3b>();
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<uchar>(index);

图像算术运算

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

图像感兴趣区域

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

原文地址:https://www.cnblogs.com/zhanghang-BadCoder/p/7593273.html