在一些特殊情况下,经常需要依据图像中的人脸,对图片进行倾斜矫正。

例如拍照角度幅度过大之类的情况,而进行人工矫正确实很叫人头大。

那是不是可以有一种算法,可以根据人脸的信息对图片进行角度的修复呢?

答案肯定是确认的。

再次例如,想要通过人脸的特征对人物的表情和情绪进行精准判断,

那么这个时候如果能确保人脸没有发现严重倾斜,无疑对准确率判断有一定的帮助。

那么假如一张图片只有一个人脸,其实很好判断,通过眼睛的位置的坐标,根据两眼的直线角度,

就可以计算出修正的角度。

然后旋转图片到对应角度即可。

但是如果,一张图片存在多张人脸的时候该怎么办?

有两种方法:

1.找到最大的那个人脸,以它为基准

2.找到频次最高的人脸角度,以频次为基准

当然在大多数情况,方法1是比较合理的。

这两个种情况就留给各位看官去实现了。

本人仅仅考虑一张人脸的情况,演示如何实现该功能。

倾斜角度计算的代码如下:

    float diffEyeX = right_eye_x - left_eye_x;
float diffEyeY = right_eye_y - left_eye_y; float fAngle;
float M_PI = 3.1415926535897932384626433832795f;
if (fabs(diffEyeX) < 0.0000001f)
fAngle = .f;
else
fAngle = atanf(diffEyeY / diffEyeX) * 180.0f / M_PI;

如果看不明白,需要好好补一下高中数学基础。

为了节约时间,直接复用《自动红眼移除算法 附c++完整代码》的代码。

增加函数如下:

void RotateBilinear(unsigned char *sourceData, int width, int height, int Channels, int RowBytes,
unsigned char *destinationData, int newWidth, int newHeight, float angle, bool keepSize = true,
int fillColorR = , int fillColorG = , int fillColorB = ) {
if (sourceData == NULL || destinationData == NULL) return; float oldXradius = (float) (width - ) / ;
float oldYradius = (float) (height - ) / ; float newXradius = (float) (newWidth - ) / ;
float newYradius = (float) (newHeight - ) / ; double MPI = 3.14159265358979323846;
double angleRad = -angle * MPI / 180.0;
float angleCos = (float) cos(angleRad);
float angleSin = (float) sin(angleRad); int srcStride = RowBytes;
int dstOffset = newWidth * Channels - ((Channels == ) ? newWidth : newWidth * Channels); unsigned char fillR = fillColorR;
unsigned char fillG = fillColorG;
unsigned char fillB = fillColorB; unsigned char *src = (unsigned char *) sourceData;
unsigned char *dst = (unsigned char *) destinationData; int ymax = height - ;
int xmax = width - ;
if (Channels == ) {
float cy = -newYradius;
for (int y = ; y < newHeight; y++) {
float tx = angleSin * cy + oldXradius;
float ty = angleCos * cy + oldYradius; float cx = -newXradius;
for (int x = ; x < newWidth; x++, dst++) {
float ox = tx + angleCos * cx;
float oy = ty - angleSin * cx; int ox1 = (int) ox;
int oy1 = (int) oy; if ((ox1 < ) || (oy1 < ) || (ox1 >= width) || (oy1 >= height)) {
*dst = fillG;
} else {
int ox2 = (ox1 == xmax) ? ox1 : ox1 + ;
int oy2 = (oy1 == ymax) ? oy1 : oy1 + ;
float dx1 = ;
if ((dx1 = ox - (float) ox1) < )
dx1 = ;
float dx2 = 1.0f - dx1;
float dy1 = ;
if ((dy1 = oy - (float) oy1) < )
dy1 = ;
float dy2 = 1.0f - dy1; unsigned char *p1 = src + oy1 * srcStride;
unsigned char *p2 = src + oy2 * srcStride; *dst = (unsigned char) (dy2 * (dx2 * p1[ox1] + dx1 * p1[ox2]) +
dy1 * (dx2 * p2[ox1] + dx1 * p2[ox2]));
}
cx++;
}
cy++;
dst += dstOffset;
}
} else if (Channels == ) {
float cy = -newYradius;
for (int y = ; y < newHeight; y++) {
float tx = angleSin * cy + oldXradius;
float ty = angleCos * cy + oldYradius; float cx = -newXradius;
for (int x = ; x < newWidth; x++, dst += Channels) {
float ox = tx + angleCos * cx;
float oy = ty - angleSin * cx; int ox1 = (int) ox;
int oy1 = (int) oy; if ((ox1 < ) || (oy1 < ) || (ox1 >= width) || (oy1 >= height)) {
dst[] = fillR;
dst[] = fillG;
dst[] = fillB;
} else {
int ox2 = (ox1 == xmax) ? ox1 : ox1 + ;
int oy2 = (oy1 == ymax) ? oy1 : oy1 + ; float dx1 = ;
if ((dx1 = ox - (float) ox1) < )
dx1 = ;
float dx2 = 1.0f - dx1;
float dy1 = ;
if ((dy1 = oy - (float) oy1) < )
dy1 = ;
float dy2 = 1.0f - dy1; unsigned char *p1 = src + oy1 * srcStride;
unsigned char *p2 = p1;
p1 += ox1 * Channels;
p2 += ox2 * Channels; unsigned char *p3 = src + oy2 * srcStride;
unsigned char *p4 = p3;
p3 += ox1 * Channels;
p4 += ox2 * Channels; dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[])); dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[])); dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[]));
}
cx++;
}
cy++;
dst += dstOffset;
}
} else if (Channels == ) {
float cy = -newYradius;
for (int y = ; y < newHeight; y++) {
float tx = angleSin * cy + oldXradius;
float ty = angleCos * cy + oldYradius; float cx = -newXradius;
for (int x = ; x < newWidth; x++, dst += Channels) {
float ox = tx + angleCos * cx;
float oy = ty - angleSin * cx; int ox1 = (int) ox;
int oy1 = (int) oy; if ((ox1 < ) || (oy1 < ) || (ox1 >= width) || (oy1 >= height)) {
dst[] = fillR;
dst[] = fillG;
dst[] = fillB;
dst[] = ;
} else {
int ox2 = (ox1 == xmax) ? ox1 : ox1 + ;
int oy2 = (oy1 == ymax) ? oy1 : oy1 + ; float dx1 = ;
if ((dx1 = ox - (float) ox1) < )
dx1 = ;
float dx2 = 1.0f - dx1;
float dy1 = ;
if ((dy1 = oy - (float) oy1) < )
dy1 = ;
float dy2 = 1.0f - dy1; unsigned char *p1 = src + oy1 * srcStride;
unsigned char *p2 = p1;
p1 += ox1 * Channels;
p2 += ox2 * Channels; unsigned char *p3 = src + oy2 * srcStride;
unsigned char *p4 = p3;
p3 += ox1 * Channels;
p4 += ox2 * Channels; dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[])); dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[])); dst[] = (unsigned char) (
dy2 * (dx2 * p1[] + dx1 * p2[]) +
dy1 * (dx2 * p3[] + dx1 * p4[]));
dst[] = ;
}
cx++;
}
cy++;
dst += dstOffset;
}
}
} void facialPoseCorrection(unsigned char *inputImage, int Width, int Height, int Channels, int left_eye_x, int left_eye_y,
int right_eye_x, int right_eye_y) {
float diffEyeX = right_eye_x - left_eye_x;
float diffEyeY = right_eye_y - left_eye_y; float fAngle;
float M_PI = 3.1415926535897932384626433832795f;
if (fabs(diffEyeX) < 0.0000001f)
fAngle = .f;
else
fAngle = atanf(diffEyeY / diffEyeX) * 180.0f / M_PI;
size_t numberOfPixels = Width * Height * Channels * sizeof(unsigned char);
unsigned char *outputImage = (unsigned char *) malloc(numberOfPixels);
if (outputImage != nullptr) {
RotateBilinear(inputImage, Width, Height, Channels, Width * Channels, outputImage, Width, Height, fAngle);
memcpy(inputImage, outputImage, numberOfPixels);
free(outputImage);
}
}

上效果图片。

原图:

红眼修复+倾斜矫正:

项目地址:

https://github.com/cpuimage/MTCNN

命令行参数:

mtcnn 模型文件路径 图片路径

例如: mtcnn ../models ../sample.jpg

用cmake即可进行编译示例代码,详情见CMakeLists.txt。

若有其他相关问题或者需求也可以邮件联系俺探讨。

邮箱地址是: 
gaozhihan@vip.qq.com

人脸姿态校正算法 附完整C++示例代码的更多相关文章

  1. WebRTC 音频采样算法 附完整C++示例代码

    之前有大概介绍了音频采样相关的思路,详情见<简洁明了的插值音频重采样算法例子 (附完整C代码)>. 音频方面的开源项目很多很多. 最知名的莫过于谷歌开源的WebRTC, 其中的音频模块就包 ...

  2. 基于傅里叶变换的音频重采样算法 (附完整c代码)

    前面有提到音频采样算法: WebRTC 音频采样算法 附完整C++示例代码 简洁明了的插值音频重采样算法例子 (附完整C代码) 近段时间有不少朋友给我写过邮件,说了一些他们使用的情况和问题. 坦白讲, ...

  3. 音频降噪算法 附完整C代码

    降噪是音频图像算法中的必不可少的. 目的肯定是让图片或语音 更加自然平滑,简而言之,美化. 图像算法和音频算法 都有其共通点. 图像是偏向 空间 处理,例如图片中的某个区域. 图像很多时候是以二维数据 ...

  4. mser 最大稳定极值区域(文字区域定位)算法 附完整C代码

    mser 的全称:Maximally Stable Extremal Regions 第一次听说这个算法时,是来自当时部门的一个同事, 提及到他的项目用它来做文字区域的定位,对这个算法做了一些优化. ...

  5. 磨皮美颜算法 附完整C代码

    前言 2017年底时候写了这篇<集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序> 这也算是学习过程中比较有成就感的一个算法. 自2015年做算法开始到今天,还有个把月 ...

  6. 基于RNN的音频降噪算法 (附完整C代码)

    前几天无意间看到一个项目rnnoise. 项目地址: https://github.com/xiph/rnnoise 基于RNN的音频降噪算法. 采用的是 GRU/LSTM 模型. 阅读下训练代码,可 ...

  7. 音频自动增益 与 静音检测 算法 附完整C代码

    前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用于评估一定长度音频的音量强度, 而分析之后,很多类似的需求,肯定是做音频增益,提高音量诸如此类做法. ...

  8. 音频自动增益 与 静音检测 算法 附完整C代码【转】

    转自:https://www.cnblogs.com/cpuimage/p/8908551.html 前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用 ...

  9. 图片文档倾斜矫正算法 附完整c代码

    2年前在学习图像算法的时候看到一个文档倾斜矫正的算法. 也就是说能将一些文档图像进行旋转矫正, 当然这个算法一般用于一些文档扫描软件做后处理 或者用于ocr 文字识别做前处理. 相关的关键词: 抗倾斜 ...

随机推荐

  1. DB Query Analyzer 6.04 is distributed, 78 articles concerned have been published

        DB Query Analyzer 6.04 is distributed,78 articles concerned have been published  DB Query Analyz ...

  2. Ubuntu启动eclipse问题

    Ubuntu启动eclipse问题 在Ubuntu安装eclipse后 可以在终端顺利启动eclipse:进入eclipse目录,在终端输入命令 ./eclipse > /dev/null 2& ...

  3. go-mysql,一个易用的mysql接口框架实现

    介绍 go-mysql是一个用go写的mysql driver,使用接口类似于go自身的database sql,但是稍微有一点不同,现阶段还不支持集成进go database/sql中,但实现难度并 ...

  4. Android移动后端服务(BAAS)快速搭建后台服务器之Bmob-android学习之旅(75)

    个人移动开发者的最头疼的问题,就是App的网络后台,包含数据库等,国外目前有比较成熟的解决方案,但是限制于墙的问题,推荐国内的解决方案,比较出名的是Bmob和AVOS cloud和Atom等,这一次我 ...

  5. iOS开发的10个知识点

    1.关于关键字volatile 一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了.精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这 ...

  6. 开源视频平台:MediaCore(MediaDrop)

    MediaCore 是一个多媒体的建站系统,主要的功能包括视频.音频.YouTube集成.播客和 iTunes RSS 生成,用户可以提交各种多媒体内容. <开源中国>网站上说它是一个开源 ...

  7. [转]Android长度单位详解

    android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...

  8. 如何成为Android高手

    要成为Android 高手并不是一件容易的事情.并不是很多人想象的 能够飞快的写出几行漂亮的代码去解决一些困难的问题 就是Android 高手了.真正的Android 高手需要考虑的问题远远不是写些漂 ...

  9. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  10. Java常见运算符整理

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44724267 本文主要介绍Java中常见的运算符,重点介绍 ...