행렬의 생성과 초기화
다음은 Mat 클래스 정의 중 생성과 초기화의 일부분 코드이다.
class Mat {
public:
Mat(); // 1)
Mat(int rows, int cols, int type); // 2)
Mat(Size size, int type); // 3)
Mat(int rows, int cols, int type, const Scalar& s); // 4)
Mat(Size size, int type, const Scalar& s); // 5)
Mat(int rows, int cols, int type, void* data, size_t step = AUTO_STEP);
Mat(Size size, int type, void* data, size_t step = AUTO_STEP); // 6)
static MatExpr zeros(int rows, int cols, int type);
static MatExpr zeros(Size size, int type); // 7)
static MatExpr ones(int rows, int cols, int type);
static MatExpr ones(Size size, int type); // 8)
static MatExpr eye(int rows, int cols, int type);
static MatExpr eye(Size size, int type); // 9)
void Mat::create(int rows, int cols, int type);
void Mat::create(Size size, int type); // 10)
Mat& Mat::operator=(const Scalar& s); // 11)
Mat& Mat::setTo(InputArray value, InputArray mask = noArray()); // 12)
template<typename _Tp> _Tp* ptr(int i0=0);
template<typename _Tp> _Tp& at(int row, int col); // 13)
};
생성자
1) 객체를 비어있는 행렬로 생성하여 멤버 변수인 rows, cols 값은 0, data 값은 null로 저장하는 기본 생성자이다.
2) 객체를 생성함과 동시에 공간을 할당하는 방식으로, 인자는 행 개수, 열 개수, 타입 순으로 입력받아 저장하는 생성자
3) 2)와 비슷한 방식으로 행렬의 크기를 Size 클래스를 대신 입력받는 생성자이다.
4,5) 객체를 생성함과 동시에 모든 원소 값까지 초기화하는 방식의 생성자이다.
// Mat 객체는 생성할 때 행렬의 모든 원소 값을 특정 값으로 초기화하는 것이 안전하다.
// Scalar 클래스로 컬러 영상을 색상을 지정할 때는 B, G, R 순으로 입력한다.
6) 행렬 원소 값을 새로 할당하는 것이 아니라 기존에 있던 메모리 공간의 데이터를 참조하는 방식으로 객체를 생성하는 방식의 생성자이다. 입력은 다른 생성자와 비슷하고 data에는 행렬 원소 값 주소이고, size_t에는 한 행이 차지하는 바이트 수이고 패딩 바이트가 있다면 명시적으로 지정해야 하며, 기본 값은 AUTO_STEP으로 패딩 바이트가 없다고 간주한다.
행렬 원소 값 초기화
7, 8, 9) 순서대로 모든 값을 0으로 초기화(zeros), 1로 초기화(ones) 그리고 단위행렬로 초기화(eye)하고 사용 방식은 세 함수 모두 같다.
10) 입력 받은 인자가 행렬 크기와 타입이 다른 경우 동작하는데, 기존 메모리 공간을 해제하고 새로운 행렬 데이터 공간을 할당한다.
11, 12) create 함수에는 초기화 기능이 없어 초기화를 하려면 = 연산자나 setTo() 멤버 함수를 사용해야 한다. setTo 함수의 두 가지 인자 중에 mask 같은 경우 특정 영역의 원소 값을 설정할 때 사용하여 평소에는 생략해도 상관없다.
13) Mat 클래스를 상속하여 만든 Mat_를 사용하여 행렬 원소 값을 설정한다.
// 13) 예시
// 첫 번째 방법
Mat <float> mat1_(2,3);
mat1_ << 1, 2, 3, 4, 5, 6;
Mat mat1 = mat1_;
// 두 번째 방법
Mat mat2 = (Mat_<float>(2,3) <<1, 2, 3, 4, 5, 6);
// 세 번째 방법, C++11의 초기화 리스트 이용
Mat mat3 = Mat_<float>({2,3}, {1, 2, 3, 4, 5, 6});
모든 함수 사용 예시
int main(void) {
float data[] = { 1, 2, 3, 4, 5, 6 };
Mat img1();
Mat img2(480, 640, CV_8UC3);
Mat img3(Size(640, 480), CV_8UC3);
Mat img4(480, 640, CV_8UC3, Scalar(0, 0, 255));
Mat img5(Size(640, 480), CV_8UC3, Scalar(0));
Mat img6(2, 3, CV_32SC1, data);
Mat mat7 = Mat::zeros(3, 3, CV_32SC1);
Mat mat8 = Mat::ones(3, 3, CV_32SC1);
Mat mat9 = Mat::eye(3, 3, CV_32SC1);
mat7.create(256, 256, CV_8UC3);
Mat mat7 = Scalar(255, 0, 0);
mat7.setTo(1.f);
return 0;
}
더 자세한 설명은 openCV 문서 페이지에서 확인 가능합니다.
https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html
OpenCV: cv::Mat Class Reference
n-dimensional dense array class More... #include Mat () CV_NOEXCEPT Mat (int rows, int cols, int type) Mat (Size size, int type) Mat (int rows, int cols, int type, const Scalar &s) Mat (Size size, int type, const Scalar &s) Mat (
docs.opencv.org
본 내용은 < OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝(저자 : 황선규) > 내용을 참고하여 작성되었습니다.
https://book.naver.com/bookdb/book_detail.nhn?bid=14722394
'프로그래밍 > 컴퓨터 비전 with openCV' 카테고리의 다른 글
영상의 필터링 (0) | 2022.04.16 |
---|---|
Mat 클래스 ( 행렬의 복사와 부분 행렬 추출 ) (0) | 2022.03.21 |
Mat 클래스 ( 개요 - Mat 행렬의 깊이, 채널, 타입 ) (0) | 2022.03.17 |
Mat 클래스 ( 개요 - 특징과 멤버 변수 ) (0) | 2022.03.16 |