怎么做到仿射变换(Afine Transformations)——倾斜?
- 使用仿射变换,输出(1)那样的x轴倾斜30度的图像(t_x=30),这种变换被称为X-sharing。
- 使用仿射变换,输出(2)那样的y轴倾斜30度的图像(t_y=30),这种变换被称为Y-sharing。
- 使用仿射变换,输出(3)那样的x轴、y轴都倾斜30度的图像(t_x = 30,t_y = 30)。
原图像的大小为h\ w,使用下面各式进行仿射变换:
- X-sharing
a=\frac{t_x}{h}\
\left[
\begin{matrix}
x’\\
y’\\
1
\end{matrix}
\right]=\left[
\begin{matrix}
1&a&t_x\\
0&1&t_y\\
0&0&1
\end{matrix}
\right]\
\left[
\begin{matrix}
x\\
y\\
1
\end{matrix}
\right] -
Y-sharing
a=\frac{t_y}{w}\
\left[
\begin{matrix}
x’\\
y’\\
1
\end{matrix}
\right]=\left[
\begin{matrix}
1&0&t_x\\
a&1&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, dx=30, dy=30):
# get shape
H, W, C = img.shape
# Affine hyper parameters
a = 1.
b = dx / H
c = dy / W
d = 1.
tx = 0.
ty = 0.
# prepare temporary
_img = np.zeros((H+2, W+2, C), dtype=np.float32)
# insert image to center of temporary
_img[1:H+1, 1:W+1] = img
# prepare affine image temporary
H_new = np.ceil(dy + H).astype(np.int)
W_new = np.ceil(dx + W).astype(np.int)
out = np.zeros((H_new, W_new, C), dtype=np.float32)
# preprare assigned index
x_new = np.tile(np.arange(W_new), (H_new, 1))
y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)
# prepare inverse matrix for 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
x = np.minimum(np.maximum(x, 0), W+1).astype(np.int)
y = np.minimum(np.maximum(y, 0), H+1).astype(np.int)
# assign value from original to affine image
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
out = affine(img, dx=30, dy=30)
# 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, double dx, double dy){
// 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 (dx != 0){
b = dx / height;
}
if (dy != 0){
c = dy / width;
}
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 + dx);
int resized_height = (int)(height * d + dy);
if (theta != 0) {
resized_width = (int)(width + dx);
resized_height = (int)(height + dy);
}
// other parameters
int x_before, y_before;
// 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, 0, 30, 30);
//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
输入:
输出1:
输出2:
输出3: