使用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))

 

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