如何进行仿射变换( Afine Transformations )——旋转?
- 使用仿射变换,逆时针旋转30度。
- 使用仿射变换,逆时针旋转30度并且能让全部图像显现(也就是说,单纯地做仿射变换会让图片边缘丢失,这一步中要让图像的边缘不丢失,需要耗费一些工夫)。
使用下面的式子进行逆时针方向旋转A度的仿射变换:
\left(
\begin{matrix}
x’\\
y’\\
1
\end{matrix}
\right)=
\left(
\begin{matrix}
\cos(A)&-\sin(A)&t_x\\
\sin(A)&\cos(A)&t_y\\
0&0&1
\end{matrix}
\right)\
\left(
\begin{matrix}
x\\
y\\
1
\end{matrix}
\right)
python实现:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# affine
def affine(img, a, b, c, d, tx, ty):
H, W, C = _img.shape
# temporary image
img = np.zeros((H+2, W+2, C), dtype=np.float32)
img[1:H+1, 1:W+1] = _img
# get shape of new image
H_new = np.round(H).astype(np.int)
W_new = np.round(W).astype(np.int)
out = np.zeros((H_new, W_new, C), dtype=np.float32)
# get position of new image
x_new = np.tile(np.arange(W_new), (H_new, 1))
y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)
# get position of original image by affine
adbc = a * d - b * c
x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1
y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1
# adjust center by affine
dcx = (x.max() + x.min()) // 2 - W // 2
dcy = (y.max() + y.min()) // 2 - H // 2
x -= dcx
y -= dcy
x = np.clip(x, 0, W + 1)
y = np.clip(y, 0, H + 1)
# assign pixcel
out[y_new, x_new] = img[y, x]
out = out.astype(np.uint8)
return out
# Read image
_img = cv2.imread("imori.jpg").astype(np.float32)
# Affine
A = 30.
theta = - np.pi * A / 180.
out = affine(img, a=np.cos(theta), b=-np.sin(theta), c=np.sin(theta), d=np.cos(theta),
tx=0, ty=0)
# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("out.jpg", out)
c++实现:
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// affine
cv::Mat affine(cv::Mat img, double a, double b, double c, double d, double tx, double ty, double theta){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();
// get detriment
double det = a * d - b * c;
if (theta != 0){
// Affine parameters
double rad = theta / 180. * M_PI;
a = std::cos(rad);
b = - std::sin(rad);
c = std::sin(rad);
d = std::cos(rad);
tx = 0;
ty = 0;
double det = a * d - b * c;
// center transition
double cx = width / 2.;
double cy = height / 2.;
double new_cx = (d * cx - b * cy) / det;
double new_cy = (- c * cx + a * cy) / det;
tx = new_cx - cx;
ty = new_cy - cy;
}
// Resize width and height
int resized_width = (int)(width * a);
int resized_height = (int)(height * d);
if (theta != 0) {
resized_width = (int)(width);
resized_height = (int)(height);
}
// other parameters
int x_before, y_before;
double dx, dy, wx, wy, w_sum;
double val;
int _x, _y;
// output
cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3);
// Affine transformation
for (int y = 0; y < resized_height; y++){
for (int x = 0; x < resized_width; x++){
// get original position x
x_before = (int)((d * x - b * y) / det - tx);
if ((x_before < 0) || (x_before >= width)){
continue;
}
// get original position y
y_before = (int)((-c * x + a * y) / det - ty);
if ((y_before < 0) || (y_before >= height)){
continue;
}
// assign pixel to new position
for (int c = 0; c < channel; c++){
out.at<cv::Vec3b>(y, x)[c] = img.at<cv::Vec3b>(y_before, x_before)[c];
}
}
}
return out;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// affine
cv::Mat out = affine(img, 1, 0, 0, 1, 0, 0, -30);
//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
输入:
输出1:
输出2: