더북(TheBook)

3.2.7 행렬 연산

OpenCV에서 Mat 클래스는 영상을 표현하는 용도로 많이 사용되고 있지만, 일반적인 행렬 표현과 행렬 연산을 위한 기능도 충분히 제공하고 있습니다. 이 절에서는 Mat 클래스를 이용하여 기본적인 행렬 연산을 수행하는 방법과 역행렬, 전치 행렬 등을 구하는 방법을 예제 코드와 함께 알아보겠습니다.

먼저 행렬의 사칙 연산을 수행하는 방법부터 알아보겠습니다. OpenCV는 Mat 클래스가 표현하는 행렬을 마치 수학 수식을 쓰듯이 사용할 수 있도록 다양한 연산자 재정의 함수를 제공합니다. 이를 통하여 행렬과 행렬의 덧셈 또는 곱셈 연산을 수행할 수 있고, 행렬에 상수 값을 곱하는 연산을 수행할 수도 있습니다. 행렬 사칙 연산을 위한 주요 연산자 재정의 함수는 다음과 같습니다.

MatExpr operator + (const Mat& a, const Mat& b);
MatExpr operator + (const Mat& a, const Scalar& s);
MatExpr operator + (const Scalar& s, const Mat& a);
 
MatExpr operator - (const Mat& a, const Mat& b);
MatExpr operator - (const Mat& a, const Scalar& s);
MatExpr operator - (const Scalar& s, const Mat& a);
 
MatExpr operator - (const Mat& m);
 
MatExpr operator * (const Mat& a, const Mat& b);
MatExpr operator * (const Mat& a, double s);
MatExpr operator * (double s, const Mat& a);
 
MatExpr operator / (const Mat& a, const Mat& b);
MatExpr operator / (const Mat& a, double s);
MatExpr operator / (double s, const Mat& a);
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.