【图像处理】根据双眼的坐标对齐人脸Python实现

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


# -*- coding: utf-8 -*-
"""
Created on Thu Jan  1 16:09:32 2015

@author: crw
"""
# 参数含义:
#  CropFace(image, eye_left, eye_right, offset_pct, dest_sz)
# eye_left is the position of the left eye
# eye_right is the position of the right eye
# 比例的含义为:要保留的图像靠近眼镜的百分比,
# offset_pct is the percent of the image you want to keep next to the eyes (horizontal, vertical direction)
# 最后保留的图像的大小。
# dest_sz is the size of the output image
#
import sys,math,Image
 
 # 计算两个坐标的距离
def Distance(p1,p2):
      dx = p2[0]- p1[0]
      dy = p2[1]- p1[1]
      return math.sqrt(dx*dx+dy*dy)
 
 # 根据参数,求仿射变换矩阵和变换后的图像。
def ScaleRotateTranslate(image, angle, center =None, new_center =None, scale =None, resample=Image.BICUBIC):
      if (scale is None)and (center is None):
            return image.rotate(angle=angle, resample=resample)
      nx,ny = x,y = center
      sx=sy=1.0
      if new_center:
            (nx,ny) = new_center
      if scale:
            (sx,sy) = (scale, scale)
      cosine = math.cos(angle)
      sine = math.sin(angle)
      a = cosine/sx
      b = sine/sx
      c = x-nx*a-ny*b
      d =-sine/sy
      e = cosine/sy
      f = y-nx*d-ny*e
      return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
 # 根据所给的人脸图像,眼睛坐标位置,偏移比例,输出的大小,来进行裁剪。
def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
      # calculate offsets in original image 计算在原始图像上的偏移。
      offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
      offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
      # get the direction  计算眼睛的方向。
      eye_direction = (eye_right[0]- eye_left[0], eye_right[1]- eye_left[1])
      # calc rotation angle in radians  计算旋转的方向弧度。
      rotation =-math.atan2(float(eye_direction[1]),float(eye_direction[0]))
      # distance between them  # 计算两眼之间的距离。
      dist = Distance(eye_left, eye_right)
      # calculate the reference eye-width    计算最后输出的图像两只眼睛之间的距离。
      reference = dest_sz[0]-2.0*offset_h
      # scale factor   # 计算尺度因子。
      scale =float(dist)/float(reference)
      # rotate original around the left eye  # 原图像绕着左眼的坐标旋转。
      image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
      # crop the rotated image  # 剪切
      crop_xy = (eye_left[0]- scale*offset_h, eye_left[1]- scale*offset_v)  # 起点
      crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)   # 大小
      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])))
      # resize it 重置大小
      image = image.resize(dest_sz, Image.ANTIALIAS)
      return image
  
if __name__ =="__main__":
      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")
      leftx =117
      lefty=287
      rightx=187
      righty= 288
      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")
      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")
      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")
      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")
      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")
      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 数据库)






  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人脸检测与对齐是计算机视觉的基础任务之一,其在图像处理、视频监控、人机交互等领域都有着广泛的应用。Python语言拥有丰富的第三方库和强大的科学计算能力,因此在人脸检测与对齐方面也有着很好的应用优势。 在Python实现人脸检测,常用的是基于OpenCV(Open Source Computer Vision Library)的人脸检测方法。首先需要安装opencv-python库,然后使用CascadeClassifier类进行人脸检测。CascadeClassifier类通过调用OpenCV开源的人脸检测分类器进行检测。对于检测到的人脸,可以通过OpenCV库提供的函数进行裁剪、调整大小等基本操作,以便后续的人脸识别或其他任务的处理。 人脸对齐方面,通常的方法是使用人脸关键点检测技术。可以使用OpenCV库提供的dlib或者face_recognition库进行人脸关键点检测。检测到人脸关键点后,可以根据关键点坐标进行人脸对齐。具体的方法可以是仿射变换、透视变换等。同样,Python提供了丰富的第三方库支持,例如scipy库提供的affine_transform函数、openCV库的warpAffine函数等。通过对关键点的移动、旋转、缩放等操作,可以将图像进行对齐,使其更好的用于后续的人脸识别和分析任务。 总之,Python作为一种快速、灵活、易学易用的编程语言,在人脸检测和对齐方面有着很好的应用优势。在实现过程,需要考虑算法复杂度、准确性以及可扩展性等因素,以便更好的满足实际需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值