mfc动态改变clip风格_图像风格转化实现模拟清明上河图

【源码分享】通过迁移算法实现图像风格转换,python语言实现:

179ae65a2d072bde00d63ad5bd1a18ec.png

更多解析可以百度我的文章“AI模拟清明上河图

部分代码:
#返回值为_style_loss的值*0.5,1,1.5,4的加和def style_loss_func(sess, model):   def _gram_matrix(F, N, M):      Ft = tf.reshape(F, (M, N))      return tf.matmul(tf.transpose(Ft), Ft)   #a,x都为'conv1_1', conv2_1', 'conv3_1', 'conv4_1','conv5_1'中的参数遍历   def _style_loss(a, x):      #同内容损失函数      N = a.shape[3]      M = a.shape[1] * a.shape[2]      A = _gram_matrix(a, N, M)      G = _gram_matrix(x, N, M)      return (1 / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow(G - A, 2))   return sum([_style_loss(sess.run(model[layer_name]), model[layer_name]) * w for layer_name, w in STYLE_LAYERS])再定义生成图片,读取图片,保存图片函数:#产生噪声图片def generate_noise_image(content_image, noise_ratio=NOISE_RATIO):   #随机产生矩阵图片,矩阵元素内容符合标准正太分布   noise_image = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, COLOR_C)).astype('float32')   #将产生的矩阵内各元素与神经网络加和   input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio)   return input_image#读取图片,改变尺寸,变成1行多列矩阵,将矩阵与初始值相减返回def load_image(path):   image = scipy.misc.imread(path)   image = scipy.misc.imresize(image, (IMAGE_H, IMAGE_W))   #image.shape为[800,600,3],则(1, ) + image.shape)为[1,800,600,3]   image = np.reshape(image, ((1, ) + image.shape))   #MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))   #其中image为三通道矩阵,MEAN_VALUES为三维矩阵可以相减   image = image - MEAN_VALUES   return image#保存图片def save_image(path, image):   image = image + MEAN_VALUES   #参见上面图像加载时多加了1维,故形成时要减少维度,   image = image[0]   #截取所有数值在0-255之间的,因为像素值必须是这个范围。而参数运算后可能会超过这个值   image = np.clip(image, 0, 255).astype('uint8')   #保存   scipy.misc.imsave(path, image)

完整代码:后台回复‘风格迁移’

da5b2a62b32b9cf2b31df61c8168fed0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值