MTCNN中构建图像金字塔

MTCNN中构建图像金字塔

MTCNN在进行人脸检测任务时,输入是一张图像,输出图像中人脸所在位置的Bounding Box。MTCNN基于卷积神经网络,通常只适用于检测一定尺寸范围内的人脸,比如其中的P-Net,用于判断12×12大小范围内是否含有人脸,但是输入图像中人脸的尺寸未知,需要构建图像金字塔获得不同尺寸的图像,缩放图像是为了将图像中的人脸缩放到网络能检测的适宜尺寸,只要某个人脸被放缩到12×12左右,就可以被检测出来,下图为MTCNN人脸检测流程。
在这里插入图片描述
构建金字塔需要解决几个问题:

1. 金字塔要建多少层,即一共要生成多少张图像

2. 每张图像的尺寸如何确定
在人脸检测时,通常要设置原图中要检测的最小人脸尺寸,原图中小于这个尺寸的人脸可以忽略,MTCNN代码中为minsize=20,MTCNN P-Net用于检测12×12大小的人脸。
人脸检测中的图像金字塔构建,涉及如下数据:
输入图像尺寸,定义为(h, w) = (100, 120)
最小人脸尺寸,定义为 min_face_size = 20
最大人脸尺寸,如果不设置,为图像高宽中较短的那个,定义为max_face_size = 100
网络能检测的人脸尺寸,定义为net_face_size = 12
金字塔层间缩放比率,定义为factor = 0.709
图像金字塔中
最大缩放尺度max_scale = net_face_size / min_face_size = 12 / 20,
最小缩放尺度min_scale = net_face_size / max_face_size = 12 / 100,
中间的缩放尺度scale_n = max_scale * (factor ^ n) = ,
对应的图像尺寸为(h_n, w_n) = (h * scale_n, w_n * scale_n)
举例解释说明:
当原图(100, 120)中有一个最小人脸(20, 20)时,
(20, 20)>(12, 12)图像缩小比(为缩放尺寸的倒数)最小20 / 12,原图由(100, 120)>(60, 72),图像(60, 72)为图像金字塔的最底层。根据金字塔层间缩放比率,每层图像的尺寸为:
(h_n, w_n) = (h * scale_n, w_n * scale_n)
=(100×12/20×0.709^ n,120×12/20×0.709^ n)
同时需要保证min(h_n, w_n) >net_face_size
附上代码:

    def processed_image(img, scale):
        height, width, channels = img.shape
        new_height = int(height * scale)  
        new_width = int(width * scale)  
        new_dim = (new_width, new_height)
        img_resized = cv2.resize(img, new_dim, interpolation=cv2.INTER_LINEAR)  # resized image
        img_resized = (img_resized - 127.5) / 128   
        return img_resized
    
    def pyramid_image(img):
        net_size = 12       
        min_face_size = 20
        current_scale = float(net_size) / self.min_face_size  # find initial scale
        im_resized = processed_image(img, current_scale)  # the first layer of image pyramid
        current_height, current_width, _ = im_resized.shape
        while min(current_height, current_width) > net_size:
            current_scale *= self.scale_factor
            im_resized = processed_image(im, current_scale)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章