Import OpenCV and the original image
import cv2
import numpy as np
from matplotlib import pyplot as plt
ucanalytics_logo= cv2.imread('YOU-CANalytics-Logo.jpg')
ucanalytics_logo = cv2.cvtColor(ucanalytics_logo, cv2.COLOR_BGR2RGB)
Display the original image
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)
plt.imshow(ucanalytics_logo)
Create a few basic filter matrices
Edge = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])
Edge1 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
Sharpen=np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
Gaussian_Blur=np.array([[1, 2, 1],
[2, 4, 2],
[1, 2, 1]])*1/16
Apply edge detector filter to the image and display the image with edge detector filter
%pylab inline
pylab.rcParams['figure.figsize'] = (12, 8)
plt.subplot(1, 4, 1)
plt.imshow(Edge)
plt.title('Edge')
plt.subplot(1, 4, 2)
plt.imshow(Edge1)
plt.title('Edge 1')
plt.subplot(1, 4, 3)
plt.imshow(Sharpen)
plt.title('Sharpen')
plt.subplot(1, 4, 4)
plt.imshow(Gaussian_Blur)
plt.title('Gaussian Blur')
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)
ucanalytics_logo_flt1 = cv2.filter2D(ucanalytics_logo, -1, Edge1)
plt.imshow(ucanalytics_logo_flt1)
Apply the Maxpooling operation to the original image
import tensorflow as tf
ucanalytics_logo=ucanalytics_logo.reshape(1,120,607,3)
X = tf.placeholder(tf.float32, shape=(1,120, 607, 3))
MaxPool = tf.nn.max_pool(X, ksize=[1,2,2,1], strides=[1,2,2,1],padding="VALID")
with tf.Session() as sess:
output = sess.run(MaxPool, feed_dict={X: ucanalytics_logo})
output=output.reshape(60,303,3)
plt.imshow(output.astype(np.uint8)) # plot the output for the 1st image
plt.show()
Apply the Maxpooling operation to the filtered image (edge1)
ucanalytics_logo_flt1=ucanalytics_logo_flt1.reshape(1,120,607,3)
X = tf.placeholder(tf.float32, shape=(1,120, 607, 3))
MaxPool = tf.nn.max_pool(X, ksize=[1,2,2,1], strides=[1,2,2,1],padding="VALID")
with tf.Session() as sess:
output = sess.run(MaxPool, feed_dict={X: ucanalytics_logo_flt1})
output=output.reshape(60,303,3)
plt.imshow(output.astype(np.uint8)) # plot the output for the 1st image
plt.show()