import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
This will be our filter — it tests the presence of a horizontal line:
conv_hline = [
[-1,-1,-1],
[ 2, 2, 2],
[-1,-1,-1]
]
plt.imshow(conv_hline, cmap='gray_r')
Dot product between a horizontal line and the horizontal line filter:
data = [
[0,0,0],
[1,1,1],
[0,0,0]
]
s = np.tensordot(data, conv_hline)
plt.imshow(data, cmap='gray_r')
plt.title('Dot product: {:.2f}'.format(s))
Dot product between a vertical line and the horizontal line filter:
data = [
[1,0,0],
[1,0,0],
[1,0,0]
]
s = np.tensordot(data, conv_hline)
plt.imshow(data, cmap='gray_r')
plt.title('Dot product: {:.2f}'.format(s))
Now, we want to detect parts of an image that contain certain features, such as horizontal lines.
img = plt.imread('https://i.imgur.com/BeRusDc.png')
plt.imshow(img, cmap='gray')
img.shape
We will apply our 3x3 filter on 3x3 pixels of the image, sliding from top to bottom, left to right.
height, width = img.shape
res = []
for i in range(height-3+1):
line = []
for j in range(width-3+1):
s = np.tensordot(img[i:i+3,j:j+3], conv_hline)
line.append(float(s))
res.append(line)
The dot product of all 3x3 pixels convoluted with the horizontal line filter is now an image that only contains horizontal lines.
img_h = np.array(res).reshape(height-3+1, -1)
img_h.shape
plt.imshow(img_h, cmap='gray_r')
We will do the same thing with vertical lines.
conv_vline = [
[-1,2,-1],
[-1,2,-1],
[-1,2,-1]
]
plt.imshow(conv_vline, cmap='gray_r')
height, width = img.shape
res = []
for i in range(height-3+1):
line = []
for j in range(width-3+1):
s = np.tensordot(img[i:i+3,j:j+3], conv_vline)
line.append(float(s))
res.append(line)
img_v = np.array(res).reshape(height-3+1, -1)
img_v.shape
plt.imshow(img_v, cmap='gray_r')
If we add the two, the image now contains horizontal and vertical lines. We basically just simplified the initial image.
plt.imshow(img_v + img_h, cmap='gray_r')