Edge detection image enhancement

In [1]:
# set cell width to 100%
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
In [2]:
from IPython.display import Image
Image(filename='input.jpg')
Out[2]:
In [3]:
import cv2
import numpy as np
from matplotlib import pyplot as plt

Read image

In [4]:
img = cv2.imread('input.jpg')

Create 2 sets of kernels for convolution

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

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

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

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

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

In [10]:
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)
Out[10]:
True

Sum input image and its version with extracted edges

In [11]:
img_enh = cv2.add(img_avg, img)
cv2.imwrite('img_enh.png', img_enh)
Out[11]:
True

Average of edges

In [12]:
Image(filename='img_avg.png')
Out[12]:

Sum of edges

In [13]:
Image(filename='img_sum.png')
Out[13]:
In [14]:
Image(filename='img_enh.png')
Out[14]:

Create gif from images of extracted edges

In [15]:
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)
In [16]:
Image(filename='movie.gif')
Image(url='movie.gif')
Out[16]:

by Wojciech Wiertarka

http://wiertareczka.ddns.net/