python-PIL 画出带有角度的椭圆

来源:互联网 发布:数据透视表刷新数据 编辑:程序博客网 时间:2024/05/22 17:47
  • 最近在学习FDDB的人脸识别库,发现他们的标记是用椭圆来标记的,而且还是有角度的椭圆。
  • 在PIL库中好像并没有画出带有角度椭圆的函数,所以在StackOverflow上找到了如下代码:
def ellipse_with_angle(im,x,y,major,minor,angle,color):    # take an existing image and plot an ellipse centered at (x,y) with a    # defined angle of rotation and major and minor axes.    # center the image so that (x,y) is at the center of the ellipse    x -= int(major/2)    y -= int(major/2)    # create a new image in which to draw the ellipse    im_ellipse = Image.new('RGBA', (major,major), (255,255,255,0))    draw_ellipse = ImageDraw.Draw(im_ellipse, "RGBA")    # draw the ellipse    ellipse_box = (0,int(major/2-minor/2),major,int(major/2-minor/2)+minor)    draw_ellipse.ellipse(ellipse_box, fill=color)    # rotate the new image    rotated = im_ellipse.rotate(angle)    rx,ry = rotated.size    # paste it into the existing image and return the result    im.paste(rotated, (x,y,x+rx,y+ry), mask=rotated)    return im
  • 效果还不错,如下图所示:
    image

  • 其实用matplotlib.patches.Ellipse也可以画出来,不过没办法转化成矩阵的形式,我主要是想判断selective search方法提取出来的方框判断是否是脸,给他们打上label。

  • 实验室就我一个人在瞎搞人脸识别,不知道我这样做的做法对不对,总感觉我姿势不对。。。
  • 如果有大神觉得有问题,请告诉我正确姿势。。谢谢
原创粉丝点击