我正在使用 Keras 进行图像样式转移,但我陷入了通过平均像素移除零中心的部分
from __future__ import print_function
from keras.preprocessing.image import load_img, img_to_array
from scipy.misc import imsave
import numpy as np
from scipy.optimize import fmin_l_bfgs_b
import time
import argparse
from keras.applications import vgg19
from keras import backend as K
base_image_path = "images/input.jpg"
style_reference_image_path = "images/style.jpg"
result_prefix = "output"
iterations = 10
# Weights
content_weight = 0.025
style_weight = 1.0
# total variation weight
total_variation_weight = 1.0
# output
width, height = load_img(base_image_path).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
# Fit into VGG19 format
def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg19.preprocess_input(img)
return img
# Turning feature vectors into image
def deprocess_image(x):
if K.image_data_format() == 'channels_first':
x = x.reshape((3, img_nrows, img_ncols))
x = x.transpose((1, 2, 0))
else:
x = x.reshape((img_nrows, img_ncols, 3))
# (Remove zero-center by mean pixel)
x[:, :, 0] += 103.939
x[:, :, 1] += 116.779
x[:, :, 2] += 123.68
# 'BGR'->'RGB'
x = x[:, :, ::-1]
x = np.clip(x, 0, 255).astype('uint8')
return x
最后一部分(通过平均像素删除零中心),我在谷歌上搜索但找不到类似的方法。103.939、116.779 和 123.68 --> 我无法使用图像的平均值计算这些数字。
为什么会有“BGR”?他们不应该在一开始就处于“RGB”吗?