在图像上执行反光中心垫
Performing a reflective center pad on an image
来自活跃的 Kaggle 竞赛的 This forum thread 提到了作者称之为 "reflective center pad" 的东西。基本上这是一种变换,它获取图像的边缘并将它们向外反射,从而导致图像边缘出现镜像,作者略微展示了这一点,但显着提高了模型性能。
作为参考,这是他们 post 演示此技术的图像:
我的问题是双重的:
- 此转换是否有规范名称? "Reflective center pad" 听起来非官方。
- 用代码表达这种转换的简单方法是什么,也许使用
numpy
和 skimage
之类的东西?
回答你的第二个问题:
import Image
import numpy as np
from scipy.misc import face
# example input
f = face()[200:500:2, 400:800:2]
# example output size
outy, outx = 480, 640
iny, inx, *_ = f.shape
iny -= 1; inx -= 1
yoffs, xoffs = (outy - iny) // 2, (outx - inx) // 2
Y, X = np.ogrid[:outy, :outx]
# transformation logic is essentially contained in line below
out = f[np.abs((Y - yoffs + iny) % (2*iny) - iny), np.abs((X - xoffs + inx) % (2*inx) - inx)]
Image.fromarray(out).save('m.png')
结果:
Does this transformation have a canonical name? "Reflective center
pad" sounds unofficial.
"Symmetric padding" 是表示此转换的常用表达式。
What's a simple way of expressing this transformation in code
我认为实现该目标的最简单方法是使用 Numpy 的 pad
和 mode='symmetric'
。
演示
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
img = data.astronaut()
padded = np.pad(img, pad_width=((100, 200), (100, 500), (0, 0)), mode='symmetric')
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.imshow(img)
ax2.imshow(padded)
fig.show()
This forum thread 提到了作者称之为 "reflective center pad" 的东西。基本上这是一种变换,它获取图像的边缘并将它们向外反射,从而导致图像边缘出现镜像,作者略微展示了这一点,但显着提高了模型性能。
作为参考,这是他们 post 演示此技术的图像:
我的问题是双重的:
- 此转换是否有规范名称? "Reflective center pad" 听起来非官方。
- 用代码表达这种转换的简单方法是什么,也许使用
numpy
和skimage
之类的东西?
回答你的第二个问题:
import Image
import numpy as np
from scipy.misc import face
# example input
f = face()[200:500:2, 400:800:2]
# example output size
outy, outx = 480, 640
iny, inx, *_ = f.shape
iny -= 1; inx -= 1
yoffs, xoffs = (outy - iny) // 2, (outx - inx) // 2
Y, X = np.ogrid[:outy, :outx]
# transformation logic is essentially contained in line below
out = f[np.abs((Y - yoffs + iny) % (2*iny) - iny), np.abs((X - xoffs + inx) % (2*inx) - inx)]
Image.fromarray(out).save('m.png')
结果:
Does this transformation have a canonical name? "Reflective center pad" sounds unofficial.
"Symmetric padding" 是表示此转换的常用表达式。
What's a simple way of expressing this transformation in code
我认为实现该目标的最简单方法是使用 Numpy 的 pad
和 mode='symmetric'
。
演示
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
img = data.astronaut()
padded = np.pad(img, pad_width=((100, 200), (100, 500), (0, 0)), mode='symmetric')
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.imshow(img)
ax2.imshow(padded)
fig.show()