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能夠有如下的值:spa

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。上述代碼是在一個圖片上隨機產生鹽點。code

  迭代器

 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);

 

圖像算術運算

  圖像的算術運算能夠有加減乘除,能夠對圖片有亮度有影響,具體的後續再瞭解。orm

圖像感興趣區域

  圖像感興趣區域指的是,對一個圖片的部分區域感興趣,則能夠只操做感興趣的部分區域,也即子區域。blog

相關文章
相關標籤/搜索