質問編集履歴
1
解決した方法を残す
title
CHANGED
File without changes
|
body
CHANGED
@@ -87,4 +87,92 @@
|
|
87
87
|
###補足情報
|
88
88
|
- Python 3.5.2 :: Anaconda 4.2.0
|
89
89
|
- numpy 1.13.1
|
90
|
-
- PIL.Image 1.1.7
|
90
|
+
- PIL.Image 1.1.7
|
91
|
+
|
92
|
+
### 追記(20170820)
|
93
|
+
|
94
|
+
回答により色の補正ができるようになり、問題解決しました。
|
95
|
+
最終的なソースコードを文献として残しておきます。
|
96
|
+
|
97
|
+
```python
|
98
|
+
import sys
|
99
|
+
import numpy as np
|
100
|
+
from PIL import Image
|
101
|
+
from matplotlib import pylab as plt
|
102
|
+
import cv2
|
103
|
+
|
104
|
+
if len(sys.argv) != 2:
|
105
|
+
print('python blurer.py fft_of_original.png')
|
106
|
+
sys.exit(1)
|
107
|
+
|
108
|
+
# ブレを表現する画像をつくる
|
109
|
+
size = 128
|
110
|
+
half_size = int((size - 1) / 2)
|
111
|
+
blur = Image.new('L', (size, size))
|
112
|
+
blur_pixels = blur.load()
|
113
|
+
|
114
|
+
for x in range(half_size, half_size + 5):
|
115
|
+
for y in range(half_size, half_size + 20):
|
116
|
+
blur_pixels[half_size, y] = 255
|
117
|
+
|
118
|
+
blur.save('blur.png')
|
119
|
+
|
120
|
+
# FFTと畳み込み
|
121
|
+
|
122
|
+
# ブレ画像
|
123
|
+
blur_arr = np.array(blur)
|
124
|
+
Z = np.fft.fft2(blur_arr)
|
125
|
+
Z = np.fft.fftshift(Z)
|
126
|
+
P = np.log(np.abs(Z) + 1)
|
127
|
+
P_norm = (P * 255.0) / np.amax(P)
|
128
|
+
P_norm = (P_norm - np.amin(P_norm)) / np.amax(P_norm) * 255
|
129
|
+
y = np.around(P_norm)
|
130
|
+
blur_img = np.around(P_norm)
|
131
|
+
print("BLUR MIN:%s"%np.min(y))
|
132
|
+
print("BLUR MAX:%s"%np.max(y))
|
133
|
+
img_out = Image.fromarray(y).convert('L')
|
134
|
+
img_out.save("blur_arr.png")
|
135
|
+
|
136
|
+
# 原画像
|
137
|
+
img_in = Image.open(sys.argv[1])
|
138
|
+
x = np.array(img_in.convert('L'))
|
139
|
+
dest = np.min(x)
|
140
|
+
X = np.fft.fft2(x)
|
141
|
+
X = np.fft.fftshift(X)
|
142
|
+
P = np.log(np.abs(X) + 1)
|
143
|
+
P_norm = (P * 255.0) / np.amax(P)
|
144
|
+
y = np.around(P_norm)
|
145
|
+
# dest = np.min(y)
|
146
|
+
fftshift = np.around(P_norm)
|
147
|
+
print("ORIGINAL MIN:%s"%np.min(y))
|
148
|
+
print("ORIGINAL MAX:%s"%np.max(y))
|
149
|
+
img_out = Image.fromarray(y).convert('L')
|
150
|
+
img_out.save("fftshift.png")
|
151
|
+
|
152
|
+
# 畳み込み結果
|
153
|
+
W = np.fft.fftshift(np.fft.ifft2(Z * X))
|
154
|
+
P = np.log(np.abs(W) + 1)
|
155
|
+
P_norm = P / np.amax(P) * 255
|
156
|
+
src = np.min(P_norm)
|
157
|
+
y = np.around(P_norm)
|
158
|
+
print("CONV MIN:%s"%np.min(y))
|
159
|
+
print("CONV MAX:%s"%np.max(y))
|
160
|
+
img_out = Image.fromarray(y).convert('L')
|
161
|
+
img_out.save("conv.png")
|
162
|
+
|
163
|
+
# ガンマ補正
|
164
|
+
|
165
|
+
gamma = np.log(src/255) / np.log(dest/255)
|
166
|
+
|
167
|
+
print(gamma)
|
168
|
+
lookUpTable = np.zeros((256, 1), dtype = 'uint8')
|
169
|
+
for i in range(256):
|
170
|
+
if i == 0:
|
171
|
+
lookUpTable[i][0] = 255 * pow(float(0.000001) / 255, 1.0 / gamma)
|
172
|
+
else:
|
173
|
+
lookUpTable[i][0] = 255 * pow(float(i) / 255, 1.0 / gamma)
|
174
|
+
|
175
|
+
img = cv2.imread('conv.png', 1)
|
176
|
+
img_gamma = cv2.LUT(img, lookUpTable)
|
177
|
+
cv2.imwrite('conv-lut.png', img_gamma)
|
178
|
+
```
|