teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

内容の変更

2018/10/01 06:32

投稿

tukutuku
tukutuku

スコア14

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

2

内容の変更

2018/10/01 06:32

投稿

tukutuku
tukutuku

スコア14

title CHANGED
File without changes
body CHANGED
File without changes

1

内容の変更

2018/10/01 06:09

投稿

tukutuku
tukutuku

スコア14

title CHANGED
@@ -1,1 +1,1 @@
1
- エラunsupported operand type(s) for -: 'NoneType' and 'NoneType'について
1
+ PSNR,SSIMのコ
body CHANGED
@@ -1,57 +1,1 @@
1
- 画像を評価するためにPSNRの値を出したいのですが、local variable 'files' referenced before assignment というエラーが出てきました。コードは以下の通りです。
2
-
3
- import os
4
- import cv2
5
- import numpy as np
6
- import math
7
-
8
- def load_images(inputpath, imagesize, type_color):
9
- imglist = []
10
-
11
- for root, dirs, files in os.walk(inputpath):
12
- for fn in sorted(files):
13
- bn, ext = os.path.splitext(fn)
14
- if ext not in [".bmp", ".BMP", ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]:
15
- continue
16
-
17
- filename = os.path.join(root, fn)
18
-
19
- if type_color == 'Color':
20
- testimage = cv2.imread(filename, cv2.IMREAD_COLOR)
21
- height, width = testimage.shape[:2]
22
- testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA)
23
- testimage = np.asarray(testimage, dtype=np.float64)
24
- testimage = testimage.transpose(2, 0, 1)
25
-
26
- testimage = testimage[::-1]
27
-
28
- elif type_color == 'Gray':
29
- testimage = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
30
- height, width = testimage.shape[:2]
31
- testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA)
32
- testimage = np.asarray([testimage], dtype=np.float64)
33
- testimage = np.asarray(testimage, dtype=np.float64).reshape((1, imagesize, imagesize))
34
- testimage = testimage.transpose(1, 2, 0)
35
-
36
- imglist.append(testimage)
37
- imgsdata = np.asarray(imglist, dtype=np.float32)
38
-
39
- return imgsdata, sorted(files)
40
-
41
- def psnr(img1, img2):
42
- mse = np.mean( (img1 - img2) ** 2 )
43
- if mse == 0:
44
- return 100
45
- PIXEL_MAX = 255.0
46
- return 10 * math.log10(PIXEL_MAX / math.sqrt(mse))
47
-
48
- imagelow = load_images(".\result1\8_gausian_4層_3186枚_epoch=30_k=3.png", 512, 'Gray')
49
- imagehigh = load_images(".\HighImage\306.png", 512, 'Gray')
50
-
51
- d = psnr(imagelow, imagehigh)
52
- print("d =", d)
53
-
54
-
55
- 具体的にどうすれば良いのか教えていただきたいです。
56
- またはPSNRのコードを知っている方いましたら、お願します。
1
+ PSNRとSSIMを使って画像を評価したいですが、コードを知っている方いましたら、教えてただきたいです。
57
- 加えて、SSIMのコード知りたいので、わかる方いましたらお願いします。