【tensorflow】MTCNN网络基本函数rotate

import tensorflow as tf
import numpy as np
import cv2
def rotate(img, bbox, landmark, alpha):
    """
        given a face with bbox and landmark, rotate with alpha
        and return rotated face with bbox, landmark (absolute position)
    """
    #对于给定的bbox和landmark,旋转alpha角度,同时返回旋转后的图片和landmark的绝对坐标
    center = ((bbox[0]+bbox[2])/2, (bbox[1]+bbox[3])/2) #中心坐标
    rot_mat = cv2.getRotationMatrix2D(center, alpha, 1)           #逆时针旋转,缩放因子是1
    #whole image rotate
    #pay attention: 3rd param(col*row)
    img_rotated_by_alpha = cv2.warpAffine(img, rot_mat,(img.shape[1],img.shape[0]))

    landmark_ = np.asarray([(rot_mat[0][0]*x+rot_mat[0][1]*y+rot_mat[0][2],
                 rot_mat[1][0]*x+rot_mat[1][1]*y+rot_mat[1][2]) for (x, y) in landmark])
    #crop face
    face = img_rotated_by_alpha[bbox[1]:bbox[3]+1,bbox[0]:bbox[2]+1]
    cv2.imshow("img_rotated_by_alpha", img_rotated_by_alpha)
    return (face, landmark_)

img = cv2.imread("IOU.jpg",1)
bbox = np.array([5,5,75,75])   # x1(横坐标),y1(纵坐标),x2,y2
landmark = np.array([[10,10],[50,10],[35,20],[10,50],[50,50]])
img = cv2.resize(img,(300,250))
cv2.circle(img,(10,10),2,(0,0,255),-1)
cv2.circle(img,(50,10),2,(0,0,255),-1)
cv2.circle(img,(35,20),2,(0,0,255),-1)
cv2.circle(img,(10,50),2,(0,0,255),-1)
cv2.circle(img,(50,50),2,(0,0,255),-1)
cv2.rectangle(img,(bbox[0],bbox[1]),(bbox[2],bbox[3]),(0,255,0), 2)
rotate(img, bbox, landmark, 5)
cv2.imshow("img",img)
cv2.waitKey()

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhouzongzong/article/details/94649479