Computer Vision - Convolution and Edge Detection II

Jacky Baltes
National Taiwan Normal University
Taipei, Taiwan
jacky.baltes@ntnu.edu.tw
06 April 2021

import PIL

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

im = PIL.Image.open( str(bogie.localFileStem) + "." + bogie.suffix )
img = np.asarray(im)
ax.set_xticks([])
ax.set_yticks([])
ax.imshow(img)

Convolutions

  • Convolution is the application of a weight matrix to each pixel
  • The values in the matrix correspond to the weights of the neighbors of that pixel
  • The convolution matrix is usually odd to have a defined center pixel
  • At the border, you may need to pad the image, or extend it with zeros, or wrap around
import scipy.signal as ss
import matplotlib.colors as mcolors

im = PIL.Image.open( str(bogie.localFileStem) + "." + bogie.suffix )
im = im.resize((320,240))
img = np.asarray(im)[:,:,1]

print('img', img.shape)
img1 = img.copy()
print('img1', img1.shape)

img2 = img1.copy()
img3 = img2.copy()

conv = np.array( [
    [ 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
    [ 0, 1, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
] )

print(img3.shape)
print(conv.shape)

for i in range(20):
    img3 = ss.convolve2d( img3, conv, mode='same' ) # / np.sum(conv)

print(img3[0:2,0:2])

fig = plt.figure(figsize=(10,20))
ax1 = fig.add_subplot(1,3,1)
ax1.imshow(img1, cmap='gray')

ax2 = fig.add_subplot(1,3,2)
ax2.imshow(img2, cmap='gray')

ax3 = fig.add_subplot(1,3,3)
ax3.imshow(img3, cmap='gray', norm=mcolors.Normalize(0,255))

f1 = addJBFigure("f1", 0, 0, fig )
plt.close()
img (240, 320)
img1 (240, 320)
(240, 320)
(5, 5)
[[171 170]
 [184 181]]

Convolution Matrices: Shift Left

import scipy.signal as ss
im = PIL.Image.open( str(bogie.localFileStem) + "." + bogie.suffix )
img = np.asarray(im)

img2 = img.copy()[:,:,2]
img3 = img2.copy()/256.0

conv = img2[ 175:186, 570:591 ]

for i in range(1):
    img3 = ss.convolve2d( img3, conv, mode='same' ) 

fig = plt.figure(figsize=(10,20))
ax1 = fig.add_subplot(1,3,1)
ax1.imshow(img2, cmap='gray')

ax3 = fig.add_subplot(1,3,2)
ax3.imshow( conv, cmap='gray' )

print( img3[0:3,0:3] )
mx = img3.max()
print('mx', mx)

m = np.where( img3 == mx )
print('m', m)

ax2 = fig.add_subplot(1,3,3)
ax2.imshow(img3, cmap='gray')

f4 = addJBFigure( "f4", 0, 0, fig )
plt.close()
[[6490.76953125 7067.515625   7671.62109375]
 [6939.26953125 7540.73046875 8171.90625   ]
 [7249.8203125  7875.19921875 8526.078125  ]]
mx 22400.26953125
m (array([303]), array([868]))

Convolution as Template Matching

import scipy.signal as ss

image = PIL.Image.open( str( cfg['ASSETS']['bogie'].localFileStem ) + "." + cfg['ASSETS']['bogie'].suffix )
img2 = np.asarray( image.convert("L").resize((640,480) ) )
img3 = img2.copy()

sharpen = np.array( [
    [ 0, 0, 0, 0, 0 ],
    [ 0, 0, -1, 0, 0 ],
    [ 0, -1, 5, -1, 0 ],
    [ 0, 0, -1, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
] )

blur = np.array( [
  [ 1, 1, 1, 1, 1 ],
  [ 1, 1, 1, 1, 1 ],
  [ 1, 1, 1, 1, 1 ],
  [ 1, 1, 1, 1, 1 ],
  [ 1, 1, 1, 1, 1 ]                
] )

print(img3.shape)
print(conv.shape)

for i in range(5):
    img3 = ss.convolve2d( img3, blur, mode='same' ) 

img4 = img3.copy()

for i in range(3):
  img4 = ss.convolve2d( img4, sharpen, mode='same' )

fig = plt.figure(figsize=(10,20))
ax1 = fig.add_subplot(1,3,1)
ax1.imshow(img2, cmap='gray')

ax2 = fig.add_subplot(1,3,2)
ax2.imshow(img3, cmap='gray')


ax3 = fig.add_subplot(1,3,3)
ax3.imshow(img4, cmap='gray')

f5 = addJBFigure("f5", 0, 0, fig )
plt.close()
(480, 640)
(11, 21)

Convolution: Blur and Sharpen

import scipy.signal as ss

image = PIL.Image.open( str( taiwan1.localFileStem ) + "." + taiwan1.suffix )
img2 = np.asarray( image.convert("L").resize((640,480) ) )
img3 = img2.copy()

kirsch1 = np.array( [
    [ 0, 0, 0, 0, 0 ],
    [ 0, 0, -1, 0, 1 ],
    [ 0, -2, 0, 2, 0 ],
    [ -1, 0, 1, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
] )

blur = np.ones( (5,5) )

print(img3.shape)
print(conv.shape)

#for i in range(1):
#    img3 = ss.convolve2d( img3, blur, mode='same' ) 

img4 = ss.convolve2d( img2, kirsch1, mode='same' )

fig = plt.figure(figsize=(10,20))
ax1 = fig.add_subplot(1,2,1)
ax1.imshow(img2, cmap='gray')

ax2 = fig.add_subplot(1,2,2)
ax2.imshow(img4, cmap='gray')

f6 = addJBFigure("f6", 0, 0, fig )
plt.close()
(480, 640)
(11, 21)

Convolution: 45 deg. Sobel