# set cell width to 100%
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
from IPython.display import Image
Image(filename='input.jpg')
import cv2
import numpy as np
from matplotlib import pyplot as plt
Read image
img = cv2.imread('input.jpg')
Create 2 sets of kernels for convolution
# kernel = np.ones((5,5), np.float32) / 25
kernel_N = np.array([[2, 2, 2],
[2, -4, 2],
[-2, -2, -2]])
kernel_E = np.array([[-2, 2, 2],
[-2, -4, 2],
[-2, 2, 2]])
kernel_S = np.array([[-2, -2, -2],
[2, -4, 2],
[2, 2, 2]])
kernel_W = np.array([[2, 2, -2],
[2, -4, -2],
[2, 2, -2]])
kernel_SE = np.array([[-2, -2, 2],
[-2, -4, 2],
[2, 2, 2]])
kernel_NE = np.array([[2, 2, 2],
[-2, -4, 2],
[-2, -2, 2]])
kernel_SW = np.array([[2, -2, -2],
[2, -4, -2],
[2, 2, 2]])
kernel_NW = np.array([[2, 2, 2],
[2, -4, -2],
[2, -2, -2]])
# different set of kernels
kernel_N2 = np.array([[2, 2, 2],
[0, 0, 0],
[-2, -2, -2]])
kernel_E2 = np.array([[-2, 0, 2],
[-2, 0, 2],
[-2, 0, 2]])
kernel_S2 = np.array([[-2, -2, -2],
[0, 0, 0],
[2, 2, 2]])
kernel_W2 = np.array([[2, 0, -2],
[2, 0, -2],
[2, 0, -2]])
Fill dictionaries with kernel arrays
kernels = dict()
kernels["kernel_N"] = kernel_N
kernels["kernel_E"] = kernel_E
kernels["kernel_S"] = kernel_S
kernels["kernel_W"] = kernel_W
kernels["kernel_NE"] = kernel_NE
kernels["kernel_SE"] = kernel_SE
kernels["kernel_SW"] = kernel_SW
kernels["kernel_NW"] = kernel_NW
kernels2 = dict()
kernels2["kernel_N"] = kernel_N2
kernels2["kernel_E"] = kernel_E2
kernels2["kernel_S"] = kernel_S2
kernels2["kernel_W"] = kernel_W2
Write edge detection images into files
output_imgs = list()
for name, kernel in kernels.items():
dst = cv2.filter2D(img, -1, kernel)
output_imgs.append(dst)
cv2.imwrite(name + '.png', dst)
This function is used to create average image from a list of images
def create_average_image(images_list):
h, w, c = images_list[0].shape
average = np.zeros((h, w, c), np.float)
N = len(images_list)
for img in images_list:
average += img / (N/2)
average = np.array(np.round(average), dtype = np.uint8)
return average
This function is used to create image from a sum of images in a list
def create_sum_image(images_list):
h, w, c = images_list[0].shape
img_sum = np.zeros([h, w, c], dtype=np.uint8)
for i in range(len(images_list)):
img_sum = cv2.add(img_sum, images_list[i])
return img_sum
Create and save images into files
height, width, channels = img.shape
# print(output_imgs[0].dtype)
img_sum = create_sum_image(output_imgs)
img_avg = create_average_image(output_imgs)
cv2.imwrite('img_sum.png', img_sum)
cv2.imwrite('img_avg.png', img_avg)
Sum input image and its version with extracted edges
img_enh = cv2.add(img_avg, img)
cv2.imwrite('img_enh.png', img_enh)
Image(filename='img_avg.png')
Image(filename='img_sum.png')
Image(filename='img_enh.png')
Create gif from images of extracted edges
import imageio
images = list()
image_names = [name+'.png' for name in kernels.keys()]
for filename in image_names:
images.append(imageio.imread(filename))
imageio.mimsave('movie.gif', images)
Image(filename='movie.gif')
Image(url='movie.gif')
by Wojciech Wiertarka