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)
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))
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))
卷积,对每个通道进行卷积运算
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)
加模糊程度
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)
# 替代imag内容 不模糊
plt.imshow(image[330:380,280:420])
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)
来源:CSDN
作者:网络毒刘
链接:https://blog.csdn.net/qq_41856814/article/details/103480350