PSNRを使って超解像の評価をしたいのですが、うまく動きません。現在でているエラーが「local variable 'files' referenced before assignment」というエラーです。解決法がわかる方がいましたら、ご教示ください。以下が実装コードです。
import
1import cv2 2import numpy as np 3import math 4 5# ディレクトリ内の画像を読み込む 6# inputpath: ディレクトリ文字列,imagesize: 画像サイズ, type_color: ColorかGray 7def load_images(inputpath, imagesize, type_color): 8 imglist = [] 9 10 for root, dirs, files in os.walk(inputpath): 11 for fn in sorted(files): 12 bn, ext = os.path.splitext(fn) 13 if ext not in [".bmp", ".BMP", ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]: 14 continue 15 16 filename = os.path.join(root, fn) 17 18 if type_color == 'Color': 19 # カラー画像の場合 20 testimage = cv2.imread(filename, cv2.IMREAD_COLOR) 21 # サイズ変更 22 height, width = testimage.shape[:2] 23 testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA) #主に縮小するのでINTER_AREA使用 24 testimage = np.asarray(testimage, dtype=np.float64) 25 # チャンネル,高さ,幅に入れ替え.data_format="channels_first"を使うとき必要 26 #testimage = testimage.transpose(2, 0, 1) 27 # チャンネルをbgrの順からrgbの順に変更 28 testimage = testimage[::-1] 29 30 elif type_color == 'Gray': 31 # グレースケール画像の場合 32 testimage = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) 33 # サイズ変更 34 height, width = testimage.shape[:2] 35 testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA) #主に縮小するのでINTER_AREA使用 36 # チャンネルの次元がないので1次元追加する 37 testimage = np.asarray([testimage], dtype=np.float64) 38 testimage = np.asarray(testimage, dtype=np.float64).reshape((1, imagesize, imagesize)) 39 # 高さ,幅,チャンネルに入れ替え.data_format="channels_last"を使うとき必要 40 testimage = testimage.transpose(1, 2, 0) 41 42 imglist.append(testimage) 43 imgsdata = np.asarray(imglist, dtype=np.float32) 44 45 return imgsdata, sorted(files) # 画像リストとファイル名のリストを返す 46 47def psnr(img1, img2): 48 mse = np.mean( (img1 - img2) ** 2 ) 49 if mse == 0: 50 return 100 51 PIXEL_MAX = 255.0 52 return 10 * math.log10(PIXEL_MAX / math.sqrt(mse)) 53 54imagelow = load_images(".\result1\8_gausian_4層_3186枚_epoch=30_k=3.png", 512, 'Gray') 55imagehigh = load_images(".\HighImage\306.png", 512, 'Gray') 56 57d = psnr(imagelow, imagehigh) 58print("d =", d) 59 60コード
回答2件
あなたの回答
tips
プレビュー