Image Processing with Python: Enhancement for Image Differencing Applications

Why image sharpening is not suitable for image differencing?

(Original Image by GMA News TV)
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imshow
from skimage.color import rgb2gray
from scipy.signal import convolve2d
from skimage import transform
from skimage import img_as_ubyte
from skimage.exposure import histogram, cumulative_distribution
from scipy.stats import norm, cauchy, logistic
fig, ax = plt.subplots(1,2, figsize=(16,8))traffic = imread('traffic_frame10.jpg')
traffic2 = imread('traffic_frame12.jpg')
ax[0].imshow(traffic)
ax[0].set_title('Frame #10');
ax[1].imshow(traffic2)
ax[1].set_title('Frame #12');
(Original Image by GMA News TV)
def rgb_convolve2d(image, kernel):
red = convolve2d(image[:,:,0], kernel, 'valid')
green = convolve2d(image[:,:,1], kernel, 'valid')
blue = convolve2d(image[:,:,2], kernel, 'valid')
return np.stack([red, green, blue], axis=2)
fig, ax = plt.subplots(1,2, figsize=(16,8))
kernel = np.array([[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1],
[-1, -1, 25, -1, -1],
[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1]])
conv_im1 = (rgb_convolve2d(traffic, kernel[::-1, ::-1])
.clip(0,255)
.astype(np.uint8))
ax[0].imshow(conv_im1, cmap='gray')
ax[0].set_title("Sharpened Frame#10")
conv_im2 = (rgb_convolve2d(traffic2, kernel[::-1, ::-1])
.clip(0,255)
.astype(np.uint8))
ax[1].imshow(conv_im2, cmap='gray')
ax[1].set_title("Sharpened Frame#12")
(Original Image by GMA News TV)
area_of_interest = [(890, 575),
(1085, 575),
(670, 1030),
(440, 1030)]
area_of_projection = [(930, 600),
(1070, 600),
(1070, 1000),
(930, 1000)]
def project_planes(image, src=area_of_interest,
dst=area_of_projection):
new_image = image.copy()
projection = np.zeros_like(new_image)
tform = transform.estimate_transform('projective',
np.array(src),
np.array(dst))
transformed = transform.warp(image, tform.inverse)
return transformed
transformed1 = project_planes(rgb2gray(conv_im1),
area_of_interest, area_of_projection)
transformed2 = project_planes(rgb2gray(conv_im2),
area_of_interest, area_of_projection)
diff = transformed1 - transformed2
imshow(diff);
(Original Image by GMA News TV)
def individual_channel(image, dist, channel):
im_channel = img_as_ubyte(image[:,:,channel])
freq, bins = cumulative_distribution(im_channel)
new_vals = np.interp(freq, dist.cdf(np.arange(0,256)),
np.arange(0,256))
return new_vals[im_channel].astype(np.uint8)
def distribution(image, function, mean, std):
dist = function(mean, std)
image_intensity = img_as_ubyte(rgb2gray(image))

plt.figure(figsize=(8,6))
freq, bins = cumulative_distribution(image_intensity)
plt.step(bins, freq, c='b', label='Actual CDF')
plt.plot(dist.cdf(np.arange(0,256)),
c='r', label='Target CDF')
plt.legend()
plt.title('Actual vs. Target Cumulative Distribution')
distribution(traffic, norm, 120, 50);
(Image by Author)
def show_distribution(image, function, mean, std, ax, title):
dist = function(mean, std)

image_intensity = img_as_ubyte(rgb2gray(image))
freq, bins = cumulative_distribution(image_intensity)

red = individual_channel(image, dist, 0)
green = individual_channel(image, dist, 1)
blue = individual_channel(image, dist, 2)

ax.imshow(np.dstack((red, green, blue)))
ax.set_title(title)
return np.dstack((red, green, blue))
fig, ax = plt.subplots(1,2, figsize=(16,8))
new = show_distribution(traffic, norm, 120, 40,
ax[0], "Corrected Frame#10");
new2 = show_distribution(traffic2, norm, 120, 40,
ax[1], "Corrected Frame#12");
(Original Image by GMA News TV)
transformed1 = project_planes(rgb2gray(new), 
area_of_interest, area_of_projection)
transformed2 = project_planes(rgb2gray(new2),
area_of_interest, area_of_projection)
diff = transformed1 - transformed2
imshow(diff)
(Original Image by GMA News TV)

A Data Scientist and Electrical Engineer trying to share his ideas.