Import OpenCV and the original image

In [1]:
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

In [2]:
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)
plt.imshow(ucanalytics_logo)
Populating the interactive namespace from numpy and matplotlib
Out[2]:
<matplotlib.image.AxesImage at 0x24663682400>

Create a few basic filter matrices

In [3]:
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

In [4]:
%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')
Populating the interactive namespace from numpy and matplotlib
Out[4]:
Text(0.5, 1.0, 'Gaussian Blur')
In [5]:
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)
ucanalytics_logo_flt1 = cv2.filter2D(ucanalytics_logo, -1, Edge1)
plt.imshow(ucanalytics_logo_flt1)
Populating the interactive namespace from numpy and matplotlib
Out[5]:
<matplotlib.image.AxesImage at 0x246637e1f60>

Apply the Maxpooling operation to the original image

In [6]:
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)

In [7]:
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()