トップページ -> コンピュータ実習 -> OpenCV の使用法 -> OpenCV を用いた行列操作
[サイトマップへ]  

OpenCV を用いた行列操作

サイト構成 連絡先,業績など コンピュータ実習 データの扱い コンピュータ設定 教材(公開) サポートページ

この Web ページでは,行列、行ベクトル,列ベクトルという言葉を次の意味で扱う.


要素の型

参考Webページ:

http://docs.opencv.org/modules/core/doc/intro.html#fixed-pixel-types-limited-use-of-templates

◆ チャンネル数=1の場合の表記 ※ 末尾に「C1」を付けても同じ意味


行ベクトル

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat

行ベクトルのコンストラクタ

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  unsigned char v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat vec01 = cv::Mat(1, sizeof(v)/sizeof(unsigned char), CV_8U, v); 
  cv::Mat vec02 = cv::Mat::zeros(1, 5, CV_8U);
  cv::Mat vec03 = cv::Mat::ones(1, 5, CV_8U);
  cv::Mat vec04 = cv::Mat(1, 5, CV_8U, cv::Scalar(8));

  cv::Mat vec05 = cv::Mat::zeros(1, 5, CV_8U);
  cv::randu(vec05, cv::Scalar(0), cv::Scalar(256));

  cv::Mat vec06 = cv::Mat::zeros(1, 5, CV_8U);
  cv::randn(vec06, cv::Scalar(128), cv::Scalar(5));

  unsigned char v07[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat vec07 = cv::Mat(1, sizeof(v)/sizeof(unsigned char), CV_8U, v07); 
  cv::Mat vec08 = vec07;  
  vec08.cv::Mat::col(2) = 23; 

  unsigned char v09[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat vec09 = cv::Mat(1, sizeof(v)/sizeof(unsigned char), CV_8U, v09); 
  cv::Mat vec10 = vec09.clone();  
  vec10.cv::Mat::col(2) = 23; 

  unsigned char v11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat11 = cv::Mat(3, 4, CV_8U, v11); 
  cv::Mat vec11 = mat11.reshape(1, 12);  

  unsigned char v12[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat vec12 = cv::Mat(1, sizeof(v)/sizeof(unsigned char), CV_8U, v12); 
  cv::Mat vec13;
  vec12.convertTo( vec13, CV_32F ); 

  cout << vec01 << "\n";
  cout << vec02 << "\n";
  cout << vec03 << "\n";
  cout << vec04 << "\n";
  cout << vec05 << "\n";
  cout << vec06 << "\n";
  cout << vec07 << "\n";
  cout << vec08 << "\n";
  cout << vec09 << "\n";
  cout << vec10 << "\n";
  cout << mat11 << "\n";
  cout << vec11 << "\n";
  cout << vec12 << "\n";
  cout << vec13 << "\n";

  waitKey(0);

  return 0;
}

◆ ビルド操作の例

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例

行列の要素の操作

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  unsigned char v[] = { 101, 102, 103, 104 }; 
  cv::Mat vec01 = cv::Mat(1, sizeof(v)/sizeof(unsigned char), CV_8U, v); 

  cout << vec01.cv::Mat::col(0) << "\n";
  cout << vec01.cv::Mat::col(2) << "\n";

  vec01.cv::Mat::col(2) = 23; 
  cout << vec01 << "\n";

  waitKey(0);

  return 0;
}

◆ ビルド操作の例 ※ 上と同じ

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例

行ベクトルの演算

http://docs.opencv.org/modules/core/doc/basic_structures.html#matrix-expressions

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  cout << cv::Mat::zeros(1, 5, CV_8U).type() << "\n";

  cout << cv::Mat::zeros(1, 5, CV_8U).size().width << "\n";

  cout << cv::Mat::zeros(1, 5, CV_8U).t() << "\n";

  cout << cv::Mat::zeros(1, 5, CV_8U) + 5 << "\n";

  cout << ( cv::Mat::zeros(1, 5, CV_8U) + 5 ) * 3 << "\n";

  float f[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cout << 1.0 / cv::Mat(1, sizeof(f)/sizeof(float), CV_32F, f) << "\n";

  unsigned char v01[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cout << ( cv::Mat(1, sizeof(v01)/sizeof(unsigned char), CV_8U, v01) == 4 ) << "\n";

  unsigned char v02[] = { 3, 6, 2, 9, 1, 4, 7, 5, 8 }; 
  cv::Mat vec02 = cv::Mat(1, sizeof(v02)/sizeof(unsigned char), CV_8U, v02); 
  cv::Mat vec03, vec04; 
  cv::sort( vec02, vec03, CV_SORT_ASCENDING );
  cout << vec03 << "\n";
  cv::sort( vec02, vec04, CV_SORT_DESCENDING );
  cout << vec04 << "\n";

  unsigned char v05[] = { 1, 2, 3 };
  unsigned char v06[] = { 4, 5, 6 };
  cv::Mat vec05 = cv::Mat(1, sizeof(v05)/sizeof(unsigned char), CV_8U, v05); 
  cv::Mat vec06 = cv::Mat(1, sizeof(v06)/sizeof(unsigned char), CV_8U, v06); 
  cout << vec05.dot( vec06 ) << "\n";

  waitKey(0);

  return 0;
}

◆ ビルド操作の例 ※ 上と同じ

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例


行列

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat

行列のコンストラクタ

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  unsigned char v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat01 = cv::Mat(3, 4, CV_8U, v); 

  cv::Mat mat02 = cv::Mat::zeros(3, 4, CV_8U); 

  cv::Mat mat03 = cv::Mat::ones(3, 4, CV_8U); 

  cv::Mat mat04 = cv::Mat(3, 4, CV_8U, cv::Scalar(8));

  cv::Mat mat05 = cv::Mat::zeros(3, 4, CV_8U); 

  cv::Mat mat06 = cv::Mat::zeros(3, 4, CV_8U); 
  cv::randu(mat06, cv::Scalar(0), cv::Scalar(256));

  cv::Mat mat07 = cv::Mat::zeros(3, 4, CV_8U); 
  cv::randn(mat07, cv::Scalar(128), cv::Scalar(5));

  unsigned char v08[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat08 = cv::Mat(3, 4, CV_8U, v08); 
  cv::Mat mat09 = mat08;
  mat09.cv::Mat::col(2).cv::Mat::row(2) = 23; 

  unsigned char v10[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat10 = cv::Mat(3, 4, CV_8U, v10); 
  cv::Mat mat11 = mat10.clone();  
  mat11.cv::Mat::col(2).cv::Mat::row(2) = 23; 

  unsigned char v12[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat vec12 = cv::Mat(1, 12, CV_8U, v12); 
  cv::Mat mat12 = vec12.reshape(3, 4);  

  unsigned char v13[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat13 = cv::Mat(3, 4, CV_8U, v13); 
  cv::Mat mat14;
  mat13.convertTo( mat14, CV_32F ); 

  cout << mat01 << "\n";
  cout << mat02 << "\n";
  cout << mat03 << "\n";
  cout << mat04 << "\n";
  cout << mat05 << "\n";
  cout << mat06 << "\n";
  cout << mat07 << "\n";
  cout << mat08 << "\n";
  cout << mat09 << "\n";
  cout << mat10 << "\n";
  cout << mat11 << "\n";
  cout << vec12 << "\n";
  cout << mat12 << "\n";
  cout << mat13 << "\n";
  cout << mat14 << "\n";

  waitKey(0);

  return 0;
}

◆ ビルド操作の例 ※ 上と同じ

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例

行列の要素の操作

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  unsigned char v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat01 = cv::Mat(3, 4, CV_8U, v); 

  cout << mat01 << "\n";

  cout << mat01.cv::Mat::col(1) << "\n";

  cout << mat01.cv::Mat::row(2) << "\n";

  cout << mat01.diag(0) << "\n"; 
  cout << mat01.diag(-1) << "\n"; 
  cout << mat01.diag(1) << "\n"; 

  cout << mat01.cv::Mat::col(1).cv::Mat::row(2) << "\n";

  cout << ( mat01.cv::Mat::col(1).cv::Mat::row(2) = 23 ) << "\n";

  waitKey(0);

  return 0;
}

◆ ビルド操作の例 ※ 上と同じ

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例





以下、書きかけ

行列の演算

http://docs.opencv.org/modules/core/doc/basic_structures.html#matrix-expressions

◆ ソースコードの例

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
  cout << cv::Mat::zeros(4, 3, CV_8U).type() << "\n";

  cout << cv::Mat::zeros(4, 3, CV_8U).size().width << "\n";

  cout << cv::Mat::zeros(4, 3, CV_8U).t() << "\n";

  cout << cv::Mat::zeros(4, 3, CV_8U) + 5 << "\n";

  cout << ( cv::Mat::zeros(4, 3, CV_8U) + 5 ) * 3 << "\n";

  cout << 1.0 / ( cv::Mat::zeros(4, 3, CV_32F) ) << "\n";

  float f[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cout << 1.0 / cv::Mat(3, 4, CV_32F, f) << "\n";

  unsigned char v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 
  cv::Mat mat01 = cv::Mat(3, 4, CV_8U, v) == 4;
  cout << mat01 << "\n";

  unsigned char v03[] = { 3, 5, 2, 9, 11, 1, 8, 6, 7, 12, 4, 10 }; 
  cv::Mat mat03 = cv::Mat(3, 4, CV_8U, v03); 
  cv::Mat mat04, mat05; 
  cv::sort( mat03, mat04, CV_SORT_ASCENDING );
  cv::sort( mat03, mat05, CV_SORT_DESCENDING );
  cout << mat03 << "\n";
  cout << mat04 << "\n";
  cout << mat05 << "\n";
  
  float f06[] = { 1, 2, 3, 4 }; 
  cv::Mat mat06 = cv::Mat(2, 2, CV_32F, f06); 
  float f07[] = { 5, 6, 7, 8 }; 
  cv::Mat mat07 = cv::Mat(2, 2, CV_32F, f07); 
  cv::Mat mat08 = mat06 * mat07; 
  cout << mat06 << "\n";
  cout << mat07 << "\n";
  cout << mat08 << "\n";

  float f10[] = { 1, 2, 3, 4 }; 
  cv::Mat mat10 = cv::Mat(2, 2, CV_32F, f10); 
  float f11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat mat11 = cv::Mat(3, 3, CV_32F, f11); 
  cv::Mat mat12 = mat10.inv( DECOMP_LU );
  cv::Mat mat13 = mat11.inv( DECOMP_LU );
  cout << mat10 << "\n";
  cout << mat11 << "\n";
  cout << mat12 << "\n";
  cout << mat13 << "\n";

  cv::Mat mat14 = mat10.inv( DECOMP_SVD );
  cv::Mat mat15 = mat11.inv( DECOMP_SVD );
  cout << mat14 << "\n";
  cout << mat15 << "\n";

  cv::Mat mat16, mat17; 
  cv::eigen( mat10, mat16, mat17 );
  cv::Mat mat18, mat19; 
  cv::eigen( mat11, mat18, mat19 );
  cout << mat16 << "\n";
  cout << mat17 << "\n";
  cout << mat18 << "\n";
  cout << mat19 << "\n";

  double det10 = cv::determinant( mat10 ); 
  double det11 = cv::determinant( mat11 ); 
  cout << det10 << "\n";
  cout << det11 << "\n";

  cv::Scalar t10 = cv::trace( mat10 ); 
  cv::Scalar t11 = cv::trace( mat11 ); 

  cv::SVD svd10( mat10 ); 
  cv::SVD svd11( mat11 ); 
  cout << svd10.u << "\n";
  cout << svd10.w << "\n";
  cout << svd10.vt << "\n";
  cout << svd11.u << "\n";
  cout << svd11.w << "\n";
  cout << svd11.vt << "\n";

  float x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  cv::Mat X = cv::Mat(3, 3, CV_32F, f10); 
  float b[] = { 1, 2, 3, 4 }; 
  cv::Mat B = cv::Mat(2, 2, CV_32F, f10); 
  cv::Mat Y; 
  cv::filter2D(X, Y, -1, B, Point(-1,-1), 0.0, BORDER_DEFAULT);

  float f20[] = {11, 12, 13, 14, 15, 16, 19, 18, 17};
  cv::Mat mat20 = cv::Mat(2, 2, CV_32F, f20); 
  float f21[] = {21, 23, 22, 26, 25, 24, 28, 29, 27};
  cv::Mat mat21 = cv::Mat(3, 3, CV_32F, f21); 
  cv::Mat covar, mean; 
  cv::calcCovarMatrix(mat20, covar, mean, CV_COVAR_SCRAMBLED ); 

  waitKey(0);

  return 0;
}

◆ ビルド操作の例 ※ 上と同じ

g++ -o a.out hoge.cc -I/usr/local/include -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_highgui -lopencv_imgproc -lopencv_core

◆ 実行結果の例 (書きかけ)


  • 除算: \

    同じサイズの2つの行列やベクトルに対して、 要素ごとの除算を行う。

      float f09[] = { 1, 2, 3, 4 }; 
      cv::Mat mat09 = cv::Mat(2, 2, CV_32F, f09); 
      float f10[] = { 5, 6, 7, 8 }; 
      cv::Mat mat10 = cv::Mat(2, 2, CV_32F, f10); 
      cv::Mat mat11 = mat09 / mat10;