1. 程式人生 > >深度學習,opencv讀取圖片,歸一化,顯示,多張圖片顯示

深度學習,opencv讀取圖片,歸一化,顯示,多張圖片顯示

import numpy as np
import cv2

def cv_norm_proc(img): # cv_norm_proc函式將圖片歸一化 [-1,1]
    img_rgb = (img / 255. - 0.5) * 2
    return img_rgb

def cv_inv_proc(img): # cv_inv_proc函式將讀取圖片時歸一化的圖片還原成影象
    img_rgb = (img + 1.) * 127.5
    return img_rgb.astype(np.float32)

def get_write_picture(x_image, y_image, fake_y, fake_x_, fake_x, fake_y_):
    x_image = cv_inv_proc(x_image)
    y_image = cv_inv_proc(y_image)
    fake_y = cv_inv_proc(fake_y)
    fake_x_ = cv_inv_proc(fake_x_)
    fake_x = cv_inv_proc(fake_x)
    fake_y_ = cv_inv_proc(fake_y_)
    row1 = np.concatenate((x_image, fake_y, fake_x_), axis=1) # 第一行
    row2 = np.concatenate((y_image, fake_x, fake_y_), axis=1) # 第二行
    output = np.concatenate((row1, row2), axis=0)
    return output

if __name__ == '__main__':
    img_path = r"n02381460_140.jpg"
    img = cv2.imread(img_path)

    img_norm = cv_norm_proc(img)

    # img_rgb = cv_inv_proc(img_norm).astype(np.uint8)
    img_rgb = cv_inv_proc(img_norm)

    cv2.imwrite("img.jpg",img_rgb)

    cv2.imshow('img', img_rgb)
    cv2.waitKey(0)
    cv2.destroyAllWindows()