實驗說明:
本文主要講一些opencv 2.0版本后出現的Mat矩形類,主要是參考opencv自帶doc文件夾下的tutiol教材。通過這次實驗覺得用Mat的話下面幾點需要特別注意(在代碼中可以體現出來):
-
利用create函數重新改變Mat數據在內存中的布局。
-
注意多通道數據在Mat中其實也是占一個元素的位置的。
-
學會多維Mat的創建方法。
-
當Mat矩陣比較小時,學會直接賦值的方法,即用Mat_。
5. 掌握Mat矩陣內容輸出到終端時的幾種常見格式。
6. 注意如果vector是單獨一維的話需要轉換成Mat才能輸出,多維的可以直接輸出,例如vector里面存放的是點的話。
開發環境:Ubuntu12.04+Qt4.8.2+QtCreator2.5+opencv2.4.2
實驗結果:
軟件運行后界面(每按一下next,執行關於Mat的一部分代碼):

終端輸出數據截圖:

實驗代碼和注釋:
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_nextButton_clicked(); void on_closeButton_clicked(); private: Ui::MainWindow *ui; int next_num; }; #endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //setStyleSheet為設置工作表的風格,其參數是一個Qstring類型,utf-8是unicode中一種變長編碼 //background-color為Qt Style Sheets的一個屬性,具體的使用方法可以查看其幫助文檔 ui->textBrowser->setStyleSheet(QString::fromUtf8 ("background-color:black")); next_num = 0; } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_nextButton_clicked() { next_num ++; ui->textBrowser->setTextColor( Qt::green ); switch (next_num) { case 1: { ui->textBrowser->append ( "--------------------------------------------------------------------------\r" "This program shows how to create matrices(cv::Mat) in OpenCV and its serial" " out capabilities\r" "That is, cv::Mat M(...); M.create and cout << M.\r " "Shows how output can be formated to OpenCV, python, numpy, csv and C styles.\r" "Usage:\r" "./cvout_sample\r" "--------------------------------------------------------------------------\r" ); break; } case 2: { Mat M( 2, 2, CV_8UC3, Scalar(0,255,0) );//其實是2*6的矩陣,因為每個元素有3個通道。 ui->textBrowser->append("create by using the constructor......" ); cout<<"M = "<<M<<endl; M.create( 4, 4, CV_8UC(2) );//括號里面的2表示2通道 ui->textBrowser->append( "create by using create function......" ); cout<<"M = "<<M<<endl; break; }//當case語句里面變量定義時,需要用括號括起來,否則會報錯 case 3: { int sz[3] = {2, 2, 2}; Mat L( 3, sz, CV_8UC(1), Scalar::all(0) ); ui->textBrowser->append( "create multidimensional matrix......" ); // cout<<"L = "<<L<<endl;此處不能打印出來,因為那只適應二維數組 break; } case 4: { Mat E = Mat::eye(4, 4, CV_64F); Mat O = Mat::ones(2, 3, CV_32F); Mat Z = Mat::zeros(3, 3, CV_8UC1); ui->textBrowser->append( "using matlab stytle......" ); cout<<"E = "<<E<<endl; cout<<"O = "<<O<<endl; cout<<"Z = "<<Z<<endl; break; } case 5: { Mat C =(Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);//直接賦初始值的方法 Mat row_clone = C.row(1).clone(); ui->textBrowser->append( "create 3*3 double-precision identity matrix......" ); cout<<"C = "<<C<<endl; cout<<"row_clone = "<<row_clone<<endl; break; } case 6: { Mat R = Mat( 3, 2, CV_8UC3 ); randu( R, Scalar::all(0), Scalar::all(255) ); ui->textBrowser->append( "fill a matrix with rand numbers......" ); cout<<"R (default) = "<<R<<endl; ui->textBrowser->append( "demonstrate the output formating options......" ); cout<<"R (python) = "<<format(R, "python")<<endl; cout<<"R (numpy) = "<<format(R, "numpy")<<endl;//numpy是一個用python實現的科學計算包 cout<<"R (csv) = "<<format(R, "csv")<<endl;//csv,逗號分隔符 cout<<"R (c) =" <<format(R, "C")<<endl; break; } case 7: { ui->textBrowser->append( "the point format output......" ); Point2f P1(5, 1); cout<<"Point (2D) = "<<P1<<endl; Point3f P2(4, 5, 6); cout<<"Point (3D) = "<<P2<<endl; vector<float>v; v.push_back( (float)CV_PI); v.push_back( 2 );//push_back為在其尾部加入一個數據 v.push_back( 3.01f ); cout<<"vector of float: = "<<Mat(v)<<endl;//vector數據是沒法單獨輸出的,因此可以借助mat輸出 int N = 20; vector<Point2f>vPoints(N);//vector可以用變量定義其長度,比數組好用 for( size_t E = 0; E < vPoints.size(); ++E ) //size_t其實就是一個unsigned int類型 vPoints[E] = Point2f((float)(E*5), (float)(E%7)); cout<<"vPoints[] = "<<vPoints<<endl;//但是vector點確實可以直接輸出的,因為這時候的vector本身就是 //一個多維(至少2維)Mat了 break; } default:break; } } void MainWindow::on_closeButton_clicked() { close(); }
main.cpp:
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
