OpenCv Mat 클래스(3)
1.행렬의 원소값 참조
1. Mat::at()
(1) 행,열 나타내는 두t개의 정수를 인자로 받아 해당 위치의 행렬 원소값을 참조형식으로 반환
template<typename_TP> _TP& Mat::at(int y, int x)
2. Mat::ptr()
(1) Mat 행렬에서 특정 행의 첫 번째 원소 주소를 반환
template<typename_TP>
_TP* Mat::ptr(int y)
3. Matlterator_ 반복자
위의 1,2번 함수같은 경우 함수 인자로 전달된 값이 행렬의 크기르 벗어나면 에러가 발생.
이러한 단점을 해소하기위해 반복자 개념을 도입. 사용방법은 C++언어와 비슷
for (MatIterator_<uchar> it=mat1.begin<uchar>(); ir !=mat1.end<uchar>(); ++it) {
(*it)++;
}
2. 행렬 정보 참조하기
1. Mat 클래스 정보 참조를 위한 멤버 함수
int Mat::channels() const; 행렬의 채널 수 반환
int Mat::depth() const; 행렬의 깊이 반환
size_t Mat::elemSize() const; 한 개의 원소가 차지하는 메모리 크기를 바이트 단위로 반환
size_t Mat::elemSize1() const; 하나의 채널에서 한개의 원소가 차지하는 메모리 크기를 바이트 단위로 반환
bool Mat::empty() const; 비어있는 행렬이면 true를 반환
bool Mat::isContinuous() const; 각 행의 원소가 연속적으로 저장되어 있으면 true를 반환
bool Mat::isSubmatrix() const; 행렬이 다른 행렬의 부분행렬이면 true 반환
Size Mat::size() const; 행렬 크기를 Size 타입으로 반환
size_t Mat::total() const; 전체 원소 개수 반환
int Mat::type() const; 행렬의 타입 반환
2. <<연산자 재정의를 이용하여 행렬 원소 출력
Mat 객체에 int, float, double 같은 자료형의 행렬이 저장되어 있는 경우라면 imshow()함수는 적철지 못함.
static inline
std::ostream&operator << (std::ostream& out, const Mat&mtx)
3. 행렬의 연산
행렬의 사칙연산을 수행 할 수 있다.
mat3 = mat1 + mat 2
mat3 = mat1 - mat2
위와 같이 +, -, *, / 을 이용하여 사칙연산을 계산할 수 있다.
Mat::mul() - 두 행렬에서 같은 위치에 있는 원소끼리 곰셈 연산 수행
MatExpr Mat::mul(InputArray m, double scale=1) const;
Mat::inv() - 역행렬 구하는 연산 수행
MatExpr Mat::inv(int method=DECOMP_LU) const;
Mat::t() - 전치행렬 구하는 연산 수행
MatExpr Mat::t() const;
4. 크기 및 타입 변환 함수
Mat::converTo() - 행렬의 타입 변경
void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const;
m(x,y) = saturate_cast<rtype>(alphax(*this)(x,y)+beta)
Mat::reshape() - 행렬의 크기 또는 채널 수 변경
Mat Mat::reshape(int cn, int rows=0) const;
Mat::resize() - 행렬의 행의 크기 변환 (행의 갯수 sz개로 변경)
void Mat::resize(size_t sz);
void Mat::reszie(size_t sz, const Scalar& s);
Mat::push_back() - 존재하는 행렬에 원소 데이터(elem) 추가
template<typename _TP> void Mat::push_back(const _TP& elem);
template<typename _TP> void Mat::push_back(const Mat<_TP>& elem);
template<typename _TP> void Mat::push_back(const std::vector<_TP>& elem);
void Mat::push_back(const Mat& m);
Mat::pop_back() - 행렬에서 맨 아래에 있는 행 제거 (nelems=제거할 행 갯수)
Mat::pop_back(size_t nelems=1);