컴퓨터 비전

OpenCV :: Mat Class (1)

Jooyoung Lee 2023. 1. 10. 19:10
행렬을 표현하는 클래스, 2차원 행렬 및 고차원 행렬까지 표현 할 수 있다.
정수, 실수, 복소수 등으로 구성된 행렬 또는 벡터를 저장하거나 그레이 스케일과 컬러 영상도 저장할 수 있다.
2차원 영상 데이터를 저장하고 처리하는 용도로 가장 많이 쓰이고 있음.

 

멤버 변수

  • Mat::dims : 행렬의 차원
  • Mat::cols : 열 개수 (가로 픽셀 크기)
  • Mat::rows : 행 개수 (세로 픽셀 크기)
  • Mat::size : 3차원 이상 행렬의 크기 정보
  • Mat::data : 행렬의 원소 데이터가 저장된 메모리 공간을 가리키는 포인터형 멤버 변수

 

자료형 매크로 상수

  • CV_8U : uchar, usigned char
  • CV_8S : schar, signed char
  • CV_16U : ushort, unsigned short
  • CV_16S : signed short
  • CV_32S : int
  • CV_32F : float
  • CV_64F : double
  • CV_16F : float16_t

행렬 생성과 초기화

Mat::Mat(int rows, int cols, int type);   // 정의

Mat img2 (480, 640, CV_8UC1); // unsigned char, 1-channel
Mat img3 (480, 640, CV_8UC3); // unsigned char, 3-channels
  • rows : 새로 만들 행렬의 행 개수 (영상의 세로 크기)
  • cols  : 새로 만들 행렬의 열 개수 (영상의 가로 크기)
  • type  : 새로 만들 행렬의 타입
Mat::Mat(Size size, int type);

Mat img4(Size(640,480), CV_8UC3);   // Size(width, height)
  • size : 새로 만들 행렬의 크기, Size(cols, rows) or Size(width, height)
  • type : 새로 만들 행렬의 타입
Mat::Mat(int rows, int cols, int type, const Scalar& s);
Mat::Mat(Size size, int type, const Scalar& s);

Mat img5(480, 640, CV_8UC1, Scalar(128));		// initial values, 128
Mat img6(480, 640, CV_8UC3, Scalar(0, 0, 255));		// initial values, red
  • rows : 새로 만들 행렬의 행 개수 (영상의 세로 크기)
  • cols  : 새로 만들 행렬의 열 개수 (영상의 가로 크기)
  • size  : 새로 만들 행렬의 크기
  • type  : 새로 만들 행렬의 타입
  • s       : 행렬 원소 초기값 
/* 반환값 : 모든 원소가 0으로 초기화 된 행렬 표현식 */

static MatExpr Mat::zeros(int rows, int cols, int type);
static MatExpr Mat::zeros(Size size, int type);

Mat mat1 = Mat::zeros(3, 3, CV_32SC1);		// 0's matrix

/* 반환값 : 모든 원소가 1으로 초기화 된 행렬 표현식 */

static MatExpr Mat::ones(int rows, int cols, int type);
static MatExpr Mat::ones(Size size, int type);

Mat mat1 = Mat::ones(3, 3, CV_32FC1);		// 1's matrix

/* 반환값 : 단위 행렬을 표현하는 표현식 */

static MatExpr Mat::eye(int rows, int cols, int type);
static MatExpr Mat::eye(Size size, int type);

Mat mat1 = Mat::eye(3, 3, CV_32FC1);		// identity matrix

 

외부 메모리 공간의 주소를 지정하는 클래스 

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
Mat::Mat(Size size, int type, 


float data[] = { 1, 2, 3, 4, 5, 6 };

Mat mat4(2, 3, CV_32FC1, data);
Mat mat5 = (Mat_<float>(2, 3) << 1, 2, 3, 4, 5, 6);
Mat mat6 = Mat_<uchar>({2, 3}, { 1, 2, 3, 4, 5, 6 });

$$ mat4 = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$

$$ mat5 = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$

$$ mat6 = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$

 

행렬 생성 및 초기화 클래스

void Mat::create(int rows, int cols, int type);
void Mat::create(Size size, int type);

mat4.create(256, 256, CV_8UC3);	// uchar, 3-channels
mat5.create(4, 4, CV_32FC1);	// float, 1-channel
Mat& Mat::operator = (const Scalar& s);
// s : 행렬 원소에 설정할 값
// RETURN : 값이 설정된 Mat 객체 참조

Mat& Mat::setTo(InputArray value, InputArray mask = no array());
// Value  : 행렬 원소에 설정할 값
// mask   : 마스크 행렬의 원소가 0이 안인 위치에서만 value 값이 설정됨.
// RETURN : Mat 객체의 참조

mat4 = Scalar(255, 0, 0);
mat5.setTo(1.f);