void cv::warpAffine ( InputArray src,
OutputArray dst,
InputArray M,
Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar & borderValue = Scalar()
)
cv::Mat src = cv::imread("lena.jpg", 0);
cv::Mat dst;
cv::Mat warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(0), -sin(0), 200,
sin(0), cos(0), 0);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR);
cv::imshow("Shift along X-axis", dst);
cv::waitKey(0);
warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(0), -sin(0), 0,
sin(0), cos(0), 200);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR );
cv::imshow("Shift along Y-axis", dst);
cv::waitKey(0);
double alpha_45 = PI / 4; // 顺时针旋转 45°
double alpha_90 = PI / 2; // 顺时针旋转 90°
warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(alpha_45), -sin(alpha_45), 0,
sin(alpha_45), cos(alpha_45), 0);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR);
cv::imshow("Clockwise-45", dst);
cv::waitKey(0);
warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(alpha_90), -sin(alpha_90), 0,
sin(alpha_90), cos(alpha_90), 0);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR);
cv::imshow("Clockwise-90", dst);
cv::waitKey(0);
这里可以看到是围绕左上角的点进行的旋转
所以旋转 90° 的时候,就看不到图像了
warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(alpha_90), -sin(alpha_90), 200,
sin(alpha_90), cos(alpha_90), 0);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR);
cv::imshow("Clockwise-90 and shift", dst);
cv::waitKey(0);
这里先顺时针旋转了 90°,然后再向 X 轴的正向平移 200 个像素
cv::Point2f center(src.rows / 2., src.cols / 2.);
double angle = 45.0; // 这里正数代表逆时针旋转
warp_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
cv::warpAffine(src, dst, warp_matrix, cv::Size(src.cols, src.rows), cv::INTER_LINEAR);
cv::imshow("Counter-clockwise-45 around center", dst);
cv::waitKey(0);