3

I need to build an LoG filer using only my method and only matplotlib OpenCV and NumPy (but not build-in functions that do the filter just to help calculate)

def edgeDetectionZeroCrossingLOG(img: np.ndarray) -> (np.ndarray):
    """
    Detecting edges using the "ZeroCrossingLOG" method
    :param I: Input image
    :return: :return: Edge matrix
    """
    kernel = np.ndarray((3, 3))
    b_img = blurImage1(img, kernel)
    k = np.array([[0, 1, 0],
                  [1, -4, 1],
                  [0, 1, 0]])
    img_dervative = conv2D(img, k)
    ***Zero-Crossing***

Steps:

  1. use a Gaussian filter to blur the image
def blurImage1(in_image: np.ndarray, kernel_size: np.ndarray) -> np.ndarray:
    """
    Blur an image using a Gaussian kernel
    :param inImage: Input image
    :param kernelSize: Kernel size
    :return: The Blurred image
    """

    gaussian = np.ndarray(kernel_size)
    sigma = 0.3 * ((kernel_size[0] - 1) * 0.5 - 1) + 0.8
    for x in range(0, kernel_size[0]):
        for y in range(0, kernel_size[1]):
            gaussian[x, y] = math.exp(-((x ** 2 + y ** 2) / (2.0 * sigma ** 2))) / (math.pi * (sigma ** 2) * 2)
    return conv2D(in_image, gaussian)
  1. Use derivation Laplacian kernel Ix,Iy
    k = np.array([[0, 1, 0],
                  [1, -4, 1],
                  [0, 1, 0]])
  1. Need to find all the "zero-crossing" in the 2D array of the image and marke them as one's and the rest zero's

My main problem is the zero crossings, I cannot find a way to do it.

I need to check all the crossings without a threshold -> { (-+),(+-),(-0+),(+0-)}, and for every crossing to make as 1 and the rest leave at zero.

( The convolution is also implemented by me the function conv2D)

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
EladAskenazi
  • 115
  • 2
  • 10

1 Answers1

5

I stumbled across this thread after I actually have implemented it myself. It is fairly simple if you utilize scikit-image. I'll first present the code, then go through it

from skimage.filters import laplace
import numpy as np

lap = np.sign(laplace(image))
lap = np.pad(lap, ((0, 1), (0, 1)))
diff_x = lap[:-1, :-1] - lap[:-1, 1:] < 0
diff_y = lap[:-1, :-1] - lap[1:, :-1] < 0

edges =  np.logical_or(diff_x, diff_y).astype(float)

First we find the laplacian and take the sign of it, as we are not interested in the values, then proceed to pad the last column and rows with zeroes, for the convolution. Then we proceed to check along the x-axis if there is a crossing from - => +. Just change the comparison to change for the other way around. The idea to check three states in a row can be done similarly where the comparison has to evaluate to True in order to be 1