TensorFlow
high-pass filter
digital signal processing
neural networks
machine learning

Implementing high-pass filter in tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A high-pass filter emphasizes rapid changes and suppresses slowly varying components. In image work, that usually means sharpening edges or highlighting detail. In TensorFlow, the most direct implementation is a convolution with a kernel whose weights cancel smooth regions and amplify local differences.

Use Convolution for a Spatial High-Pass Filter

For images, a spatial high-pass filter is often just a small convolution kernel. A classic example is a Laplacian-style kernel:

python
1import tensorflow as tf
2
3image = tf.constant(
4    [[
5        [[10.0], [10.0], [10.0], [10.0]],
6        [[10.0], [20.0], [20.0], [10.0]],
7        [[10.0], [20.0], [20.0], [10.0]],
8        [[10.0], [10.0], [10.0], [10.0]],
9    ]]
10)
11
12kernel = tf.constant(
13    [[
14        [[-1.0]], [[-1.0]], [[-1.0]]
15    ], [
16        [[-1.0]], [[ 8.0]], [[-1.0]]
17    ], [
18        [[-1.0]], [[-1.0]], [[-1.0]]
19    ]]
20)
21
22filtered = tf.nn.conv2d(image, kernel, strides=1, padding="SAME")
23print(filtered[0, :, :, 0])

The input shape is [batch, height, width, channels], and the kernel shape is [kernel_height, kernel_width, in_channels, out_channels]. TensorFlow's convolution APIs are strict about these shapes, so matching the channel dimensions matters.

Why This Kernel Acts as High-Pass

The center weight is positive and the surrounding weights are negative. In a flat region, the neighbors cancel the center value and the response stays near zero. At an edge or sharp transition, that cancellation no longer works cleanly, so the output becomes large in magnitude.

That is the essence of high-pass behavior: smooth low-frequency content gets suppressed, while local change survives.

Different kernels give different effects. Some emphasize edges aggressively, while others are milder and behave more like sharpening filters. The core idea is the same: the weights should sum to zero or near zero so constant regions cancel out.

Apply It to Batches and Color Images

The example above used one grayscale image, but the same pattern extends to batches and multiple channels. For RGB images, you usually filter each channel independently or convert to grayscale first depending on the application.

Here is a channel-wise example for an RGB image using depthwise convolution:

python
1import tensorflow as tf
2
3image = tf.random.uniform([1, 128, 128, 3])
4
5base_kernel = tf.constant([
6    [-1.0, -1.0, -1.0],
7    [-1.0,  8.0, -1.0],
8    [-1.0, -1.0, -1.0],
9], dtype=tf.float32)
10
11kernel = tf.reshape(base_kernel, [3, 3, 1, 1])
12kernel = tf.tile(kernel, [1, 1, 3, 1])
13
14filtered = tf.nn.depthwise_conv2d(image, kernel, strides=[1, 1, 1, 1], padding="SAME")
15print(filtered.shape)

depthwise_conv2d is a good fit when each input channel should get the same spatial filter independently.

Think About Scaling and Display

High-pass outputs often contain negative values, which is correct mathematically but awkward for image display. If your next step is visualization, you may want to rescale or clip the result:

python
display_ready = tf.clip_by_value(filtered + 128.0, 0.0, 255.0)

For machine-learning pipelines, you often keep the raw filtered tensor instead of forcing it into display-friendly pixel ranges. The right choice depends on whether the filter is part of a model feature pipeline or part of a human-facing visualization step.

Common Pitfalls

The most common mistake is building the kernel with the wrong TensorFlow shape. Convolution kernels must match TensorFlow's expected layout exactly, or the operation fails.

Another issue is assuming a high-pass output should look like a normal image immediately. Negative values and strong edge responses are expected.

People also sometimes confuse a mathematically ideal frequency-domain high-pass filter with a small spatial edge kernel. The spatial kernel is often the practical answer, even though it is not the same thing as a hard cutoff in Fourier space.

Summary

  • In TensorFlow, a high-pass filter is often implemented as a convolution kernel that emphasizes local differences.
  • A Laplacian-style kernel is a simple and effective starting point for image data.
  • Flat regions tend to cancel out, while edges and sharp transitions produce strong responses.
  • Use tf.nn.conv2d or tf.nn.depthwise_conv2d depending on how you want to handle channels.
  • Expect negative values and rescale only if you need a human-friendly display image.

Course illustration
Course illustration

All Rights Reserved.