人脸姿态校正算法 附完整C++示例代码
在一些特殊情况下,经常需要依据图像中的人脸,对图片进行倾斜矫正。
例如拍照角度幅度过大之类的情况,而进行人工矫正确实很叫人头大。
那是不是可以有一种算法,可以根据人脸的信息对图片进行角度的修复呢?
答案肯定是确认的。
再次例如,想要通过人脸的特征对人物的表情和情绪进行精准判断,
那么这个时候如果能确保人脸没有发现严重倾斜,无疑对准确率判断有一定的帮助。
那么假如一张图片只有一个人脸,其实很好判断,通过眼睛的位置的坐标,根据两眼的直线角度,
就可以计算出修正的角度。
然后旋转图片到对应角度即可。
但是如果,一张图片存在多张人脸的时候该怎么办?
有两种方法:
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++示例代码的更多相关文章
- WebRTC 音频采样算法 附完整C++示例代码
之前有大概介绍了音频采样相关的思路,详情见<简洁明了的插值音频重采样算法例子 (附完整C代码)>. 音频方面的开源项目很多很多. 最知名的莫过于谷歌开源的WebRTC, 其中的音频模块就包 ...
- 基于傅里叶变换的音频重采样算法 (附完整c代码)
前面有提到音频采样算法: WebRTC 音频采样算法 附完整C++示例代码 简洁明了的插值音频重采样算法例子 (附完整C代码) 近段时间有不少朋友给我写过邮件,说了一些他们使用的情况和问题. 坦白讲, ...
- 音频降噪算法 附完整C代码
降噪是音频图像算法中的必不可少的. 目的肯定是让图片或语音 更加自然平滑,简而言之,美化. 图像算法和音频算法 都有其共通点. 图像是偏向 空间 处理,例如图片中的某个区域. 图像很多时候是以二维数据 ...
- mser 最大稳定极值区域(文字区域定位)算法 附完整C代码
mser 的全称:Maximally Stable Extremal Regions 第一次听说这个算法时,是来自当时部门的一个同事, 提及到他的项目用它来做文字区域的定位,对这个算法做了一些优化. ...
- 磨皮美颜算法 附完整C代码
前言 2017年底时候写了这篇<集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序> 这也算是学习过程中比较有成就感的一个算法. 自2015年做算法开始到今天,还有个把月 ...
- 基于RNN的音频降噪算法 (附完整C代码)
前几天无意间看到一个项目rnnoise. 项目地址: https://github.com/xiph/rnnoise 基于RNN的音频降噪算法. 采用的是 GRU/LSTM 模型. 阅读下训练代码,可 ...
- 音频自动增益 与 静音检测 算法 附完整C代码
前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用于评估一定长度音频的音量强度, 而分析之后,很多类似的需求,肯定是做音频增益,提高音量诸如此类做法. ...
- 音频自动增益 与 静音检测 算法 附完整C代码【转】
转自:https://www.cnblogs.com/cpuimage/p/8908551.html 前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用 ...
- 图片文档倾斜矫正算法 附完整c代码
2年前在学习图像算法的时候看到一个文档倾斜矫正的算法. 也就是说能将一些文档图像进行旋转矫正, 当然这个算法一般用于一些文档扫描软件做后处理 或者用于ocr 文字识别做前处理. 相关的关键词: 抗倾斜 ...
随机推荐
- RabbitMQ消息队列(五):Routing 消息路由
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings ...
- 【翻译】Ext JS 6 Beta发布
原文:Ext JS 6 Beta is Now Available 概述 Ext JS 6的好处 新的Ext JS功能和工具 需要你的反馈意见 概述 很高兴,Ext JS 6 beta版本现在发布了. ...
- Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds
本问题是在windows server2012R2系统ADFS3.0环境下遇到的,CRM2013部署ADFS后运行一段时间(大概有一两个月)后在IE浏览器中访问登陆界面点击登陆后就报以下错误 &quo ...
- Google浏览器设置搜索打开新的标签页
每次用google搜索的时候,点击进去原来的搜索页面就不见了 ,其实可以设置打开新的标签页 1.登陆Chrome 2.登陆这个网址https://www.google.com/preferences ...
- uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案
由于技术选型的原因,在一个产品中,我选择了uploadify,选择它的原因是它有完善的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...
- 【嵌入式开发】C语言 命令行参数 函数指针 gdb调试
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...
- Android开源项目——设置图文居中的按钮 IconButton
本文介绍一下一个小众的开源项目--IconButton. 本文原创,转载请注明出处: http://blog.csdn.net/maosidiaoxian/article/details/435602 ...
- ubuntu系统AndroidStudio修改内存大小
位于android-studio/bin目录下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize,-XX:Reserved ...
- FPGA学习笔记(二)模块建立及变量连接
Verilog所写的工程是由一个一个的模块连接起来的,每个文件代表一个模块,模块的名字和文件名要保持一致,一个模块的基本声明方法为: //FileName:main_module module mai ...
- R12.2. Start and Stop Procedure
R12.2. Start and Stop Procedure Leave a comment Individual Components: Application(Middle Tier) $ ...