Showing posts with label Image Processing. Show all posts
Showing posts with label Image Processing. Show all posts

Saturday, November 21, 2026

Sobel vs Canny Edge Detection in Python: An OpenCV Tutorial

How does a computer turn a photograph into lines? In this hands-on Python example, we compare two ways of finding intensity changes: Sobel derivatives and Canny edge detection. You will see the same input image, a gradient-strength map, and a binary edge map.

Watch the visual explanation: Sobel vs Canny Short. For the complete walkthrough, watch the Python and OpenCV tutorial.

What is an edge?

An edge is a location where pixel intensity changes sharply. A dark object against a bright background can create an edge, but so can a shadow, texture, or noise. An edge detector identifies changes in the image; it does not identify an object or understand its meaning.

How Sobel responds to changes

Sobel uses small, fixed kernels to approximate intensity derivatives. One derivative measures change along the x direction and another along y. Combining their values provides a gradient-strength map: brighter pixels in our visualization indicate a stronger response after the gradient magnitude is clipped to the 0–255 display range.

A filter scanning across an image is a useful way to understand the computation. The Sobel kernel is chosen by us; it is not a trained neural-network filter. If you want the background on sliding kernels, see How CNN Filters Find Edges.

What Canny adds

Canny is a multi-stage edge detector. Its stages include gradient estimation, thinning candidate edges through non-maximum suppression, and hysteresis thresholding. In this example, we apply Gaussian blur before Canny to reduce sensitivity to noise. The Sobel result uses unblurred grayscale, matching the course exercise. This makes the demonstration useful for exploring each step, but it is not a comparison with identical preprocessing.

The two Canny thresholds work together: strong candidate edges are kept; weaker candidates can be kept when connected to a strong edge. Changing thresholds can remove noise or detail, but the best settings depend on the input image. cv2.Canny does not automatically perform the separate Gaussian blur shown in this example.

Try it with Python and OpenCV

Install Python, then install the dependencies in your environment:

python -m pip install opencv-python numpy

To recreate the geometric image used in the video, save the following code as generate_input.py and run python generate_input.py in a new folder. It creates input.png; it stops if that file already exists so it does not overwrite your image.

"""Generate the seeded synthetic geometric image used in course lesson 07.

Run: python generate_input.py
Requires: pip install opencv-python numpy
"""
from pathlib import Path
import cv2
import numpy as np

rng = np.random.default_rng(7)
img = np.zeros((480, 640, 3), dtype=np.uint8)
img[:] = (30, 25, 45)
cv2.rectangle(img, (80, 80), (280, 280), (200, 180, 100), -1)
cv2.circle(img, (450, 180), 110, (100, 200, 180), -1)
cv2.ellipse(img, (320, 380), (120, 60), 30, 0, 360, (180, 100, 200), -1)
cv2.line(img, (60, 400), (580, 440), (220, 220, 80), 4)
noise = rng.normal(0, 8, img.shape).astype(np.int16)
img = np.clip(img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
output = Path(__file__).resolve().parent / 'input.png'
if output.exists():
    raise SystemExit(f'File already exists: {output}. Choose a clean folder to avoid overwriting it.')
if not cv2.imwrite(str(output), img):
    raise RuntimeError(f'Unable to write {output}')
print(f'Saved {output}')

Now save the following script as sobel_canny.py in the same folder and run python sobel_canny.py. To use your own image instead, run python sobel_canny.py YOUR_IMAGE.jpg, replacing the name with the actual filename or path. Use an image you have the right to use.

"""Sobel gradient magnitude and Canny edges, based on course lesson 07.

Run: python sobel_canny.py [input_image]
Requires: pip install numpy opencv-python
Outputs are written alongside the script. Canny operates on blurred grayscale;
Sobel operates on unblurred grayscale, matching the lesson notebook.
"""
from pathlib import Path
import json
import sys

import cv2
import numpy as np

HERE = Path(__file__).resolve().parent
source = Path(sys.argv[1]) if len(sys.argv) > 1 else HERE / "input.png"
image = cv2.imread(str(source), cv2.IMREAD_COLOR)
if image is None:
    raise SystemExit(f"Cannot read image: {source}")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
magnitude = np.sqrt(gx**2 + gy**2)
sobel = np.uint8(np.clip(magnitude, 0, 255))

def save(name, pixels):
    path = HERE / name
    if not cv2.imwrite(str(path), pixels):
        raise RuntimeError(f"Failed to write {path}")

save("sobel_magnitude.png", sobel)
measurements = {}
for low, high in [(20, 60), (50, 150), (100, 200), (150, 250)]:
    edges = cv2.Canny(blurred, low, high)
    assert edges.shape == gray.shape and set(np.unique(edges)).issubset({0, 255})
    save(f"canny_{low}_{high}.png", edges)
    measurements[f"{low}/{high}"] = int(np.count_nonzero(edges))
results = {
    "input": str(source.name), "width": int(image.shape[1]), "height": int(image.shape[0]),
    "opencv": cv2.__version__, "numpy": np.__version__,
    "canny_nonzero_pixels": measurements,
    "sobel_nonzero_pixels": int(np.count_nonzero(sobel)),
    "interpretation": "Canny is binary; Sobel shows clipped gradient magnitude. Counts are not accuracy measurements."
}
(HERE / "measurements.json").write_text(json.dumps(results, indent=2), encoding="utf-8")
print(json.dumps(results, indent=2))

The script saves sobel_magnitude.png, four Canny images for threshold pairs 20/60, 50/150, 100/200 and 150/250, and measurements.json in the script folder. Those counts report nonzero pixels; they are not accuracy scores or object counts. The demonstration was tested with OpenCV 4.13.0 and NumPy 2.3.5. Versions and image input can affect results. Re-running the processing script overwrites its output files.

Compare the results

  • Sobel: the displayed map shows the strength of intensity changes; it is not inherently a binary outline.
  • Canny: the output marks selected edge pixels after thinning and thresholding.
  • Try this: change both Canny thresholds and compare the resulting outlines. Also try a more textured image or change the blur. Do not assume that fewer lines always mean a more accurate result.

Frequently asked questions

Is Canny a neural network?
No. It is a classical, multi-stage image-processing algorithm. It does not learn weights from a training dataset.

Which is better, Sobel or Canny?
It depends on the task. Sobel provides derivatives or gradient strength; Canny is often useful when you want a thinner, binary edge map. Both are sensitive to image content and parameter choices.

Can edges be used to count objects?
They may help with later processing, but edges alone do not reliably count objects. Broken boundaries, touching objects, lighting and texture can lead to mistakes.

Continue learning

Explore the computer vision course overview and my English-language course, Computer Vision: OpenCV, Machine Learning and YOLO. The course includes hands-on lessons on image operations, edge detection and further computer vision techniques. This is my instructor referral link.

Further reading: OpenCV: Sobel derivatives and OpenCV: Canny edge detection.

Friday, November 20, 2026

How CNN Filters Find Edges: A 3×3 Convolution Example in Python

How can a convolutional neural network (CNN) begin to find useful patterns in an image? One building block is a small filter that moves across pixel values. At each position, it combines a local patch of pixels with the filter's weights to produce one number.

In this tutorial, we follow a 3×3 edge filter across a tiny image, calculate a value by hand, and reproduce the complete result in Python. The example uses a hand-picked filter to explain an operation used in CNNs; it is not an activation taken from a trained network.

Watch the moving filter and feature map in the YouTube Short.

1. The image and the filter

Our grayscale image is a 5×5 grid. Each row is identical. Here, 0 represents black and 255 represents white:

  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0
  0  255  255  255    0

The example filter has three identical rows. It subtracts the left side of each patch from the right side:

-1   0   1
-1   0   1
-1   0   1

This filter is sensitive to changes from left to right, so it responds to vertical boundaries. Different filters respond to different patterns.

2. Calculate the first output value

Place the filter over the upper-left 3×3 patch. Multiply each pixel by the weight at the same position and sum all nine products. Because the three rows in this particular patch are identical, we can show the calculation for one row and multiply its result by three:

One row: 0 × -1 + 255 × 0 + 255 × 1 = 255
Three rows: 255 + 255 + 255 = 765

The first output cell is therefore +765. Move the filter one pixel to the right and repeat. In the middle patch, all pixel values are 255, so the positive and negative contributions cancel and the output is 0. At the right boundary the response is -765.

3. From a moving filter to a feature map

We move one pixel at a time (stride 1) and do not add a border (no padding). The filter fits in three positions across and three down, so the 5×5 input produces a 3×3 output: (5 − 3 + 1) × (5 − 3 + 1).

 765   0  -765
 765   0  -765
 765   0  -765

These are signed raw responses. In this example, +765 and -765 indicate opposite directions of the same kind of brightness change. Zero appears for the uniform middle patch; it does not prove that every possible image patch with a zero response is uniform. Taking an absolute value, clipping values, or applying an activation changes how the output is interpreted.

4. Run the calculation in Python

You need Python and NumPy. If NumPy is not installed in your chosen environment, run python -m pip install numpy. Save the code below as cnn_filter_example.py and run python cnn_filter_example.py. No image file, model weights, or GPU is required.

import numpy as np

# Five identical rows make a simple grayscale image.
# Signed integers preserve negative results.
image = np.array([[0, 255, 255, 255, 0]] * 5, dtype=np.int32)
kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.int32)

# Valid cross-correlation: stride 1, no padding, no flipped kernel.
feature_map = np.zeros((3, 3), dtype=np.int32)
for row in range(feature_map.shape[0]):
    for col in range(feature_map.shape[1]):
        patch = image[row:row + 3, col:col + 3]
        feature_map[row, col] = np.sum(patch * kernel)

print('First patch:')
print(image[:3, :3])
print('First output value:', feature_map[0, 0])
print('Signed feature map:')
print(feature_map)
np.testing.assert_array_equal(feature_map, [[765, 0, -765]] * 3)

The assertion checks this particular fixture; it is not a test of a trained model's accuracy. Using a signed integer type matters: unsigned pixel types cannot represent -765 directly.

How does this relate to a CNN?

A convolutional layer applies filters to local patches to create feature maps. A real CNN can use multiple filters, input channels, biases, nonlinear activations and other layers. Its filter weights are usually learned during training. Here we chose the numbers ourselves to make the calculation easy to inspect; we did not train or run a CNN.

Strictly speaking, this example does cross-correlation: it does not flip the kernel before applying it. CNN libraries commonly call this operation convolution. The PyTorch Conv2d documentation describes the cross-correlation operation used by that layer.

Try it yourself

Reverse each filter row to [1, 0, -1] and run the script again after removing or updating the assertion. Can you predict the new signs? Keep the image and all other code unchanged. The positive and negative outputs should exchange signs, while the middle column remains zero.

Frequently asked questions

Why is the output smaller than the input?
With a 3×3 filter, stride 1 and no padding, the filter can start at only three rows and three columns of a 5×5 input. Padding and stride change the output dimensions.

Are CNN filters always edge detectors?
No. Filters are learned for the task; the patterns and their interpretation depend on the trained network, layer and input.

Does a negative value mean an error?
No. A negative value is a valid signed response. Here its sign reflects the direction of the brightness change relative to our chosen filter.

Continue learning

Watch the short animation to follow the moving window. Read the course overview on this blog. My English-language course Computer Vision: OpenCV, Machine Learning and YOLO includes a hands-on lesson on convolution. This is my instructor referral link.