.Net 교육/OpenCV(C++)
OpenCV(C++) - 행렬 연산 02
pi92
2021. 5. 21. 11:39
uchar data[] =
{
10,200,5,7,9,
15,35,60,80,170,
100,2,55,37,70
};
Mat m1(3, 5, CV_8U, data);
Mat m2(3, 5, CV_8U, Scalar(50));
cout << "[m1] = " << endl << m1 << endl;
cout << "[m2] = " << endl << m2 << endl;
Mat rMin;
min(m1, 30, rMin); //m1 행렬의 원소 중 30보다 큰 것들은 전부 '30'처리
cout << "[rMin]=" << endl << rMin << endl;
Mat rMax;
max(m1, m2, rMax); //m1의 원소와 m2의 원소를 비교하여 큰 원소가 반영됨
cout << "[rMax]=" << endl << rMax << endl;
double minVal, maxVal;
Point minLoc, maxLoc;
minMaxLoc(
m1,
&minVal,
&maxVal,
&minLoc,
&maxLoc
);
cout << minVal << endl; //m1 원소의 최소값
cout << maxVal << endl; //m1 원소의 최대값
cout << minLoc << endl; //
cout << maxLoc << endl;
void showMatInfo(Mat mat, string str)
{
cout << "채널 : " << mat.channels() << "\t\t";
cout << "행열 : " << mat.size << endl;
cout << str + " = " << endl << mat << endl;
cout << "--------------------------------------" << endl << endl;
}
int main(void)
{
Mat image = imread("image/minMax.jpg", IMREAD_GRAYSCALE);
double minVal, maxVal;
minMaxIdx(image, &minVal, &maxVal);
cout << "최소값 = " << minVal << endl;
cout << "최대값 = " << maxVal << endl;
double ratio = (maxVal - minVal) / 255;
cout << ratio << endl;
// 0.301961
Mat dest = (image - minVal) / ratio;
imshow("dest", dest);
imshow("image", image);
waitKey();
}
vector<Point> pt;
pt.push_back(Point(0, 1));
pt.push_back(Point(2, 3));
pt.push_back(Point(4, 5));
cout << pt << endl;
Matx22f m(
0, 1, // 0 >> 0*0 + 1*1 = 1
2, 3 // 1 >> 2*0 + 3*1 = 3
);
vector<Point> result;
transform(pt, result, m);
cout << result << endl;
int main(void)
{
vector<Point> rect_pt1, rect_pt2;
rect_pt1.push_back(Point(200, 50));
rect_pt1.push_back(Point(400, 50));
rect_pt1.push_back(Point(400, 250));
rect_pt1.push_back(Point(200, 250));
float theta = 20 * CV_PI / 180; // 라디안
Matx22f m(cos(theta), -sin(theta), sin(theta), cos(theta));
transform(rect_pt1, rect_pt2, m);
Mat image(400, 500, CV_8UC3, Scalar(255, 255, 255));
for (int i = 0; i < 4; i++)
{
line(image, rect_pt1[i], rect_pt1[(i + 1) % 4], Scalar(0, 0, 0), 1);
line(image, rect_pt2[i], rect_pt2[(i + 1) % 4], Scalar(255, 0, 0), 2);
cout << "rect_pt1[" << to_string(i) + "] = " << rect_pt1[i] << "\t";
cout << "rect_pt2[" << to_string(i) + "] = " << rect_pt2[i] << endl;
}
imshow("image", image);
waitKey();
return 0;
}