0
点赞
收藏
分享

微信扫一扫

【tensorflow】使用convolution处理图片

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
# plt.figure(figsize=(12,9))

image = plt.imread('./caise.jpg')
plt.imshow(image)
image.shape

(388, 690, 3)

【tensorflow】使用convolution处理图片_卷积

data = image.reshape(1,388, 690, 3).astype(np.float32)

# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels, out_channels]
filter_ = np.array([[1/27]*81]).reshape(3,3,3,3)
conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
image = sess.run(conv).reshape(388, 690,3)
plt.imshow(image.astype(np.uint8))

【tensorflow】使用convolution处理图片_卷积_02

388*690*3

803160

data = image.reshape(1,388, 690, 3).astype(np.float32)
# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels, out_channels]
filter_ = np.random.rand(3,3,3,3)/13
conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
image = sess.run(conv).reshape(388, 690,3)
plt.imshow(image.astype(np.uint8))

【tensorflow】使用convolution处理图片_2d_03

卷积,对每个通道进行卷积运算

data = image.reshape(1,388, 690, 3).astype(np.float32)

# data.shape = (3,662,1000,1)
data = np.transpose(data,axes = [3,1,2,0])

# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels, out_channels]
filter_ = np.array([[1/9]*9]).reshape(3,3,1,1)

conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
result = sess.run(conv).reshape(3,388, 690)
result = np.transpose(result,[1,2,0])
print(result.shape)
plt.figure(figsize=(16,12))
plt.imshow(result.astype(np.uint8))

(388, 690, 3)

【tensorflow】使用convolution处理图片_卷积_04

加模糊程度

data = image.reshape(1,388, 690,3).astype(np.float32)

# data.shape = (3,662,1000,1)
data = np.transpose(data,axes = [3,1,2,0])

# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels, out_channels]
filter_ = np.array([[1/49]*49]).reshape(7,7,1,1)

conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
result = sess.run(conv).reshape(3,388, 690)
result = np.transpose(result,[1,2,0])
print(result.shape)
plt.figure(figsize=(16,12))
plt.imshow(result.astype(np.uint8))

(388, 690, 3)

【tensorflow】使用convolution处理图片_卷积_05

# 替代imag内容 不模糊
plt.imshow(image[330:380,280:420])

【tensorflow】使用convolution处理图片_2d_06

data = image.reshape(1,388, 690,3).astype(np.float32)

# data.shape = (3,662,1000,1)
data = np.transpose(data,axes = [3,1,2,0])

# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels, out_channels]
filter_ = np.array([[1/49]*49]).reshape(7,7,1,1)

conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
result = sess.run(conv).reshape(3,388, 690,)
result = np.transpose(result,[1,2,0])
print(result.shape)
plt.figure(figsize=(16,12))
result = result.astype(np.uint8)

result[330:380,280:420]= image[330:380,280:420]

plt.imshow(result)
(388, 690, 3)

【tensorflow】使用convolution处理图片_卷积_07


举报

相关推荐

0 条评论