1. 导入相关模块
import numpy as np
import random
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
import matplotlib as mpl
import matplotlib.ticker as ticker
from scipy.interpolate import make_interp_spline
!gdown --id 1fsKERl26TNTFIY25PhReoCujxwJvfyHn
zhfont = mpl.font_manager.FontProperties(fname='SimHei .ttf')
zhfont2 = mpl.font_manager.FontProperties(fname='New Times Roman .ttf')
2.创建图片
img01 = np.zeros([16,16])
for i in range(16):
for j in range(16):
if ((i>=0 and i<=2) or (i>=6 and i<=8) or (i>=13 and i<=15)):
img01[i][j] = -1
else:
if (j>=6 and j<=9):
img01[i][j] = -1
else:
img01[i][j] = 1
plt.figure(figsize=(3,3))
plt.subplot(111)
plt.imshow(img01,cmap ='gray')
def get_diamond(I):
img = np.ones([16,16])
for i in range(8):
for j in range(8):
if ((i+j)>=7):
img[i][j] = I
for i in range(8):
for j in range(8,16,1):
img[i][j] = img[i][15-j]
for i in range(8,16,1):
for j in range(16):
img[i][j] = img[15-i][j]
return img
plt.figure(figsize=(3,3))
plt.subplot(111)
plt.imshow(get_diamond(-0.5),cmap ='gray')
def get_square(I):
img = np.ones([16,16])
for i in range(16):
for j in range(16):
if (i >= 3 and i <= 12) and (j >= 3 and j <= 12):
img[i][j] = I
return img
plt.figure(figsize=(3,3))
plt.subplot(111)
plt.imshow(get_square(-0.5),cmap ='gray')
def get_zhong(I):
A = np.ones([16,16])
for i in range(16):
for j in range(16):
if i != 0 and i != 15 and (j==7 or j==8):
A[i][j] = I
for i in range(16):
for j in range(16):
if i<=10 and i >=4 and (j==2 or j==3 or j==12 or j==13):
A[i][j] = I
for i in range(16):
for j in range(16):
if (i==4 or i==5 or i==8 or i==9 )and (j>=2 and j<=13):
A[i][j] = I
return A
plt.figure(figsize=(3,3))
plt.subplot(111)
plt.imshow(get_zhong(-0.5),cmap ='gray')
def get_bu(I):
A = np.ones([16,16])
for i in range(16):
for j in range(16):
if (i == 1 or i == 2) and (j > 0 and j < 15):
A[i][j] = I
for i in range(16):
for j in range(16):
if (i >= 3 and i <= 14) and (j == 7 or j == 8):
A[i][j] = I
for i in range(16):
for j in range(16):
if (i >= 4 and i <= 9):
A[i][10-i] = I
A[i][11-i] = I
for i in range(16):
for j in range(8,16,1):
A[i][j] = A[i][15-j]
return A
plt.figure(figsize=(3,3))
plt.subplot(111)
plt.imshow(get_bu(-0.5),cmap ='gray')
3.加入噪音
def gasuss_noise02(image, mean=0, var=0.1):
noise = np.random.normal(mean, var, image.shape)
return noise+image
img01_noise1 = gasuss_noise02(image=img01,mean=0,var=0.2)
img01_noise2 = gasuss_noise02(image=img01,mean=0,var=0.4)
img01_noise3 = gasuss_noise02(image=img01,mean=0,var=0.6)
img01_noise4 = gasuss_noise02(image=img01,mean=0,var=1.0)
plt.figure(figsize=(5,5))
plt.figure(1)
plt.subplot(221)
plt.imshow(img01_noise1,cmap ='gray')
plt.subplot(222)
plt.imshow(img01_noise2,cmap ='gray')
plt.subplot(223)
plt.imshow(img01_noise3,cmap ='gray')
plt.subplot(224)
plt.imshow(img01_noise4,cmap ='gray')
plt.suptitle(u"4种不同方差高斯噪音效果图",fontproperties=zhfont,fontsize=14,y=0.05)
def sp_noise(image,prob):
'''
添加椒盐噪声
prob:噪声比例
'''
output = np.zeros(image.shape)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = -0.5
elif rdn > thres:
output[i][j] = 0.5
else:
output[i][j] = image[i][j]
return output
img01_noise5 = sp_noise(image=img01, prob=0.08)
plt.figure(figsize=(3,3))
plt.imshow(img01_noise5,cmap ='gray')
plt.suptitle(u"非高斯噪音",fontproperties=zhfont,fontsize=14,y=0.02)
4.Cloning Template
filters_4a = tf.constant([
[[0.0],[1.0],[0.0]],
[[1.0],[2.0],[1.0]],
[[0.0],[1.0],[0.0]],
],dtype=tf.float32)
filters_4b = tf.constant([
[[0.0],[1.0],[0.0]],
[[1.0],[4.0],[1.0]],
[[0.0],[1.0],[0.0]],
],dtype=tf.float32)
filters_4c = tf.constant([
[[0.5],[1.0],[0.5]],
[[1.0],[4.0],[1.0]],
[[0.5],[1.0],[0.5]],
],dtype=tf.float32)
filters_4d = tf.constant([
[[0.0],[0.0],[0.0]],
[[0.0],[4.0],[0.0]],
[[0.0],[0.0],[0.0]],
],dtype=tf.float32)
filters_15 = tf.constant([
[[0.0],[-0.5],[0.0]],
[[-0.5],[2.0],[-0.5]],
[[0.0],[-0.5],[0.0]],
],dtype=tf.float32)
filters_17 = tf.constant([
[[0.0],[-1],[0.0]],
[[-1.0],[4.0],[-1.0]],
[[0.0],[-1],[0.0]],
],dtype=tf.float32)
filters_18a = tf.constant([
[[0.0],[0.0],[0.0]],
[[0.0],[2.0],[0.0]],
[[0.0],[0.0],[0.0]],
],dtype=tf.float32)
filters_18b = tf.constant([
[[-0.25],[-0.25],[-0.25]],
[[-0.25],[2.0],[-0.25]],
[[-0.25],[-0.25],[-0.25]],
],dtype=tf.float32)
filters_19 = tf.constant([
[[1.0],[1.0],[1.0]],
[[1.0],[-8.0],[1.0]],
[[1.0],[1.0],[1.0]],
],dtype=tf.float32)
print('Cloning templates: Fig.4(a)')
print(tf.reshape(tf.convert_to_tensor(filters_4a,dtype=tf.float32), [3,3]).numpy())
print('Cloning templates: Fig.4(b)')
print(tf.reshape(tf.convert_to_tensor(filters_4b,dtype=tf.float32), [3,3]).numpy())
print('Cloning templates: Fig.4(c)')
print(tf.reshape(tf.convert_to_tensor(filters_4c,dtype=tf.float32), [3,3]).numpy())
print('Cloning templates: Fig.4(d)')
print(tf.reshape(tf.convert_to_tensor(filters_4d,dtype=tf.float32), [3,3]).numpy())
filters_4a = tf.reshape(filters_4a, [3, 3, 1, 1])
filters_4b = tf.reshape(filters_4b, [3, 3, 1, 1])
filters_4c = tf.reshape(filters_4c, [3, 3, 1, 1])
filters_4d = tf.reshape(filters_4d, [3, 3, 1, 1])
filters_15 = tf.reshape(filters_15, [3, 3, 1, 1])
filters_17 = tf.reshape(filters_17, [3, 3, 1, 1])
filters_18a = tf.reshape(filters_18a, [3, 3, 1, 1])
filters_18b = tf.reshape(filters_18b, [3, 3, 1, 1])
filters_19 = tf.reshape(filters_19, [3, 3, 1, 1])
5.细胞神经网络
def sgn(x):
if x > 1:
return 1
elif x < -1:
return -1
else:
return x
rng = [i for i in range(-10,10,2)]
y = [sgn(x) for x in rng]
plt.plot(rng, y)
def convolution_fig(input_x, filterinput, epochs, fig_size, fig_num, title):
A1 = np.zeros([16,16],dtype=float)
A2 = np.zeros([16,16],dtype=float)
A3 = np.zeros([16,16],dtype=float)
for i in range(16):
for j in range(16):
A1[i][j] = float(input_x[i][j])
for i in range(16):
for j in range(16):
A3[i][j] = sgn(float(input_x[i][j]))
for k in range(epochs):
if fig_num == 4:
if k == 0:
plt.figure(figsize=(fig_size,fig_size))
plt.suptitle(title,y=0.01)
plt.subplot(221)
plt.imshow(A3,cmap ='gray')
elif k == int(epochs/4):
plt.subplot(222)
plt.imshow(A3,cmap ='gray')
elif k == int(epochs/2):
plt.subplot(223)
plt.imshow(A3,cmap ='gray')
elif k == int(epochs-1):
plt.subplot(224)
plt.imshow(A3,cmap ='gray')
plt.tight_layout()
elif fig_num == 6:
if k == 0:
plt.figure(figsize=(fig_size,fig_size/1.6))
plt.suptitle(title,y=0.01)
plt.subplot(231)
plt.imshow(A3,cmap ='gray')
elif k == 1:
plt.subplot(232)
plt.imshow(A3,cmap ='gray')
elif k == 2:
plt.subplot(233)
plt.imshow(A3,cmap ='gray')
elif k == 3:
plt.subplot(234)
plt.imshow(A3,cmap ='gray')
elif k == 4:
plt.subplot(235)
plt.imshow(A3,cmap ='gray')
elif k == 5:
plt.subplot(236)
plt.imshow(A3,cmap ='gray')
plt.tight_layout()
elif fig_num == 8:
if k == 0:
plt.figure(figsize=(fig_size,fig_size/2.2))
plt.suptitle(title,y=0.05)
plt.subplot(241)
plt.imshow(A3,cmap ='gray')
elif k == 1:
plt.subplot(242)
plt.imshow(A3,cmap ='gray')
elif k == 2:
plt.subplot(243)
plt.imshow(A3,cmap ='gray')
elif k == 3:
plt.subplot(244)
plt.imshow(A3,cmap ='gray')
elif k == 4:
plt.subplot(245)
plt.imshow(A3,cmap ='gray')
elif k == 5:
plt.subplot(246)
plt.imshow(A3,cmap ='gray')
elif k == 6:
plt.subplot(247)
plt.imshow(A3,cmap ='gray')
elif k == 7:
plt.subplot(248)
plt.imshow(A3,cmap ='gray')
A4 = np.pad(A3, ((1,1), (1,1)), 'edge')
A4 = tf.reshape(tf.convert_to_tensor(A4,dtype=tf.float32), [1, 18, 18, 1])
A4 = tf.nn.conv2d(A4, filterinput, strides=[1,1,1,1], padding='VALID')
for i in range(16):
for j in range(16):
A1[i][j] = A1[i][j] + float(A4[0][i][j][0])*0.2
for i in range(16):
for j in range(16):
A3[i][j] = sgn(float(A1[i][j]))
6.去噪效果对比
convolution_fig(input_x=img01_noise1, filterinput=filters_4a, epochs=4, fig_size=4, fig_num=4, title='Fig.5')
convolution_fig(input_x=img01_noise2, filterinput=filters_4a, epochs=4, fig_size=4, fig_num=4, title='Fig.6')
convolution_fig(input_x=img01_noise1, filterinput=filters_4b, epochs=4, fig_size=4, fig_num=4, title='Fig.7')
convolution_fig(input_x=img01_noise2, filterinput=filters_4b, epochs=4, fig_size=4, fig_num=4, title='Fig.8')
convolution_fig(input_x=img01_noise3, filterinput=filters_4b, epochs=4, fig_size=4, fig_num=4, title='Fig.9')
convolution_fig(input_x=img01_noise4, filterinput=filters_4b, epochs=8, fig_size=4, fig_num=4, title='Fig.10')
convolution_fig(input_x=img01_noise1, filterinput=filters_4c, epochs=6, fig_size=6, fig_num=6, title='Fig.11')
convolution_fig(input_x=img01_noise2, filterinput=filters_4c, epochs=6, fig_size=6, fig_num=6, title='Fig.12')
convolution_fig(input_x=img01_noise5, filterinput=filters_4a, epochs=6, fig_size=6, fig_num=6, title='Fig.13')
convolution_fig(input_x=img01_noise5, filterinput=filters_4d, epochs=6, fig_size=6, fig_num=6, title='Fig.14')
7.特征提取: 边缘与角点
convolution_fig(input_x=get_diamond(-0.5), filterinput=filters_15, epochs=8, fig_size=10, fig_num=8, title='Fig.19')
convolution_fig(input_x=get_diamond(-0.8), filterinput=filters_15, epochs=8, fig_size=10, fig_num=8, title='Fig.20')
convolution_fig(input_x=get_diamond(0.9), filterinput=filters_15, epochs=8, fig_size=10, fig_num=8, title='Fig.21')
convolution_fig(input_x=get_square(-0.8), filterinput=filters_17, epochs=64, fig_size=10, fig_num=8, title='Fig.22')
convolution_fig(input_x=get_square(-0.5), filterinput=filters_17, epochs=32, fig_size=6, fig_num=6, title='Fig.23')
convolution_fig(input_x=get_diamond(-0.5), filterinput=filters_18b, epochs=4, fig_size=4, fig_num=4, title='Fig.24')
convolution_fig(input_x=get_square(-0.5), filterinput=filters_18b, epochs=4, fig_size=4, fig_num=4, title='Fig.25')
convolution_fig(input_x=get_diamond(-0.2), filterinput=filters_17, epochs=4, fig_size=4, fig_num=4, title='Fig.26')
convolution_fig(input_x=get_square(-0.5), filterinput=filters_15, epochs=8, fig_size=4, fig_num=4, title='Fig.27')
8.中文识别
convolution_fig(input_x=get_zhong(-0.5), filterinput=filters_17, epochs=32, fig_size=4, fig_num=4, title='Fig.34')
convolution_fig(input_x=get_bu(0.1), filterinput=filters_17, epochs=32, fig_size=4, fig_num=4, title='Fig.35')
|