skimage文档是这样说的
skimage.transform.resize( image , output_shape , order=None , mode=‘reflect’ , cval=0 , clip=True , preserve_range=False , anti_aliasing=None , anti_aliasing_sigma=None )
Performs interpolation to up-size or down-size N-dimensional images.
mode{‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}, optional Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad.
numpy文档又是这么说的
‘constant’ (default) Pads with a constant value.
‘edge’ Pads with the edge values of array.
‘reflect’ Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
‘symmetric’ Pads with the reflection of the vector mirrored along the edge of the array.
‘wrap’ Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
pad和插值应该有区别吧???
============================
tf2.0的resize
tf.image.resize( images, size, method=ResizeMethod.BILINEAR, preserve_aspect_ratio=False, antialias=False, name=None )
其中 method 有四种选择:
ResizeMethod.BILINEAR :双线性插值
ResizeMethod.NEAREST_NEIGHBOR : 最近邻插值
ResizeMethod.BICUBIC : 双三次插值
ResizeMethod.AREA :面积插值
这才是正儿八经的用插值来resize图片吧?!!
实验
从实验层面来看一下他们之间的区别究竟有多大。 思路:加载一批图片,分别用上述两种resize方法进行处理,然后算一下他们之间的psnr值看看。
#加载cifar10的测试集
x_test, y_test = get_data_set("test")
#随便整几张图片试试
x_test = x_test[:33]
# img = x_test[0,:,:,:]
#
# print(img.shape)
# plt.imshow(img)
# plt.show()
# aa = tf.image.resize(img, [48, 48]).numpy()
# print(aa.shape)
# plt.imshow(aa)
# plt.show()
from skimage.transform import resize
X_test = np.array([resize(x, output_shape=(48, 48)) for x in x_test])
import math
def get_psnr(img1, img2):
mse = np.mean( (img1/1. - img2/1.) ** 2 )
if mse < 1.0e-10:
return 100
PIXEL_MAX = 1
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
#四种方式
bb = [tf.image.ResizeMethod.BILINEAR,tf.image.ResizeMethod.NEAREST_NEIGHBOR,tf.image.ResizeMethod.BICUBIC , tf.image.ResizeMethod.AREA]
for j in range(4):
for i in range(32):
temp = tf.image.resize(x_test[i, :, :, :], [48, 48], method=bb[j]).numpy()
psnr = get_psnr(temp,X_test[i, :, :, :])
print(psnr,end=" ")
print("=====================")
输出
46.669033558302885 48.90798774917477 53.38381232542708 48.43096027380447 46.63530041008276 49.853715847548045 46.97129446274798 57.92736516572543 52.02712480456207 47.9270666009561 56.815827971818976 43.58148330692217 49.15912003347911 45.571104617307476 44.778364129271104 46.13820707579342 43.018938041725974 49.702719907722226 48.09563717071776 49.98946646853693 45.87546487951293 63.99499287231072 53.94231731555267 43.738499959122024 46.908452504151406 44.42300374351233 47.622822312811095 50.28564910040936 45.20506178645428 49.319071870542366 47.76259998082413 55.95695969034264 ===================== 26.97628677450534 26.924861715004923 29.183922133559385 28.454948098090075 27.641430200201178 29.73183032295352 25.466182577940355 31.338492508076765 29.9067674657335 26.197798316302723 31.13589159316287 25.056555401591417 28.983519879947536 23.96870655942354 25.427217596503308 26.396676988617553 25.754045363673626 27.128767028700782 28.461873499287837 27.005025244577926 24.871044509277738 25.537881905908407 35.51861682838789 23.738132391790444 28.941631597341658 27.680023619665302 28.389742788850572 28.745888550449724 24.902620199920307 29.115073004421962 29.751666906604072 32.08766152133068 ===================== 37.03543857489388 37.259828641504036 40.213764737505635 38.04380796549642 36.7790259503914 39.23595602822592 35.77335658889253 42.27602482590236 40.28042495809771 37.0365490315566 42.2178204137436 35.00559003779734 38.91261259423612 34.711268849468595 35.35611781891313 36.27121114979309 35.65962402278565 37.664839650830196 38.37510414747462 37.376202536896336 36.05599500393482 36.64829305721068 45.38580840924415 34.07303854523669 38.97407018442087 37.5050909468706 39.13560505488175 39.3079174725139 35.70591190864245 38.35261549238907 39.2579847332357 43.88169978322925 ===================== 34.77223282063561 34.00181912589067 36.918973301639596 34.3329021393655 34.247488624884085 36.45586961708923 32.32954212272813 38.57978679534951 37.007158514958 34.32080115763002 38.97647966809106 32.45896604574029 35.47081126975141 31.825603643146007 32.64746063172551 33.70869438835973 33.031301832751815 34.430776528882056 35.08218773125719 34.47020053917494 33.410030293674254 32.82951933890276 43.05155492385302 31.716446871714886 36.40419415215108 35.800352782290055 35.61569268275426 36.03531143542608 32.84526522882461 35.80328351242286 37.49832733079886 39.793573612797786 =====================
显然,tf的双线性插值模式的psnr值最大,50左右。我们知道,这个值其实也不低了。我也试了,用skimage的resize处理的图片训练的模型来预测用tf.resize处理的图片。效果也不差。
|