使用python opencv 将图片规范化为固定的尺寸

最近在做dukereid数据集的时候,想尝试着先将图片进行规范化,将所有的图片都设置为固定的大小,首先统计了一下所有图片的平均width和height, 使用平均值,将图片的height设置为218,width设置为84,具体的代码如下面的代码所示:

def source_img_2_normal(img_path, base_dir):  #
    img = cv.imread(img_path)
    h, w, _ = img.shape

    dst_img = np.zeros((218,84,3),dtype='uint8')

    h_selected = np.linspace(0, h-1, 218)
    h_selected = list(h_selected)
    h_selected = [ int(x) for x in h_selected ]

    w_selected = np.linspace(0, w-1, 84)
    w_selected = list(w_selected)
    w_selected = [int(x) for x in w_selected]

    for i in range(218):
        dst_img[i] = img[h_selected[i]][w_selected]
    pic_name = img_path.split('/')[-1]
    save_path = osp.join(base_dir, pic_name)
    cv.imwrite(save_path, dst_img)
    print('save images {} successfully!'.format(pic_name))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章