New High-Quality Edge Detector

Boris Kravtsov, PhD
5 min readFeb 2, 2022

--

Edge Detection in the Visual Cortex

Photo by Chandri Anggara on Unsplash

Edge detection is widely used in image processing. It is extremely important as the main preprocessing stage in pattern recognition. We are offering a new approach to this field.

The Sobel kernel Gy shown below is “tuned” to detect horizontal edges.

                   1   0  -1             1   2   1
Gx = 2 0 -2 Gy = 0 0 0
1 0 -1 -1 -2 -1

As the deviation from the horizontal direction increases, the Sobel filter’s sensitivity falls. As a result, the vertical image edges become invisible (see image on the right below). As you can tell from the image, they are all concealed in the “blind zone” of the filter.

The original image and edge detection by Sobel filter (Gy)

As we will discuss more in our next story, there are two ways to perform image processing and, in particular, Sobel filtering:

  • Spatial domain: filtering is done by convolving the image with a Sobel kernel. OpenCV has a ready-made solution for this — cv2.Sobel(image, cv.CV_64F, 0, 1, ksize=3).
  • Frequency domain: multiplying Fourier transform of an image with Fourier transform of Sobel kernel, and compute inverse Fourier transform of the resulting product:

We propose to modify the procedure above as follows:

Here rot(b, alpha) denotes rotation of the Sobel kernel spectrum by angle alpha. This rotation changes the position of the blind zone since the modified Sobel filter is tuned to the new alpha direction.

The modified procedure applied to the image above demonstrates the repositioning of the blind zone for alpha = 0, 30, 60, and 90 degrees. The magnitude of the rotated kernel spectrum rot(b, alpha) is shown on the right.

Unfortunately, neither the Sobel filter nor its modifications can spare us from blind zones.

However, a set of modified Sobel filters acting in parallel — vc-filter may succeed.

So, let’s use a set of such filters with angles equal to 0 degrees, 12 degrees, 24 degrees, etc. Lastly, we summarize the filters’ results and gain a perfect outline image:

Edge detection by vc-filter

vc-filter is a set of orientation-selective filters

Let us compare the results of the recommended OpenCV solution (implemented as a sequence of GaussianBlur and Laplacian filters) and the results achieved by vc-filter installed from PyPI. We also applied some contrast enhancement for both compared techniques.

Test 1

Edge detection by OpenCV

Take a look at the Unsplash photo at the very top, where the photographer intentionally defocused the left side of the portrait. Since the OpenCV solution is insensitive to out-of-focus blurred edges, they are almost invisible in the contour image above.

Edge detection by vc-filter

As we see, the vc-filter finds all edges, including the blurred ones. It’s not surprising as multiple accumulations are a well-known way to detect weak signals. Note that we can increase the quality even more with contrast and the number of primitive filters.

Test 2

An even bigger problem with the recommended OpenCV filter is detecting non-existent edges.

Here from top to bottom:

  1. A group of shapes as the original image.
  2. The outline image was created by an OpenCV filter.
  3. The outline image was created by vc-filter.

See how the OpenCV filter, unlike the vc-filter, improperly duplicates all lines.

Notes

  • vc-filter is a high-quality general-purpose edge detector constructed from any set of orientation-selective filters acting in parallel. For example, vc-filter may be based on another Sobel kernel, Gx, or use Prewitt or Roberts kernels. To use the primitive filter with orientation-selective property, we need a transition to the frequency domain.
  • The vc-filter has no parameters, and its use is straightforward. However, the contour image obtained by vc-filter usually requires some simple contrast enhancement.
  • You can download an example of how to use vc-filter from our website.
  • vc-filter can be installed from PyPI right now — pip install vc-filter
  • vc-filter has been tested on the following platforms: Ubuntu 20.04, Windows 11 and macOS Ventura.
import cv2 as cv
from pathlib import Path

import vc_filter

def opencv_recommended_filter(image):
"""
https://docs.opencv.org/3.4/d5/db5/tutorial_laplace_operator.html
"""
# Remove noise by blurring with a Gaussian filter
image_gauss = cv.GaussianBlur(image, (3, 3), cv.BORDER_DEFAULT)

# Convert the image to grayscale
image_in_gray = cv.cvtColor(image_gauss, cv.COLOR_BGR2GRAY)

# Apply Laplace function
image_out_gray = cv.Laplacian(image_in_gray, cv.CV_16S, ksize=3)

# converting back to uint8
abs_image_out_gray = cv.convertScaleAbs(image_out_gray)

return abs_image_out_gray

def contrast_enhancement(image, contrast_param):
image_contrast = image * contrast_param
image_contrast[image_contrast > 255] = 255
return image_contrast


""" Get input image """
path_in = Path.cwd() / 'DATA' / 'image_in.png'
image_in = cv.imread(str(path_in), cv.IMREAD_UNCHANGED)

""" OpenCV recommended filter """
# -------------------------------------------------------------
edges_opencv = opencv_recommended_filter(image_in)

path_opencv = Path.cwd() / 'DATA' / 'edges_opencv.png'
cv.imwrite(str(path_opencv), edges_opencv)
# -------------------------------------------------------------

""" VC filter """
# -------------------------------------------------------------
edges_vcf = vc_filter.apply(image_in)

edges_vcf_enh = contrast_enhancement(edges_vcf, 1.7)

path_vcf_enh = Path.cwd() / 'DATA' / 'edges_vcf_enh.png'
cv.imwrite(str(path_vcf_enh), edges_vcf_enh)
# -------------------------------------------------------------

D. Hubel and T. Wiesel discovered a set of orientation-selective neurons tuned to different angles (Nobel Prize in Physiology or Medicine) more than 60 years ago. However, the purpose of these neurons in the visual cortex remains unclear. We believe that orientation-selective neurons compose the contour image of the visible object in the same way as the set of modified primitive filters in our method.

Gabor filter-type receptive field typical for a neuron in the primary visual cortex. Learn more.

vc-filter (VC filter) is based on the Visual Cortex study.

Read more.

--

--

Boris Kravtsov, PhD
Boris Kravtsov, PhD

Written by Boris Kravtsov, PhD

I'm trying to share some of my old thoughts and new perspectives.