使用SRCNN进行测试
准备图片
# Read and prepare the test image for SRCNN.
def prepare_data(path):
# Settings.
data = []
label = []
padding = abs(size_input - size_label) / 2
stride = 21
# Read in image and convert to ycrcb color space.
img_input = cv.imread(path)
im = cv.cvtColor(img_input, cv.COLOR_BGR2YCR_CB)
img = im2double(im) # Only use the luminance value.传入一个图片,然后转化到YCrCB空间,并且归一化
# Create groundtruth and baseline image.
im_label = modcrop_color(img, scale=multiplier)
color_base = modcrop_color(im, scale=multiplier)
size = im_label.shape
h = size[0]
w = size[1]
im_blur = scipy.misc.imresize(im_label, 1 / multiplier, interp='bicubic')
im_input = scipy.misc.imresize(im_blur, multiplier * 1.0, interp='bicubic')将测试的图片变模糊
data = np.array(im_input[:,:,0]).reshape([1, h, w, 1])
color = np.array(color_base[:,:,1:3])
label = np.array(modcrop_color(img_input))
return data, label, colordata表示灰度空间,color保留另外两个空间,不参与运算,label是合在一起的图片
准备图片2
def prepare_raw(path):
# Settings.
data = []
color = []
# Read in image and convert to ycrcb color space.
img = cv.imread(path)
im = cv.cvtColor(img, cv.COLOR_BGR2YCR_CB)
img = im2double(im) # Only use the luminance value.
size = img.shape
img_temp = scipy.misc.imresize(img, [size[0] * multiplier, size[1] * multiplier], interp='bicubic')
color_temp = scipy.misc.imresize(im, [size[0] * multiplier, size[1] * multiplier], interp='bicubic')
im_label = img_temp[:, :, 0]
im_color = color_temp[:, :, 1:3]
data = np.array(im_label).reshape([1, img.shape[0] * multiplier, img.shape[1] * multiplier, 1])
color = np.array(im_color)
return data, colorim是转化到YCrCb空间下的图片,img是归一化的图片。
img_temp是放大之后的图片
color_temp是方法之后的归一化的图片
预测函数1
传入图片的路径和要保存的路径
# Initialization.
images = tf.placeholder(tf.float32, [None, None, None, 1], name='images')
model = SRCNN(images)传给SRCNN的是一个一维的图片,我们只是训练一个灰度图片
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Generating super-resolutioned image...')
# Load the saved checkpoint.
saver = tf.train.Saver()
if load_ckpt(sess, ckpt_dir, saver):
print('Successfully loaded checkpoint.')
else:
print('Failed to load checkpoint.')加载训练的checkpoint
if os.path.isfile(path):
test_data, test_label, color = prepare_data(path)
print('test_data has shape:', test_data.shape, 'label has shape:', test_label.shape)
print('color has shape:', color.shape)传入这个路径,然后得到这个图片的测试集(灰度),以及何在一起的图片
# Generate super-resolutioned image.
conv_out = model.eval({images: test_data})
conv_out = conv_out.squeeze()
result_bw = revert(conv_out)#从归一化恢复
result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
result[:, :, 0] = result_bw
result[:, :, 1:3] = color
result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)# result经过经过卷积神经网络的图片
bicubic = scipy.misc.imresize(test_label, 1 / multiplier, interp='bicubic')
bicubic = scipy.misc.imresize(bicubic, multiplier * 1.0, interp='bicubic')
bicubic = cv.cvtColor(bicubic, cv.COLOR_BGR2RGB)# 二三插值法
# Save the image.
save_path = os.path.join(save_dir, os.path.basename(path))
scipy.misc.imsave(save_path, result)
bicubic_path = os.path.join(save_dir, 'bicubic_' + os.path.basename(path))
scipy.misc.imsave(bicubic_path, bicubic)
print('Finished testing', path)# 分辨保存两种不同处理方式的图片
京公网安备 11010502036488号