[python]OpenCV 공부노트 Mat4
에 이어서 resize, reserve, release 에 대한 함수 사용
#pragma warning(disable: 4819)
#include "opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat A(3, 3, CV_32FC1, Scalar::all(0));
cout << "A= " << A.size() << A << endl;
A.resize(2); // 행의 갯수를 2로 변경한다.
cout << "A= " << A.size() << A << endl;
A.resize(5, Scalar::all(6)); // 행의 갯수를 5로 변경한다.
cout << "A= " << A.size() << A << endl;
A.reserve(10); // 10행만큼 메모리 공간을 확보하지만 메모리 공간이 충분하면 작동하지않는다.
cout << "A= " << A.size() << A << endl;
A.release(); // A가 차지하는 메모리공간을 개방한다.
cout << "A= " << A.size() << A << endl;
// A의 공간은 개방되어서 없지만
A.create(3, 3, CV_32FC1); // create로 다시 (CV_32F 3x3사이즈로) 할당할 수 있음.
cout << "A= " << A.size() << A << endl;
return 0;
}

간단한 내용이므로 설명은 주석에 작성하였다.
관심영역 (Region of Interest)
#pragma warning(disable: 4819)
#include "opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat A(10, 10, CV_32F);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.cols; j++) {
A.at<float>(i, j) = i* A.cols + j;
}
}
cout << "A = " << A.size() << A << endl << endl;
Mat B = A(Range(5, 8), Range(3, 6)); // [5,8), [3,6) 3x3
cout << "B = " << B.size() << B << endl << endl;
Size wholeSize;
Point ofs;
B.locateROI(wholeSize, ofs);
// ofs = 원본(A)대비 B가 위치한 영역
// B는 A의 부분을 받았지만 wholeSize를 기억하고있다.
cout << "wholeSize= " << wholeSize << "ofs=" << ofs << endl << endl;
Mat C = B.adjustROI(1, 1, 1, 1);
cout << "B=" << B.size() << B << endl << endl;
cout << "C=" << C.size() << C << endl << endl;
return 0;
}

구버전 2.4의 CvMat() IplImage() 대신 cvarrToMat() 사용하기
#pragma warning(disable: 4819)
#include "opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat A(10, 10, CV_32F);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.cols; j++) {
A.at<float>(i, j) = i* A.cols + j;
}
}
cout << "A = " << A.size() << A << endl << endl;
Mat B = A(Range(5, 8), Range(3, 6)); // [5,8), [3,6) 3x3
cout << "B = " << B.size() << B << endl << endl;
Mat C = A(Rect(2, 2, 5, 5)); // x, y, w, h
cout << "C = " << C << endl;
Range ranges[2] = { Range(5,8) , Range(3,6) };
Mat D = A(ranges);
cout << "D=" << D << endl << endl;
CvMat E = D;
cout << "E= " << cvarrToMat(&E) << endl << endl;
IplImage F = D;
cout << "F= " << cvarrToMat(&F) << endl << endl;
return 0;
}

OpenCV 3.x에서는 IplImage나 CvMat의 생성자가 없기 떄문에(deprecated) 구조체를 변환하려면 cvarrToMat(&ref)를 사용해야한다.
Mat으로 Iterator사용하기
#pragma warning(disable: 4819)
#include "opencv.hpp"
#include <iomanip>
using namespace cv;
using namespace std;
int main()
{
Mat A(10, 10, CV_32F);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.cols; j++) {
A.at<float>(i, j) = i* A.cols + j;
}
}
cout << "A = " << A.size() << A << endl << endl;
cout << "sum(A) = " << sum(A) << endl;
float sum2 = 0;
MatConstIterator_<float> it = A.begin<float>(); // begin으로 iterator 객체 리턴
for (; it != A.end<float>(); it++) { // A.end()도 MatIter리턴
sum2 += *it;
}
cout.precision(3);
cout << fixed << sum2 << endl; // not using e
Mat B(10, 10, CV_32FC1);
MatConstIterator_<float> itA = A.begin<float>();
MatIterator_<float> itB = B.begin<float>();
for (; itA != A.end<float>(); itA++, itB++) {
*itB = *itA; //itB에 itA값을 대입
}
cout << "B= " << B << endl << endl;
cout << "===============================" << endl << endl;
A.setTo(Scalar::all(5));
cout << "A= " << A << endl << endl;
cout << "B= " << B << endl << endl;
return 0;
}

추가로 참조가아닌 값을 복사했기때문에 A의 값을 바꿔도 B의 값에는 변함이없다.
(*itB = *itA)
lena.jpg의 영상 평균 계산. iterator 활용 (모든 벡터 접근)
#pragma warning(disable: 4819)
#include "opencv.hpp"
#include <iomanip>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage = imread("lena.jpg", IMREAD_GRAYSCALE);
float sum = 0;
MatConstIterator_<uchar> it = srcImage.begin<uchar>();
for (; it != srcImage.end<uchar>(); it++) {
sum += *it;
}
cout.precision(3);
cout << srcImage.total() << endl << endl;
cout << fixed << sum / srcImage.total() << endl;
return 0;
}

grayscale이라 단색값으로 계산하는 것 같다. (명암만 가능한 듯)
방식을 바꾸어 컬러 계조 평균을 계산해보기로 했다…
이 부분을 해결하는것을 과제로 하자.
Mat 클래스의 끝이 보인다.
79개의 댓글