最近要做一个人脸识别的东西,可数据库的人脸图像还没有做对齐处理。所以需要自己做人脸对齐预处理。网上查了,发现已经有人做过了类似的脚本。直接拿过来用了,原地址在这:https://github.com/bytefish/facerec/blob/master/py/apps/scripts/crop_face.py   自己为了以后更改,加了一些注释。

[python] view plaincopy
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Jan  1 16:09:32 2015
  4. @author: crw
  5. """
  6. # 参数含义:
  7. #  CropFace(image, eye_left, eye_right, offset_pct, dest_sz)
  8. # eye_left is the position of the left eye
  9. # eye_right is the position of the right eye
  10. # 比例的含义为:要保留的图像靠近眼镜的百分比,
  11. # offset_pct is the percent of the image you want to keep next to the eyes (horizontal, vertical direction)
  12. # 最后保留的图像的大小。
  13. # dest_sz is the size of the output image
  14. #
  15. import sys,math,Image
  16. # 计算两个坐标的距离
  17. def Distance(p1,p2):
  18. dx = p2[0]- p1[0]
  19. dy = p2[1]- p1[1]
  20. return math.sqrt(dx*dx+dy*dy)
  21. # 根据参数,求仿射变换矩阵和变换后的图像。
  22. def ScaleRotateTranslate(image, angle, center =None, new_center =None, scale =None, resample=Image.BICUBIC):
  23. if (scale is None)and (center is None):
  24. return image.rotate(angle=angle, resample=resample)
  25. nx,ny = x,y = center
  26. sx=sy=1.0
  27. if new_center:
  28. (nx,ny) = new_center
  29. if scale:
  30. (sx,sy) = (scale, scale)
  31. cosine = math.cos(angle)
  32. sine = math.sin(angle)
  33. a = cosine/sx
  34. b = sine/sx
  35. c = x-nx*a-ny*b
  36. d =-sine/sy
  37. e = cosine/sy
  38. f = y-nx*d-ny*e
  39. return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
  40. # 根据所给的人脸图像,眼睛坐标位置,偏移比例,输出的大小,来进行裁剪。
  41. def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
  42. # calculate offsets in original image 计算在原始图像上的偏移。
  43. offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
  44. offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
  45. # get the direction  计算眼睛的方向。
  46. eye_direction = (eye_right[0]- eye_left[0], eye_right[1]- eye_left[1])
  47. # calc rotation angle in radians  计算旋转的方向弧度。
  48. rotation =-math.atan2(float(eye_direction[1]),float(eye_direction[0]))
  49. # distance between them  # 计算两眼之间的距离。
  50. dist = Distance(eye_left, eye_right)
  51. # calculate the reference eye-width    计算最后输出的图像两只眼睛之间的距离。
  52. reference = dest_sz[0]-2.0*offset_h
  53. # scale factor   # 计算尺度因子。
  54. scale =float(dist)/float(reference)
  55. # rotate original around the left eye  # 原图像绕着左眼的坐标旋转。
  56. image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
  57. # crop the rotated image  # 剪切
  58. crop_xy = (eye_left[0]- scale*offset_h, eye_left[1]- scale*offset_v)  # 起点
  59. crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)   # 大小
  60. image = image.crop((int(crop_xy[0]),int(crop_xy[1]),int(crop_xy[0]+crop_size[0]),int(crop_xy[1]+crop_size[1])))
  61. # resize it 重置大小
  62. image = image.resize(dest_sz, Image.ANTIALIAS)
  63. return image
  64. if __name__ =="__main__":
  65. image =  Image.open("/media/crw/DataCenter/Dataset/CAS-PEAL-R1/POSE/000001/MY_000001_IEU+00_PD-22_EN_A0_D0_T0_BB_M0_R1_S0.tif")
  66. leftx =117
  67. lefty=287
  68. rightx=187
  69. righty= 288
  70. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.1,0.1), dest_sz=(200,200)).save("test_10_10_200_200.jpg")
  71. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.2,0.2), dest_sz=(200,200)).save("test_20_20_200_200.jpg")
  72. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.3,0.3), dest_sz=(200,200)).save("test_30_30_200_200.jpg")
  73. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.4,0.4), dest_sz=(200,200)).save("test_40_40_200_200.jpg")
  74. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.45,0.45), dest_sz=(200,200)).save("test_45_45_200_200.jpg")
  75. CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.2,0.2)).save("test_20_20_70_70.jpg")

程序的结果:(图片采用Cas-Peal 数据库)

根据双眼的坐标对齐人脸Python实现相关推荐

  1. 三步解决C语言中struct字节对齐问题,Python进阶篇-struct字节对齐问题

    Python进阶篇-struct字节对齐问题 Python进阶篇-struct字节对齐问题 Python调用C的时候,会传递一些复杂的数据结构,例如结构体,这时候就会遇到各种各样字节对齐的问题.下边所 ...

  2. 嘉立创EDA专业版--PCB器件坐标对齐原图纸位置坐标

    PCB器件坐标对齐原图纸位置坐标 立创EDA版本说明 问题描述 问题分析 处理方案 立创EDA版本说明 本文使用嘉立创EDA专业版信息: 客户端版本 Windows 64bit V1.7.31.78b ...

  3. 图片坐标提取软件/图片坐标点和像素点颜色提取软件/图片坐标获取工具/Python图片坐标获取源码/图片像素坐标获取软件/python tkinter 图片显示(完全开源)

    该软件使用python写的,可以提取像素点的坐标还有也能获取像素点的16进制数据RGB565和RGB888(RGB888仅最新的源码才支持),可以单点坐标也可以按键坐标,甚至可以使用简单的左右键配合使 ...

  4. CV之FR之MTCNN:基于TF框架利用MTCNN算法检测并对齐人脸图像进(人脸识别/人脸相似度)而得出人脸特征向量从而计算两张人脸图片距离案例应用之详细攻略

    CV之FR之MTCNN:基于TF框架利用MTCNN算法检测并对齐人脸图像进(人脸识别/人脸相似度)而得出人脸特征向量从而计算两张人脸图片距离案例应用之详细攻略 目录 基于TF框架利用MTCNN算法检测 ...

  5. opencv画框返回坐标 python_20行Python代码实现视频字符化

    来源 | ZackSock(ID:ZackSock) 我们经常在B站上看到一些字符鬼畜视频,主要就是将一个视频转换成字符的样子展现出来. 看起来是非常高端,但是实际实现起来确实非常简单,我们只需要接触 ...

  6. 经纬度坐标转换xy坐标 python_在Python中使用NewtonRaphson迭代将经纬度转换为xy Mollweide地图坐标...

    我试图编写一个程序,从用户那里获取一组经度和纬度坐标,将它们转换为Mollweide投影图的x&y坐标,然后报告这些坐标处的像素值(在本例中,是噪声温度).在 我使用的地图/数据是Haslam ...

  7. python 坐标连线_从具有和角度的坐标绘制线 - python

    我基本上想从给定角度的坐标(x,y)绘制一条线(计算切线值). 用这样的简单代码行pl.plot([x1, x2], [y1, y2], 'k-', lw=1)可以在两点之间绘制一条线,但是为此我需要 ...

  8. lisp提取长方形坐标_用 Python 对图片主体轮廓进行提取、颜色标记、并计算区域面积...

    Python +  Opencv2  实现轮廓提取,轮廓区域面积计算: 对图像处理时,会遇到这样一个场景:找到图像主体轮廓,这是其一,可能为了凸显轮廓,需要用指定的颜色进行标记:轮廓标记完可能任务还没 ...

  9. python怎么输出坐标_使用Python实现图像标记点的坐标输出功能

    Sometimes we have need to interact  with an application,for example by marking points in an image,or ...

最新文章

  1. MySQL/ACCESS导出一句话拿WebShell后门命令
  2. 指纹、虹膜和人脸之后,身份认证领域的新热点:手背静脉识别
  3. 【机器视觉】 HDevelop语言基础(二)-变量和表达式
  4. 使用vue控制元素显示隐藏
  5. springboot 之 webscoket 服务端推送
  6. php绕过验证,PHP-Nuke绕过识别码验证漏洞
  7. 乐视网回击贾跃亭:债务处理没有进展,先拿出57亿再说
  8. DPDK报文收发 run to completion, pipeline
  9. 16、React系列之 React 路由
  10. java寻路算法_具有指定距离/节点数的寻路算法
  11. PE学习.动手写PE.见缝插针
  12. Linux下更换jdk和配置环境变量
  13. 模块参考资料-硬件资料-Air720UG/UH-功耗指标
  14. python群发邮件 不进垃圾箱_邮件群发不进垃圾箱
  15. Mockito 中被 Mocked 的对象属性及方法的默认值
  16. 广发证券基于分布式架构的新一代估值系统实践
  17. 苹果设备的微信数据收集
  18. web常见的 HTTP 5xx 状态汇总
  19. 琴生不等式(Jensen Inequality)
  20. Windows 8系统IE10无法安装Flash Player插件的解决办法

热门文章

  1. linux单体内核,加载Linux单体内核的笔记
  2. Math.round(11.5)等于多少? Math.round(-11.5)等于多少?
  3. 让数值自增_第03期:列非空与自增
  4. 内江将被打造成四川物联网西部副中心
  5. 2017,公司必须换掉的六种人,别心软!
  6. **Git本地仓库图解
  7. 别把你的目光停留在周围
  8. Lazy Line Painter – 很有趣的 jQuery 路径动画插件
  9. 【STM32 .Net MF开发板学习-17】Wifi遥控智能小车
  10. IE6动态插入option