How to make and use a roation matrix and a translation matrix - OpenCV 회전 행렬, 이동 행렬 만들기
OpenCV 2019. 8. 13. 16:03 |반응형
Explains how to make and use a rotation matrix and a translation matrix with OpenCV. Below code shows how to rotate 20 degrees and translate 20 pixels along the x-axis and 60 pixels along the y-axis.
회전 행렬을 이용해 20도 회전(CW), 이동 행렬을 이용해 x축으로 20 pixel, y축으로 60 pixel 이동하는 방법입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char* argv[]) { vector<Point> rotation_source, rotation_destination; rotation_source.push_back(Point(200, 50)); rotation_source.push_back(Point(400, 50)); rotation_source.push_back(Point(400, 250)); rotation_source.push_back(Point(200, 250)); // Rotation Matrix float theta = 20 * (float)CV_PI / 180; // 20 degrees rotation Matx22f r(cos(theta), -sin(theta), sin(theta), cos(theta)); transform(rotation_source, rotation_destination, r); vector<Point3i> translation_source, translation; translation_source.push_back(Point3i(200, 50, 1)); translation_source.push_back(Point3i(400, 50, 1)); translation_source.push_back(Point3i(400, 250, 1)); translation_source.push_back(Point3i(200, 250, 1)); // Translation Matrix Mat t = Mat::eye(3, 3, CV_8UC1); t.at<uchar>(0, 2) = 20; // x-axis 20 pixels translation t.at<uchar>(1, 2) = 60; // y-axis 60 pixels translation t.at<uchar>(2, 2) = 0; transform(translation_source, translation, t); vector<Point> translation_destination; for (int i = 0; i < translation.size(); i++) translation_destination.push_back(Point(translation[i].x, translation[i].y)); // Draw source, rotation, translation Mat image(400, 500, CV_8UC3, Scalar(255, 255, 255)); for (int i = 0; i < 4; i++) { line(image, rotation_source[i], rotation_source[(i + 1) % 4], Scalar(0, 0, 0), 2); line(image, rotation_destination[i], rotation_destination[(i + 1) % 4], Scalar(255, 0, 0), 1); line(image, translation_destination[i], translation_destination[(i + 1) % 4], Scalar(0, 0, 255), 1); } imshow("image", image); waitKey(0); return 0; } | cs |
반응형
'OpenCV' 카테고리의 다른 글
Simple motion detection with OpenCV - 간단한 움직임 감지 (2) | 2019.09.15 |
---|---|
3 ways to iterate through the matrix and comparison - OpenCV 행렬 원소를 모두 참조하는 세 가지 방법 (0) | 2019.08.18 |
Measuring execution time of a function - OpenCV에서 실행 시간 측정 하기 (0) | 2019.07.14 |
Random Number Generator(RNG) - OpenCV 난수 발생기 사용 (0) | 2019.07.13 |
cv::String class usage examples - OpenCV 문자열 클래스 사용 예제 (0) | 2019.07.13 |