質問編集履歴

3

内容の変更

2018/10/01 06:32

投稿

tukutuku
tukutuku

スコア14

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

2

内容の変更

2018/10/01 06:32

投稿

tukutuku
tukutuku

スコア14

test CHANGED
File without changes
test CHANGED
File without changes

1

内容の変更

2018/10/01 06:09

投稿

tukutuku
tukutuku

スコア14

test CHANGED
@@ -1 +1 @@
1
- エラunsupported operand type(s) for -: 'NoneType' and 'NoneType'について
1
+ PSNR,SSIMのコ
test CHANGED
@@ -1,113 +1 @@
1
- 画像を評価するためにPSNRの値を出したいのですが、local variable 'files' referenced before assignment というエラーが出てきました。コードは以下の通りです。
2
-
3
-
4
-
5
- import os
6
-
7
- import cv2
8
-
9
- import numpy as np
10
-
11
- import math
12
-
13
-
14
-
15
- def load_images(inputpath, imagesize, type_color):
16
-
17
- imglist = []
18
-
19
-
20
-
21
- for root, dirs, files in os.walk(inputpath):
22
-
23
- for fn in sorted(files):
24
-
25
- bn, ext = os.path.splitext(fn)
26
-
27
- if ext not in [".bmp", ".BMP", ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]:
28
-
29
- continue
30
-
31
-
32
-
33
- filename = os.path.join(root, fn)
34
-
35
-
36
-
37
- if type_color == 'Color':
38
-
39
- testimage = cv2.imread(filename, cv2.IMREAD_COLOR)
40
-
41
- height, width = testimage.shape[:2]
42
-
43
- testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA)
44
-
45
- testimage = np.asarray(testimage, dtype=np.float64)
46
-
47
- testimage = testimage.transpose(2, 0, 1)
48
-
49
-
50
-
51
- testimage = testimage[::-1]
52
-
53
-
54
-
55
- elif type_color == 'Gray':
56
-
57
- testimage = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
58
-
59
- height, width = testimage.shape[:2]
60
-
61
- testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA)
62
-
63
- testimage = np.asarray([testimage], dtype=np.float64)
64
-
65
- testimage = np.asarray(testimage, dtype=np.float64).reshape((1, imagesize, imagesize))
66
-
67
- testimage = testimage.transpose(1, 2, 0)
68
-
69
-
70
-
71
- imglist.append(testimage)
72
-
73
- imgsdata = np.asarray(imglist, dtype=np.float32)
74
-
75
-
76
-
77
- return imgsdata, sorted(files)
78
-
79
-
80
-
81
- def psnr(img1, img2):
82
-
83
- mse = np.mean( (img1 - img2) ** 2 )
84
-
85
- if mse == 0:
86
-
87
- return 100
88
-
89
- PIXEL_MAX = 255.0
90
-
91
- return 10 * math.log10(PIXEL_MAX / math.sqrt(mse))
92
-
93
-
94
-
95
- imagelow = load_images(".\result1\8_gausian_4層_3186枚_epoch=30_k=3.png", 512, 'Gray')
96
-
97
- imagehigh = load_images(".\HighImage\306.png", 512, 'Gray')
98
-
99
-
100
-
101
- d = psnr(imagelow, imagehigh)
102
-
103
- print("d =", d)
104
-
105
-
106
-
107
-
108
-
109
- 具体的にどうすれば良いのか教えていただきたいです。
110
-
111
- またはPSNRのコードを知っている方いましたら、お願します。
1
+ PSNRとSSIMを使って画像を評価したいですが、コードを知っている方いましたら、教えてただきたいです。
112
-
113
- 加えて、SSIMのコード知りたいので、わかる方いましたらお願いします。