# 一、项目背景

left - 人脸区域离左边界的距离

top - 人脸区域离上边界的距离

width - 人脸区域的宽度

height - 人脸区域的高度

ratation 人脸框相对于竖直方向的顺时针旋转角[-180, 180].

# 二、如何旋转

x’ = x1 + r * cos(a + b);

x’ = x1 + r * cos(a) * cos(b) - r * sin(a) * sin(b);

r * cos(b) = x2 - x1;

r * sin(b) = (y2 - y1);

x’ = x1 + cos(a) * (x2 - x1) - sin(a) * (y2 - y1);

y’ = y1 + sin(a) * (x2 - x1) - cos(a) * (y2 - y1);

# 三、源码

```/*
* @author：xcywt
* @date：2018-08-10
* @contact me: https://www.cnblogs.com/xcywt/
*/
#include<iostream>
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
#define PI 3.14159265

#define ROTATE_COUNT 180
int RotateTest2()
{
if (ROTATE_COUNT > 360)
{
return -1;
}
int x1 = 200, y1 = 200;
int x2 = 300, y2 = 200;
int x3 = 300, y3 = 300;
int x4 = 200, y4 = 300;

int arrX1[ROTATE_COUNT], arrY1[ROTATE_COUNT];
int arrX2[ROTATE_COUNT], arrY2[ROTATE_COUNT];
int arrX3[ROTATE_COUNT], arrY3[ROTATE_COUNT];
int arrX4[ROTATE_COUNT], arrY4[ROTATE_COUNT];

int nAgree = 0;
for (int i = 0; i < ROTATE_COUNT; i++)
{
nAgree = i * (360 / ROTATE_COUNT);
double dRot = nAgree * PI / 180;
double dSinRot = sin(dRot), dCosRot = cos(dRot);

arrX1[i] = x1;
arrY1[i] = y1;

arrX2[i] = x1 + dCosRot * (x2 - x1) - dSinRot * (y2 - y1);
arrY2[i] = y1 + dSinRot * (x2 - x1) + dCosRot * (y2 - y1);

arrX3[i] = x1 + dCosRot * (x3 - x1) - dSinRot * (y3 - y1);
arrY3[i] = y1 + dSinRot * (x3 - x1) + dCosRot * (y3 - y1);

arrX4[i] = x1 + dCosRot * (x4 - x1) - dSinRot * (y4 - y1);
arrY4[i] = y1 + dSinRot * (x4 - x1) + dCosRot * (y4 - y1);
}

Mat im(800, 480, CV_8UC3);
line(im, Point(x1, y1), Point(x2, y2), Scalar(89, 90, 90), 3);
line(im, Point(x1, y1), Point(x4, y4), Scalar(89, 90, 90), 3);
line(im, Point(x3, y3), Point(x2, y2), Scalar(89, 90, 90), 3);
line(im, Point(x4, y4), Point(x3, y3), Scalar(89, 90, 90), 3);

for (int i = 1; i < ROTATE_COUNT; i++)
{
line(im, Point(arrX1[i], arrY1[i]), Point(arrX2[i], arrY2[i]), Scalar(189, 255, 0), 1);
line(im, Point(arrX1[i], arrY1[i]), Point(arrX4[i], arrY4[i]), Scalar(189, 255, 0), 1);
line(im, Point(arrX3[i], arrY3[i]), Point(arrX2[i], arrY2[i]), Scalar(189, 255, 0), 1);
line(im, Point(arrX4[i], arrY4[i]), Point(arrX3[i], arrY3[i]), Scalar(189, 255, 0), 1);
}
imshow("Is ok", im);
cvWaitKey(0);

return 0;
}

int RotateTest()
{
int nAgree = 170;
double dRot = nAgree * PI / 180;
double dSinRot = sin(dRot), dCosRot = cos(dRot);

int x1 = 200, y1 = 200;
int x2 = 300, y2 = 200;
int x3 = 300, y3 = 300;
int x4 = 200, y4 = 300;

int x1_1 = x1, y1_1 = y1;
int x2_1 = x1 + dCosRot * (x2 - x1) - dSinRot * (y2 - y1), y2_1 = y1 + dSinRot * (x2 - x1) + dCosRot * (y2 - y1);
int x3_1 = x1 + dCosRot * (x3 - x1) - dSinRot * (y3 - y1), y3_1 = y1 + dSinRot * (x3 - x1) + dCosRot * (y3 - y1);
int x4_1 = x1 + dCosRot * (x4 - x1) - dSinRot * (y4 - y1), y4_1 = y1 + dSinRot * (x4 - x1) + dCosRot * (y4 - y1);

std::cout << "P1:(" << x1 << " , " << y1 << ") --> (" << x1_1 << ", " << y1_1 << ")" << std::endl;
std::cout << "P2:(" << x2 << " , " << y2 << ") --> (" << x2_1 << ", " << y2_1 << ")" << std::endl;
std::cout << "P3:(" << x3 << " , " << y3 << ") --> (" << x3_1 << ", " << y3_1 << ")" << std::endl;
std::cout << "P4:(" << x4 << " , " << y4 << ") --> (" << x4_1 << ", " << y4_1 << ")" << std::endl;

Mat im(800, 480, CV_8UC3);
line(im, Point(x1, y1), Point(x2, y2), Scalar(89, 90, 90), 2);
line(im, Point(x1, y1), Point(x4, y4), Scalar(89, 90, 90), 2);
line(im, Point(x3, y3), Point(x2, y2), Scalar(89, 90, 90), 2);
line(im, Point(x4, y4), Point(x3, y3), Scalar(89, 90, 90), 2);

line(im, Point(x1_1, y1_1), Point(x2_1, y2_1), Scalar(189, 0, 0), 2);
line(im, Point(x1_1, y1_1), Point(x4_1, y4_1), Scalar(189, 0, 0), 2);
line(im, Point(x3_1, y3_1), Point(x2_1, y2_1), Scalar(189, 0, 0), 2);
line(im, Point(x4_1, y4_1), Point(x3_1, y3_1), Scalar(189, 0, 0), 2);

imshow("Is ok", im);
cvWaitKey(0);
return 0;

}

int main()
{
//RotateTest();
RotateTest2();
return 0;
}```

ROTATE_COUNT为10时：

ROTATE_COUNT为60时：

ROTATE_COUNT为180时：

ROTATE_COUNT为360时：

# 四、总结

70 篇文章36 人订阅

0 条评论

## 相关文章

931

5298

5593

5769

4055

2273

4K10

### 【论文推荐】最新5篇网络节点表示（Network Embedding）相关论文—高阶网络、矩阵分解、多视角、虚拟网络、云计算

【导读】专知内容组整理了最近五篇网络节点表示（Network Embedding）相关文章，为大家进行介绍，欢迎查看! 1. HONE: Higher-Orde...

1.6K7

### 【论文推荐】最新六篇图像描述生成相关论文—视频摘要、注意力张量积、非自回归神经序列模型、副词识别、多主体、多样性度量

【导读】专知内容组整理了最近六篇图像描述生成（Image Caption）相关文章，为大家进行介绍，欢迎查看! 1. Textually Customized ...

3977

3859