OpenCVSharp4のMat行列の基本演算

自分へのメモも兼ねて基本演算をメモしておく

プログラム

using System;  
using OpenCvSharp;  

namespace matの演算を試してみる {  
    class Program {  
        static void Main(string[] args) {  

            //計算用のmat1とmat2を定義  
            Mat mat1 = new Mat(3, 3, MatType.CV_64FC1,  
                new double[,] {  
                    {1,2,3 },  
                    {4,5,6 },  
                    {7,8,9 }  
                }  
                );  

            Mat mat2 = new Mat(3, 3, MatType.CV_64FC1,  
                new double[,] {  
                    {2,9,4 },  
                    {7,5,3 },  
                    {6,1,8 }  
                 }  
                );  

            double num = 3;  

            //行列と行列の演算  
            Mat matA = mat1 + mat2;    //行列の和  
            Mat matB = mat1 - mat2;    //行列の差  
            Mat matC = mat1 * mat2;    //行列の積  
            Mat matD = mat1.Mul(mat2); //要素の積  
            Mat matL = new mat();  
            cv2.Divide(mat1, mat2, matL);//要素の除算  


            //行列とスカラーの演算  
            Mat matE = mat1 + num;//要素の和(順序はどちらでもよい)  
            Mat matF = mat1 - num;//要素の差  
            Mat matG = mat1 * num;//スカラー倍(順序はどちらでもよい)  
            Mat matH = mat1 / num;//スカラーで割る  
            Mat matI = num / mat1;//要素を逆数にしてスカラー倍  

            //行列の単項演算  
            double a = Cv2.Determinant(mat2); //行列式  
            double b = Cv2.Norm(mat2);       //L-2ノルム  
            Mat matJ = mat2.T();              //転置  
            Mat matK = mat2.Inv();            //逆行列(SVD)  



            Console.WriteLine("mat1 + mat2=");    Console.WriteLine(Cv2.Format(matA)); Console.WriteLine();  
            Console.WriteLine("mat1 - mat2=");    Console.WriteLine(Cv2.Format(matB)); Console.WriteLine();  
            Console.WriteLine("mat1 * mat2=");    Console.WriteLine(Cv2.Format(matC)); Console.WriteLine();  
            Console.WriteLine("mat1.Mul(mat2)="); Console.WriteLine(Cv2.Format(matD)); Console.WriteLine();  

            Console.WriteLine("mat1 + num=");   Console.WriteLine(Cv2.Format(matE)); Console.WriteLine();  
            Console.WriteLine("mat1 - num=");     Console.WriteLine(Cv2.Format(matF)); Console.WriteLine();  
            Console.WriteLine("mat1 * num=");     Console.WriteLine(Cv2.Format(matG)); Console.WriteLine();  
            Console.WriteLine("mat1 / num=");     Console.WriteLine(Cv2.Format(matH)); Console.WriteLine();  
            Console.WriteLine("num / mat1=");     Console.WriteLine(Cv2.Format(matI)); Console.WriteLine();  

            Console.WriteLine("Cv2.Determinant(mat2)="); Console.WriteLine(a);                Console.WriteLine();  
            Console.WriteLine("Cv2.Norm(mat2)");         Console.WriteLine(b);                Console.WriteLine();  
            Console.WriteLine("mat2.T=");                Console.WriteLine(Cv2.Format(matJ)); Console.WriteLine();  
            Console.WriteLine("mat2.Inv()=");            Console.WriteLine(Cv2.Format(matK)); Console.WriteLine();  


            Cv2.WaitKey();  
        }  
    }  
}  

結果

mat1 + mat2=  
[3, 11, 7;  
 11, 10, 9;  
 13, 9, 17]  

mat1 - mat2=  
[-1, -7, -1;  
 -3, 0, 3;  
 1, 7, 1]  

mat1 * mat2=  
[34, 22, 34;  
 79, 67, 79;  
 124, 112, 124]  

mat1.Mul(mat2)=  
[2, 18, 12;  
 28, 25, 18;  
 42, 8, 72]  

mat1 + num=  
[4, 5, 6;  
 7, 8, 9;  
 10, 11, 12]  

mat1 - num=  
[-2, -1, 0;  
 1, 2, 3;  
 4, 5, 6]  

mat1 * num=  
[3, 6, 9;  
 12, 15, 18;  
 21, 24, 27]  

mat1 / num=  
[0.3333333333333333, 0.6666666666666666, 1;  
 1.333333333333333, 1.666666666666667, 2;  
 2.333333333333333, 2.666666666666667, 3]  

num / mat1=  
[3, 1.5, 1;  
 0.75, 0.6, 0.5;  
 0.4285714285714285, 0.375, 0.3333333333333333]  

Cv2.Determinant(mat2)=  
-360  

Cv2.Norm(mat2)  
16.881943016134134  

mat2.T=  
[2, 7, 6;  
 9, 5, 1;  
 4, 3, 8]  

mat2.Inv()=  
[-0.1027777777777778, 0.1888888888888889, -0.01944444444444444;  
 0.1055555555555556, 0.02222222222222222, -0.06111111111111112;  
 0.0638888888888889, -0.1444444444444445, 0.1472222222222222]  
Console

#参考
http://opencv.jp/cookbook/opencv_mat.html