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.
No comments:
Post a Comment