Bird
Raised Fist0
SciPydata~20 mins

Sobel and Laplace edge detection in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Edge Detection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Sobel filter on a simple image
What is the output array after applying the Sobel filter on the given 3x3 image using scipy.ndimage.sobel with axis=0?
SciPy
import numpy as np
from scipy import ndimage

image = np.array([[10, 10, 10],
                  [20, 20, 20],
                  [30, 30, 30]])
sobel_vertical = ndimage.sobel(image, axis=0)
print(sobel_vertical)
A
[[10 10 10]
 [0 0 0]
 [-10 -10 -10]]
B
[[10 10 10]
 [20 20 20]
 [20 20 20]]
C
[[10 10 10]
 [20 20 20]
 [0 0 0]]
D
[[10 10 10]
 [20 20 20]
 [10 10 10]]
Attempts:
2 left
💡 Hint
Think about how the Sobel filter calculates the gradient along the vertical axis.
data_output
intermediate
2:00remaining
Number of edges detected by Laplace filter
After applying the Laplace filter on the given 5x5 image, how many pixels have a non-zero value indicating edges?
SciPy
import numpy as np
from scipy import ndimage

image = np.array([
    [10, 10, 10, 10, 10],
    [10, 50, 50, 50, 10],
    [10, 50, 100, 50, 10],
    [10, 50, 50, 50, 10],
    [10, 10, 10, 10, 10]
])
laplace = ndimage.laplace(image)
non_zero_count = np.count_nonzero(laplace)
print(non_zero_count)
A8
B12
C20
D16
Attempts:
2 left
💡 Hint
Edges are where the Laplace output is not zero, usually around intensity changes.
🔧 Debug
advanced
2:00remaining
Identify the error in Sobel filter application
What error will this code raise when applying the Sobel filter incorrectly?
SciPy
import numpy as np
from scipy import ndimage

image = np.array([[1, 2], [3, 4]])
sobel = ndimage.sobel(image, axis=2)
print(sobel)
AValueError: input array must be 3D
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CIndexError: axis 2 is out of bounds for array of dimension 2
DNo error, prints the Sobel filtered array
Attempts:
2 left
💡 Hint
Check the axis parameter against the image array dimensions.
visualization
advanced
2:00remaining
Visual difference between Sobel and Laplace filters
Which option best describes the visual difference when applying Sobel and Laplace filters on the same image?
ASobel and Laplace produce identical edge maps.
BSobel produces a blurred image; Laplace produces a sharpened image.
CSobel detects color changes; Laplace detects brightness changes only.
DSobel highlights edges in one direction; Laplace highlights edges regardless of direction with zero-crossings.
Attempts:
2 left
💡 Hint
Think about directional vs. non-directional edge detection.
🚀 Application
expert
3:00remaining
Combining Sobel and Laplace for edge enhancement
Given an image, which code snippet correctly combines Sobel filters on both axes and the Laplace filter to create an enhanced edge image?
A
sobel_x = ndimage.sobel(image, axis=0)
sobel_y = ndimage.sobel(image, axis=1)
laplace = ndimage.laplace(image)
enhanced = np.sqrt(sobel_x**2 + sobel_y**2) + laplace
B
sobel_x = ndimage.sobel(image, axis=0)
sobel_y = ndimage.sobel(image, axis=1)
laplace = ndimage.laplace(image)
enhanced = sobel_x + sobel_y + laplace
C
sobel = ndimage.sobel(image)
laplace = ndimage.laplace(image)
enhanced = sobel * laplace
D
sobel_x = ndimage.sobel(image, axis=0)
laplace = ndimage.laplace(image)
enhanced = sobel_x - laplace
Attempts:
2 left
💡 Hint
Combine gradients from both directions using Pythagorean theorem before adding Laplace.

Practice

(1/5)
1. What is the main purpose of using the Sobel filter in image processing?
easy
A. To detect edges by highlighting horizontal and vertical changes
B. To blur the image and reduce noise
C. To increase the brightness of the image
D. To convert the image to grayscale

Solution

  1. Step 1: Understand Sobel filter function

    The Sobel filter detects edges by calculating gradients in horizontal and vertical directions separately.
  2. Step 2: Identify the purpose of edge detection

    Edges are found by highlighting where brightness changes sharply, which Sobel does by combining horizontal and vertical gradients.
  3. Final Answer:

    To detect edges by highlighting horizontal and vertical changes -> Option A
  4. Quick Check:

    Sobel = edge detection [OK]
Hint: Sobel finds edges by checking horizontal and vertical changes [OK]
Common Mistakes:
  • Confusing Sobel with blurring filters
  • Thinking Sobel changes brightness directly
  • Mixing Sobel with color conversion
2. Which of the following is the correct way to apply the Sobel filter on a 2D image array named img using SciPy?
easy
A. scipy.ndimage.sobel(img, axis=2)
B. scipy.ndimage.laplace(img, axis=1)
C. scipy.ndimage.sobel(img, axis=0)
D. scipy.ndimage.gaussian_filter(img, sigma=1)

Solution

  1. Step 1: Recall Sobel filter usage in SciPy

    The Sobel filter is applied using scipy.ndimage.sobel with the image and axis (0 for vertical, 1 for horizontal).
  2. Step 2: Check axis validity

    For a 2D image, valid axes are 0 or 1. Axis=2 is invalid and will cause an error.
  3. Final Answer:

    scipy.ndimage.sobel(img, axis=0) -> Option C
  4. Quick Check:

    Sobel syntax = scipy.ndimage.sobel(img, axis) [OK]
Hint: Use axis 0 or 1 for 2D images with sobel [OK]
Common Mistakes:
  • Using axis=2 on 2D images causes errors
  • Confusing laplace and sobel functions
  • Using gaussian_filter instead of sobel for edges
3. Given the following code, what is the shape of the output array edges?
import numpy as np
from scipy import ndimage
img = np.array([[1, 2, 1], [0, 1, 0], [2, 1, 2]])
sobel_x = ndimage.sobel(img, axis=0)
sobel_y = ndimage.sobel(img, axis=1)
edges = np.hypot(sobel_x, sobel_y)
medium
A. (3, 3)
B. (2, 2)
C. (3, 2)
D. (1, 3)

Solution

  1. Step 1: Check input image shape

    The input image img is a 3x3 array, so shape is (3, 3).
  2. Step 2: Understand Sobel output shape

    Sobel filter preserves the input shape, so sobel_x and sobel_y are also (3, 3).
  3. Step 3: Calculate edges shape

    Using np.hypot combines two (3, 3) arrays element-wise, resulting in (3, 3).
  4. Final Answer:

    (3, 3) -> Option A
  5. Quick Check:

    Input shape = output shape for sobel [OK]
Hint: Sobel keeps input shape; hypot combines same shapes [OK]
Common Mistakes:
  • Assuming output shape shrinks after sobel
  • Confusing axis with shape dimensions
  • Thinking hypot changes array shape
4. The following code attempts to apply the Laplace filter but raises an error. What is the mistake?
from scipy import ndimage
img = [[1, 2, 3], [4, 5, 6], [7, 8]]
laplace_img = ndimage.laplace(img)
medium
A. ndimage.laplace does not exist in SciPy
B. Laplace filter requires axis parameter
C. The image must be grayscale
D. The input image must be a NumPy array, not a list

Solution

  1. Step 1: Check input type for ndimage filters

    ndimage functions require NumPy arrays, not Python lists, for image input.
  2. Step 2: Identify error cause

    Passing a list causes a TypeError because ndimage.laplace expects an array.
  3. Final Answer:

    The input image must be a NumPy array, not a list -> Option D
  4. Quick Check:

    ndimage needs np.array input [OK]
Hint: Convert lists to np.array before filtering [OK]
Common Mistakes:
  • Passing lists instead of arrays
  • Thinking laplace needs axis parameter
  • Believing laplace function is missing
5. You want to detect edges in a noisy grayscale image using SciPy. Which approach best combines Sobel and Laplace filters to improve edge detection?
hard
A. Apply Gaussian blur, then Sobel filter on axis=0 only
B. Apply Sobel filters on both axes, combine results, then apply Laplace to sharpen edges
C. Use only Laplace filter because Sobel does not work on noisy images
D. Apply Laplace filter first, then Sobel on axis=2

Solution

  1. Step 1: Understand Sobel and Laplace roles

    Sobel detects edges by gradients on horizontal and vertical axes; combining both gives full edge info.
  2. Step 2: Use Laplace to enhance edges

    Applying Laplace after Sobel can sharpen edges by detecting second-order changes.
  3. Step 3: Evaluate options

    Apply Sobel filters on both axes, combine results, then apply Laplace to sharpen edges correctly combines Sobel on both axes and Laplace for sharpening; others misuse axis or filters.
  4. Final Answer:

    Apply Sobel filters on both axes, combine results, then apply Laplace to sharpen edges -> Option B
  5. Quick Check:

    Sobel + Laplace = better edge detection [OK]
Hint: Combine Sobel axes then Laplace for sharper edges [OK]
Common Mistakes:
  • Applying Sobel on invalid axis
  • Skipping combination of horizontal and vertical Sobel
  • Using Laplace alone without Sobel for noisy images