【tensorflow2.0】实例5

import cv2
import numpy as np
from scipy import misc
i = misc.ascent()

首先,将设置输入,然后从scipy库中导入misc库,misc.ascent会返回一张很好的图像。

import matplotlib.pyplot as plt
plt.grid(False)
plt.gray()
plt.axis('off')#不显示坐标轴
#显示图片的第i个通道
plt.imshow(i)
plt.show()

matplotlib包含了绘图的函数,它可以通过colab把图片呈现在浏览器中。执行完这个代码之后,可以看到scipy输出的图像。
在此介绍一下matplotlib的plt.grid用法
plt.grid()、plt.grid(1)、plt.grid(True)、plt.grid(b=True)、plt.grid(b=1)都表示显示网格线的意思;
plt.grid(False)、plt.grid(0)为不显示网格线;
plt.grid(b=True, axis=‘x’)为只显示x轴网格线;
plt.grid(b=True, axis=‘y’)为只显示y轴网格线;
plt.grid(b=1, which=‘major’)默认就是major,即占比极小的就不显示了;若which='both’则显示;若which='minor’就不显示网格。

i_transformed = np.copy(i)
size_x = i_transformed.shape[0]
size_y = i_transformed.shape[1]
# This filter detects edges nicely
# It creates a convolution that only passes through sharp edges and straight
# lines.

#Experiment with different values for fun effects.
#filter = [ [0, 1, 0], [1, -4, 1], [0, 1, 0]]

# A couple more filters to try for fun!
filter = [ [-1, -2, -1], [0, 0, 0], [1, 2, 1]]
#filter = [ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]

# If all the digits in the filter don't add up to 0 or 1, you 
# should probably do a weight to get it to do so
# so, for example, if your weights are 1,1,1 1,2,1 1,1,1
# They add up to 10, so you would set a weight of .1 if you want to normalize them
weight  = 1

接下来,创建一个卷积,形式为3×3的数组。先给它加载一些可以很好地识别尖锐边缘的值。遍历整张图像,留下一个像素的边距,就可以发现循环是从1开始而不是从0开始的,而且最终图像大小会变成(x-1)×(y-1)。在下面的循环中,可以通过查看某个像素,以及它的邻近像素,然后用像素值与滤波器上的对应值相乘,最后把所有乘积相加,从而计算卷积值,

for x in range(1,size_x-1):
  for y in range(1,size_y-1):
      convolution = 0.0
      convolution = convolution + (i[x - 1, y-1] * filter[0][0])
      convolution = convolution + (i[x, y-1] * filter[0][1])
      convolution = convolution + (i[x + 1, y-1] * filter[0][2])
      convolution = convolution + (i[x-1, y] * filter[1][0])
      convolution = convolution + (i[x, y] * filter[1][1])
      convolution = convolution + (i[x+1, y] * filter[1][2])
      convolution = convolution + (i[x-1, y+1] * filter[2][0])
      convolution = convolution + (i[x, y+1] * filter[2][1])
      convolution = convolution + (i[x+1, y+1] * filter[2][2])
      convolution = convolution * weight
      if(convolution<0):
        convolution=0
      if(convolution>255):
        convolution=255
      i_transformed[x, y] = convolution

接下来将结果画出来:

# Plot the image. Note the size of the axes -- they are 512 by 512
plt.gray()
plt.grid(False)
plt.imshow(i_transformed)
#plt.axis('off')
plt.show() 

可以发现,只有一些特定的特征通过了滤波器。改变前面的滤波器,就会得到不一样的图像结果。

接下来,看看池化:(本例中是最大池化)

new_x = int(size_x/2)
new_y = int(size_y/2)
newImage = np.zeros((new_x, new_y))
for x in range(0, size_x, 2):
  for y in range(0, size_y, 2):
    pixels = []
    pixels.append(i_transformed[x, y])
    pixels.append(i_transformed[x+1, y])
    pixels.append(i_transformed[x, y+1])
    pixels.append(i_transformed[x+1, y+1])
    newImage[int(x/2),int(y/2)] = max(pixels)

# Plot the image. Note the size of the axes -- now 256 pixels instead of 512
plt.gray()
plt.grid(False)
plt.imshow(newImage)
#plt.axis('off')
plt.show()      

从4块像素中,只让最大的一个像素值通过。可以发现,图像的特征得到保留,但从坐标轴来看,它们更紧密了;同时图像的大小变成了原来的一半(从500变成了250)。

总结一下:
卷积的工作原理是:在封装下,tensorflow在图像上尝试不同的滤波器,然后根据训练数据,学习哪一个更管用。因此,当它起作用的时候,可以大大减少通过这个网络的信息,但是因为它能分离和识别特征,我们可以得到更高的精度。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值