2014-09-30 289 views
1

我正在尝试编写一个Python函数来裁剪,旋转和调整面的大小。它用于面部识别应用。 我将眼睛的坐标传递给函数,函数处理图像(旋转它以使眼睛的平面平行于图像的横轴并缩放/裁剪/调整大小)。 问题是,脸部根本不旋转。他们只是被裁剪。 修改以下函数以返回旋转后的图像和旋转前完成的图像的副本。它们是相同的。在OpenCV中旋转图像

def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.25,0.25), dest_sz = (250,250)): 

    offset_h = math.floor(float(offset_pct[0])*dest_sz[0]) 
    offset_v = math.floor(float(offset_pct[1])*dest_sz[1]) 

    eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1]) 

    rotation = -math.atan2(float(eye_direction[1]), float(eye_direction[0])) 

    dist = Distance(eye_left, eye_right) 

    reference = dest_sz[0] - 2.0*offset_h 

    scale = float(dist)/float(reference) 

    sz = image.shape 
    if len(sz) > 2: sz = sz[:2] 

    print rotation 

    image2 = image.copy() 

    mat = cv2.getRotationMatrix2D(eye_left, rotation, 1.0) 
    result = cv2.warpAffine(image, mat, sz, flags = cv2.INTER_CUBIC) 

    crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v) 
    crop_size = (dest_sz[0]*scale, dest_sz[1]*scale) 
    result = result[int(crop_xy[1]):int(crop_xy[1]+crop_size[1]), int(crop_xy[0]):int(crop_xy[0]+crop_size[0])] 
    image2 = image2[int(crop_xy[1]):int(crop_xy[1]+crop_size[1]), int(crop_xy[0]):int(crop_xy[0]+crop_size[0])] 

    return (result, image2) 
+0

我认为问题是'cv2.getRotationMatrix2D'函数和白衣更有可能'角度'有问题!检查它的论点! – Kasramvd 2014-09-30 10:23:16

+0

@Kasra你在想什么问题? – 2014-09-30 17:02:34

+0

@RiccardoBestetti Kasra可能指的是[math.atan2](https://docs.python.org/2/library/math.html#math.atan2)的输出与[cv2.getRotationMatrix2D ](http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#getrotationmatrix2d)。 – beaker 2014-09-30 18:11:15

回答

3

的问题是,对于

cv2.getRotationMatrix2D(center, angle, scale) 

angle参数是度(opencv documentation

而Python,

angle = math.atan2(y, x) 

返回以弧度的角度。 (Python documentation

所以当OpenCV期待度数时,由rotation指定的角度是弧度。