Dlib Opencv cv2.fitEllipse用于人眼轮廓椭圆拟合

dlib库的安装以及人脸特征点的识别分布分别在前两篇博文里面

Dlib Python 检测人脸特征点 Face Landmark Detection

Mac OSX下安装dlib (Python)

这篇主要涉及 cv2.ellipse 和 cv2.fitEllipse 的用法

import cv2
import dlib
import numpy as np

detector = dlib.get_frontal_face_detector()
landmark_predictor = dlib.shape_predictor('/Users/apple/Downloads/shape_predictor_68_face_landmarks.dat')

img = cv2.imread('/Users/apple/Downloads/3-1.jpg')
faces = detector(img,1)
left_eye = []
right_eye = []
if ( len(faces) > 0):
    for k,d in enumerate(faces):
        shape = landmark_predictor(img,d)
        for i in range(36,42):
            right_eye.append([shape.part(i).x,shape.part(i).y])
        for i in range(42,48):
            left_eye.append([shape.part(i).x,shape.part(i).y])





ellipse_left = cv2.fitEllipse(np.array(left_eye))
ellipse_right = cv2.fitEllipse(np.array(right_eye))
cv2.ellipse(img, ellipse_left, (0,255,0), 1)
cv2.ellipse(img, ellipse_right, (0,255,0), 1)



cv2.fitEllipse(points)

里面的points类型要求是numpy.array([[x,y],[x1,y1]...]) 并不是把所有点都包括在椭圆里面,而是拟合出一个椭圆尽量使得点都在圆上

cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) 

其中每个参数的意义如下:

· img 图像

· center 中心坐标

· axes 椭圆的尺寸(长短轴)

· angle 旋转角度

· startAngle 起始角度

· endAngle 终止角度

· 后面参数都是跟线条有关的

ellipse_left

((275.1310119628906, 197.24081420898438),
 (13.491097450256348, 47.203433990478516),
 84.19256591796875)





这个时候返回的分别是椭圆的中心坐标,短轴长轴(也就是2b,2a),旋转角度.

center = ellipse_left[0]
size = ellipse_left[1]
angle = ellispe_left[2]



cv2.imshow('PIC',img)
cv2.waitKey(0)

posted @ 2017-08-30 22:38  as3asddd  阅读(10822)  评论(1编辑  收藏  举报