TensorFlow卷积处理图片

简介

通过卷积我们可以提取图片的轮廓 可以进行图片平滑处理 可以处理彩色图片
 

黑白图片处理

导包
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline

原图
在这里插入图片描述

flower = plt.imread('./flower.png')
flower.shape
plt.imshow(flower,cmap = plt.cm.gray)

data = flower.reshape(1,332,444,1)
# 浮雕效果 卷积核
filter_ = np.array([[-1,-1,0],[-1,0,1],[0,1,1]]).reshape(3,3,1,1)
# neural network
# KNN ----> nearest neighbors
# CNN ---->convolution neural network:卷积神经网络
conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
with tf.Session() as sess:
    image = sess.run(conv).reshape(332,444)
plt.imshow(image,cmap = plt.cm.gray) #调整颜色

在这里插入图片描述
修改卷积核

data = flower.reshape(1,332,444,1)
# 强调边缘
filter_ = np.array([[1,1,1],[1,-10,1],[1,1,1]]).reshape(3,3,1,1)
# neural network
# KNN ----> nearest neighbors
# CNN ---->convolution neural network:卷积神经网络
conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
with tf.Session() as sess:
    image = sess.run(conv).reshape(332,444)
plt.imshow(image,cmap = plt.cm.gray)

在这里插入图片描述

data = flower.reshape(1,332,444,1)
# 边缘检测
filter_ = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]]).reshape(3,3,1,1)
# neural network
# KNN ----> nearest neighbors
# CNN ---->convolution neural network:卷积神经网络
conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
with tf.Session() as sess:
    image = sess.run(conv).reshape(332,444)
plt.imshow(image,cmap = plt.cm.gray)

在这里插入图片描述
卷积核也可以通过随机数进行生成 正太分布 效果会有变化
filter_ = np.random.randn(3,3,1,1)
 

图片平滑

原图 有噪声
在这里插入图片描述

均值滤波平滑原理

平滑均值滤波
该卷积核的作用在于取九个值的平均值代替中间像素值,所以起到的平滑的效果
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf

plt.figure(figsize=(12,9))
moon = plt.imread('./moonlanding.png')
#一般图片后缀以png结尾 图片是float类型值
plt.imshow(moon,cmap = plt.cm.gray)
data = moon.reshape(1,474,630,1)

# 均值平滑,滤波 将噪声清除掉 变得平滑 求平均值
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:
    image = sess.run(conv).reshape(474,630)
plt.figure(figsize=(12,9))
plt.imshow(image,cmap = plt.cm.gray)

在这里插入图片描述

高斯平滑原理

高斯平滑
高斯平滑水平和垂直方向呈现高斯分布,更突出了中心点在像素平滑后的权重,相比于均值滤波而言,有着更好的平滑效果
在这里插入图片描述

data = moon.reshape(1,474,630,1)
# 高斯平滑,滤波 更丝润平滑一点
filter_ = np.array([[1/16,2/16,1/16],[2/16,4/16,2/16],[1/16,2/16,1/16]]).reshape(3,3,1,1)
conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
    image = sess.run(conv).reshape(474,630)
plt.figure(figsize=(12,9))
plt.imshow(image,cmap = plt.cm.gray)

在这里插入图片描述
这就是卷积核起名为filter的原因 意为 过滤掉不重要的东西 留下特征
 

彩色图片处理

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
image = plt.imread('./sence.jpg') #图片自己找
plt.imshow(image)
image.shape

在这里插入图片描述

data = image.reshape(1,662,1000,3).astype(np.float32)
# and a filter / kernel tensor of shape
# `[filter_height, filter_width, in_channels(和上面3对应), 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:
    result = sess.run(conv).reshape(662,1000,3)
plt.imshow(result.astype(np.uint8)) 
#输出的三个通道的值完全一样 所以是黑白图片

在这里插入图片描述

data = image.reshape(1,662,1000,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
#随机数 /13是因为有的像素超过1了
conv = tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME')
with tf.Session() as sess:
    result = sess.run(conv).reshape(662,1000,3)
plt.imshow(result.astype(np.uint8))

在这里插入图片描述

卷积,对每个通道进行卷积运算
data = image.reshape(1,662,1000,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,662,1000)
    result = np.transpose(result,[1,2,0])
    print(result.shape)
plt.figure(figsize=(16,12))
plt.imshow(result.astype(np.uint8))

在这里插入图片描述

data = image.reshape(1,662,1000,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,662,1000)
    result = np.transpose(result,[1,2,0])
    print(result.shape)
plt.figure(figsize=(16,12))
plt.imshow(result.astype(np.uint8))

在这里插入图片描述
一张图片清楚 需要每个像素值之间差距大才可以 求他们之间的平均值代替 会变得模糊 元素值之间变得差不多了

人选出来 背景虚化
plt.imshow(image[230:600,420:550])

在这里插入图片描述

data = image.reshape(1,662,1000,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,662,1000)
    result = np.transpose(result,[1,2,0])
    print(result.shape)
plt.figure(figsize=(16,12))
result = result.astype(np.uint8)
result[230:600,420:550] = image[230:600,420:550]
plt.imshow(result)

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值