반응형

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(20050));
    rotation_source.push_back(Point(40050));
    rotation_source.push_back(Point(400250));
    rotation_source.push_back(Point(200250));
    // 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(200501));
    translation_source.push_back(Point3i(400501));
    translation_source.push_back(Point3i(4002501));
    translation_source.push_back(Point3i(2002501));
    // Translation Matrix
    Mat t = Mat::eye(33, CV_8UC1);
    t.at<uchar>(02= 20// x-axis 20 pixels translation
    t.at<uchar>(12= 60// y-axis 60 pixels translation
    t.at<uchar>(22= 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(400500, CV_8UC3, Scalar(255255255));
    for (int i = 0; i < 4; i++)
    {
        line(image, rotation_source[i], rotation_source[(i + 1) % 4], Scalar(000), 2);
        line(image, rotation_destination[i], rotation_destination[(i + 1) % 4], Scalar(25500), 1);
        line(image, translation_destination[i], translation_destination[(i + 1) % 4], Scalar(00255), 1);
    }
 
    imshow("image", image);
    waitKey(0);
 
    return 0;
}
cs





반응형
Posted by J-sean
: