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)
我認爲問題是'cv2.getRotationMatrix2D'函數和白衣更有可能'角度'有問題!檢查它的論點! – Kasramvd 2014-09-30 10:23:16
@Kasra你在想什麼問題? – 2014-09-30 17:02:34
@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