First we import the package we need
import os
import numpy as np
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
Right now we have a black cross(size 5*5) here
cross = np.array([[255,255,0,255,255],[255,255,0,255,255],[0,0,0,0,0],[255,255,0,255,255],[255,255,0,255,255]])
plt.imshow(cross, cmap='gray')
print(cross)
So how could we recognize the horizontal line of that cross?
We need a filter(3*3)
filter_1 = np.array([[1,0,1],[1,0,1],[1,0,1]])
print(filter_1)
This will activate the area related to the line.
cross = np.reshape(cross, [-1, 5, 5, 1])
filter_1 = np.reshape(filter_1, [3, 3, 1, 1])
tf.reset_default_graph()
filter_test_input = tf.placeholder(tf.float32, shape=[None, 5, 5, 1])
filter_result = tf.nn.conv2d(filter_test_input,filter_1, [1,1,1,1], padding='SAME')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
f_result_val = sess.run([filter_result], feed_dict={
filter_test_input : cross,
})
f_result_val = np.reshape(f_result_val, [5,5])
plt.imshow(f_result_val, 'gray')
The black horizontal line is activited as a white line.
This is how we use filter to recognize the patten on an image