一、项目背景

最近在做一个人脸检测项目,需要接入百度AI的系统进行识别和检测。主要流程就是往指定的URL上post图片上去,之后接收检测结果就好了。

百度的检测结果包含这样的信息:

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

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

  width - 人脸区域的宽度

  height - 人脸区域的高度

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

如果我想把人脸框出来,很容易想到的是以(left, top)为左上顶点,以width 为宽,height为高,画一个矩形就好了。但其实这样是不合理的,如果人头是倒着的,这样画出来是不合理的。就像下面这样:

所以必须考虑把旋转角加上去。于是我想的策略是先把框画出来,再逆时针旋转ratation 就可以了。

二、如何旋转

大致策略就是:先算出四个点的左标,再以左上角的点为原点,逆时针旋转ratation 就ok了。四个点的左标比较容易确定,利用起点左标加宽高就能算出来。

这里主要讲解如何算旋转后的左标,如下图:

已知x1,x2,y1,y2和∠a,求x’,和y’。这时候就需要用到高中的三角函数的知识了。

假设,(x1, y1) 到 (x2, y2)的长度为r,再画一个∠b。

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);

啊,这真是用了我毕生所学的数学知识,真没想到工作后还会用到三角函数的知识。还是要多学点数学知识才好啊。

三、源码

下面就是真正画图的东西了,为了测试这个公式是否可行,我用opencv画了一个四根线(其实就是一个方形),然后以左上角为顶点旋转。

下面的具体的代码,比较简单,主要是那个公式,所以也没什么注释。需要包含opencv头文件,以及链接opencv的库。

/*
* @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 > )
{
return -;
}
int x1 = , y1 = ;
int x2 = , y2 = ;
int x3 = , y3 = ;
int x4 = , y4 = ; 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 = ;
for (int i = ; i < ROTATE_COUNT; i++)
{
nAgree = i * ( / ROTATE_COUNT);
double dRot = nAgree * PI / ;
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(, , CV_8UC3);
line(im, Point(x1, y1), Point(x2, y2), Scalar(, , ), );
line(im, Point(x1, y1), Point(x4, y4), Scalar(, , ), );
line(im, Point(x3, y3), Point(x2, y2), Scalar(, , ), );
line(im, Point(x4, y4), Point(x3, y3), Scalar(, , ), ); for (int i = ; i < ROTATE_COUNT; i++)
{
line(im, Point(arrX1[i], arrY1[i]), Point(arrX2[i], arrY2[i]), Scalar(, , ), );
line(im, Point(arrX1[i], arrY1[i]), Point(arrX4[i], arrY4[i]), Scalar(, , ), );
line(im, Point(arrX3[i], arrY3[i]), Point(arrX2[i], arrY2[i]), Scalar(, , ), );
line(im, Point(arrX4[i], arrY4[i]), Point(arrX3[i], arrY3[i]), Scalar(, , ), );
}
imshow("Is ok", im);
cvWaitKey(); return ;
} int RotateTest()
{
int nAgree = ;
double dRot = nAgree * PI / ;
double dSinRot = sin(dRot), dCosRot = cos(dRot); int x1 = , y1 = ;
int x2 = , y2 = ;
int x3 = , y3 = ;
int x4 = , y4 = ; 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(, , CV_8UC3);
line(im, Point(x1, y1), Point(x2, y2), Scalar(, , ), );
line(im, Point(x1, y1), Point(x4, y4), Scalar(, , ), );
line(im, Point(x3, y3), Point(x2, y2), Scalar(, , ), );
line(im, Point(x4, y4), Point(x3, y3), Scalar(, , ), ); line(im, Point(x1_1, y1_1), Point(x2_1, y2_1), Scalar(, , ), );
line(im, Point(x1_1, y1_1), Point(x4_1, y4_1), Scalar(, , ), );
line(im, Point(x3_1, y3_1), Point(x2_1, y2_1), Scalar(, , ), );
line(im, Point(x4_1, y4_1), Point(x3_1, y3_1), Scalar(, , ), ); imshow("Is ok", im);
cvWaitKey();
return ; } int main()
{
//RotateTest();
RotateTest2();
return ;
}

其中RotateTest2()实现了在一个Mat上,画出了旋转各个角度的样子,具体把360分成 ROTATE_COUNT这么多份。可以看到效果还是很好看的。

ROTATE_COUNT为10时:

ROTATE_COUNT为60时:

ROTATE_COUNT为180时:

ROTATE_COUNT为360时:

旋转之后的神仙姐姐就框的比较准确了。这样就能正确的框住小姐姐了。

四、总结

数学还是很有用的。人工智能、深度学习还是需要具备数学知识的。

opencv实现坐标旋转(教你框住小姐姐)的更多相关文章

  1. HDU4456-Crowd (坐标旋转处理+hash处理+二维树状数组)

    题意: 给出一个矩阵,初始每个位置上的值都为0,然后有两种操作 一种是更改某个位置上的值 另一种是求某个位置附近曼哈顿距离不大于K的所有位置的值的总和 技巧: 坐标旋转,使得操作之后菱形变成方方正正的 ...

  2. HDU 1700 Points on Cycle (坐标旋转)

    题目链接:HDU 1700 Problem Description There is a cycle with its center on the origin. Now give you a poi ...

  3. 【30分钟学完】canvas动画|游戏基础(6):坐标旋转探究

    前言 本篇主要讲坐标旋转及其应用,这是编程动画必不可少的技术. 阅读本篇前请先打好前面的基础. 本人能力有限,欢迎牛人共同讨论,批评指正. 坐标旋转 模拟场景:已知一个中心点(centerX,cent ...

  4. 刷抖音太累,教你用Python把高颜值的小姐姐都爬下来慢慢看

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 星安果.AirPython 目 标 场 景 相信大家平时刷抖音短视频 ...

  5. python+opencv抠图并旋转(根据坐标抠图)

    import cv2 import numpy as np def subimage(image, center, theta, width, height): theta *= np.pi / 18 ...

  6. hdu 1700 Points on Cycle(坐标旋转)

    http://acm.hdu.edu.cn/showproblem.php?pid=1700 Points on Cycle Time Limit: 1000/1000 MS (Java/Others ...

  7. opencv图像的旋转

    #include"stdafx.h"#include"opencv2/opencv.hpp" using namespace cv;// clockwise 为 ...

  8. eigen 四元数进行坐标旋转

    (<视觉SLAM十四讲>第三讲习题7)设有小萝卜一号和二号在世界坐标系中.一号位姿q1 = [0.35, 0.2, 0.3, 0.1],t1=[0.3, 0.1, 0.1].二号位姿q2= ...

  9. UVA 11178 Morley's Theorem (坐标旋转)

    题目链接:UVA 11178 Description Input Output Sample Input Sample Output Solution 题意 \(Morley's\ theorem\) ...

随机推荐

  1. POJ 2672 Tarjan + 缩点 + 拓扑思想

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383 ...

  2. 洛谷 P1129 [ZJOI2007]矩阵游戏 解题报告

    P1129 [ZJOI2007]矩阵游戏 题目描述 小\(Q\)是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个\(N*N\)黑白方阵进行(如同国际象棋一般 ...

  3. luogu4145 上帝造题的七分钟2 (线段树)

    题意:给一个数列,维护两个操作,区间开根号.询问区间和 注意到1e12开根号六次后就变成1,而且根号1等于1 也就是说,就算我们用单点修改,只要跳过1,那么修改的次数最多也就是6n 那么维护一个区间最 ...

  4. CSS之2D转换模块

    CSS 2D转换模块 transform 参考W3手册 transform 属性向元素应用从2D 或3D转换.该属性允许我们对元素进行旋转.缩放.移动或者倾斜. 格式: transform: none ...

  5. HTML 5 canvas globalCompositeOperation 属性

    做一个canvas鼠标跟随动画的时候用到了,就copy w3c的解释整理一番: globalCompositeOperation 属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上. 源 ...

  6. windows 匿名管道: 父进程与子进程通信 (进程间通信之CreatePipe)

    看了很多篇相关的资料,感觉这个还是比较靠谱的: 进程间通信之CreatePipe   https://blog.csdn.net/dacxu/article/details/30071081 特别是  ...

  7. Gym - 101911A "Coffee Break"

    传送门 题意: Monocarp得到一份工作,每天要工作 m 分钟,他有一个爱好,喜欢在休息的时候喝咖啡,但是他的老板不乐意了,就给他规定了个 时间 d,在 d 分钟内只能喝一杯咖啡. 现给出Mono ...

  8. 新建工程时报错(26, 13) Failed to resolve: com.android.support:appcompat-v7:28.+ ,

    allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } }

  9. 基于TC做流量控制

    1 模拟延迟传输简介 netem 与 tc: netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块.该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带 ...

  10. xgboost使用调参

    欢迎关注博主主页,学习python视频资源 https://blog.csdn.net/q383700092/article/details/53763328 调参后结果非常理想 from sklea ...