-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cc
More file actions
77 lines (68 loc) · 2.16 KB
/
Copy pathmain.cc
File metadata and controls
77 lines (68 loc) · 2.16 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Geometry>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "unit.h"
std::string img_path = "test.png";
std::string output = "rota_test.png";
Eigen::Matrix2d rotate_matrix(const double &angle) {
Eigen::Matrix2d R;
R << cos(angle), -sin(angle), sin(angle), cos(angle);
return R;
}
void rotate_img(const cv::Mat &src, cv::Mat &dst, double angle,
const double &zoom) {
angle -= static_cast<int>(angle / PI) * PI;
Eigen::Matrix2d R = rotate_matrix(angle) * zoom;
std::cout << "R:" << std::endl;
std::cout << R << std::endl;
Eigen::Matrix3d T = Eigen::Matrix3d::Zero();
Eigen::Vector3d center = Eigen::Vector3d::Zero();
T.block<2, 2>(0, 0) = R;
T(2, 2) = 1;
std::cout << "T:" << std::endl;
std::cout << T << std::endl;
// 获取原图像中心点坐标
center << src.rows / 2, src.cols / 2, 1;
// 获取从dst到src的T
Eigen::Matrix3d TT = T.inverse();
TT.block<3, 1>(0, 2) = center;
std::cout << "TT:" << std::endl;
std::cout << TT << std::endl;
dst = cv::Mat(src.size(), src.type());
auto func = [&src](Eigen::Vector3d pose) -> uint8_t {
int x = static_cast<int>(pose[0]), y = static_cast<int>(pose[1]);
if (x < 0 || y < 0 || x > src.rows || y > src.cols) {
return 0;
}
return src.at<uint8_t>(x, y);
};
// 不好算范围,所以就不改变图像框大小了,(方案1)
for (int i = 0; i < src.rows; i++) {
for (int j = 0; j < src.cols; j++) {
Eigen::Vector3d pose = Eigen::Vector3d::Zero();
pose << i - src.rows / 2, j - src.cols / 2, 1;
dst.at<uint8_t>(i, j) = func(TT * pose);
}
}
}
int main(int argc, char **argv) {
if (argc != 4) {
std::cout << "Usage : main {imag_path} {output_path} {rotate_angle}" << std::endl;
exit(-1);
}
img_path = std::string(argv[1]);
output = std::string(argv[2]);
double angle = std::stod(argv[3]);
cv::Mat img = cv::imread(img_path);
cv::Mat gray;
MY_IMG::Rgb2Gray(img, gray);
std::cout << "gray img" << std::endl;
cv::Mat dimg;
rotate_img(gray, dimg, angle, 5);
cv::imshow("dimg", dimg);
cv::waitKey(0);
cv::imwrite(output, dimg);
return 0;
}