I've found an implementation which makes use of numpy and cv2 (link), but I'm having difficulties converting this code to tensorflow.
The code for the numpy implementation:
import numpy as np
import cv2
def LoG_numpy(img, sigma=1., kappa=0.75, pad=False):
"""
Applies Laplacian of Gaussians to grayscale image.
:param gray_img: image to apply LoG to
:param sigma: Gauss sigma of Gaussian applied to image, <= 0. for none
:param kappa: difference threshold as factor to mean of image values, <= 0 for none
:param pad: flag to pad output w/ zero border, keeping input image size
"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (0,) * 2, sigma) if sigma > 0. else img
img = cv2.Laplacian(img, cv2.CV_64F)
rows, cols = img.shape[:2]
# min/max of 3x3-neighbourhoods
min_map = np.minimum.reduce(list(img[r:rows-2+r, c:cols-2+c]
for r in range(3) for c in range(3)))
max_map = np.maximum.reduce(list(img[r:rows-2+r, c:cols-2+c]
for r in range(3) for c in range(3)))
# bool matrix for image value positiv (w/out border pixels)
pos_img = img[1:rows-1, 1:cols-1] > 0.
neg_min = min_map < 0.
neg_min[~pos_img] = 0.
pos_max = max_map > 0.
pos_max[pos_img] = 0.
zero_cross = neg_min + pos_max
# values: max - min, scaled to 0--255; set to 0 for no sign change
value_scale = 255. / max(1., img.max() - img.min())
values = value_scale * (max_map - min_map)
# values[1 - zero_cross] = 0.
values[~zero_cross] = 0.
# optional thresholding
if kappa >= 0.:
thresh = float(np.absolute(img).mean()) * kappa
values[values < thresh] = 0.
log_img = values.astype(np.uint8)
if pad:
log_img = np.pad(log_img, pad_width=1, mode='constant', constant_values=0)
return log_img
And my converted code:
import tensorflow as tf
import tensorflow_addons as tfa
def LoG_tensorflow(img, sigma=1., kappa=0.75, pad=False):
img = tf.convert_to_tensor(img, dtype='float64')
laplace_kernel = tf.constant([[0., 1., 0.], [1., -4., 1.], [0., 1., 0.]], dtype='float64')
laplace_kernel = laplace_kernel[:, :, tf.newaxis, tf.newaxis]
gray_img = tf.image.rgb_to_grayscale(img)
gray_img = tfa.image.gaussian_filter2d(gray_img, sigma=sigma)
gray_img = gray_img[tf.newaxis, ...]
log = tf.nn.conv2d(gray_img, laplace_kernel, [1, 1, 1, 1], 'SAME')[0]
rows, cols = log.shape[:2]
blocks = []
for r in range(3):
for c in range(3):
block = log[r:rows-2+r, c:cols-2+c]
blocks.append(block)
min_map = blocks[0]
max_map = blocks[0]
for block in blocks[1:]:
min_map = tf.math.minimum(min_map, block)
max_map = tf.math.maximum(max_map, block)
pos_img = log[1:rows-1, 1:cols-1] > 0.
neg_min = tf.cast(min_map < 0., 'uint8')
neg_min = neg_min * tf.cast(pos_img, 'uint8')
pos_max = tf.cast(max_map > 0., 'uint8')
pos_max = pos_max * tf.cast(tf.logical_not(pos_img), 'uint8')
zero_cross = tf.logical_or(tf.cast(neg_min, 'bool'), tf.cast(pos_max, 'bool'))
value_scale = 255. / tf.maximum(1., tf.reduce_max(log) - tf.reduce_min(log))
values = value_scale * (max_map - min_map)
values = values * tf.cast(tf.logical_not(zero_cross), 'float64')
if kappa >= 0.:
thresh = tf.abs(tf.reduce_mean(log)) * kappa
threshed = tf.cast(values > thresh, 'float64')
values = values * threshed
if pad:
values = tf.pad(values, [1, 1], mode='CONSTANT', constant_values=0)
return values
The laplacian filter values are taken from the cv2 docs.
img1 = cv2.imread(r"original.jpg")
result1 = LoG_numpy(img1, kappa=5.)
result2 = LoG_tensorflow(img1, kappa=5.)
result2 = result2.numpy().reshape((222, 222)).astype('uint8')
cv2.imshow('original', img1)
cv2.imshow('numpy', result1)
cv2.imshow('tensorflow', result2)
cv2.waitKey(0)
The results don't really look alike, and I'm having trouble finding the reason. The results from the laplacian convolution already greatly differ in cv2 and tensorflow, but I really can't see why. Any ideas?