Bird
Raised Fist0
Computer Visionml~20 mins

Image gradients (Sobel, Laplacian) in Computer Vision - 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
🎖️
Image Gradient Master
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 grayscale image using OpenCV's Sobel function with dx=1, dy=0, and kernel size 3?
Computer Vision
import cv2
import numpy as np

image = np.array([[10, 10, 10],
                  [10, 20, 10],
                  [10, 10, 10]], dtype=np.uint8)

sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
output = sobelx.astype(int)
print(output)
A
[[  0 -10   0]
 [  0   0   0]
 [  0  10   0]]
B
[[  0  10   0]
 [  0   0   0]
 [  0 -10   0]]
C
[[  0   0   0]
 [  0   0   0]
 [  0   0   0]]
D
[[  0   0   0]
 [ 10   0 -10]
 [  0   0   0]]
Attempts:
2 left
💡 Hint
Sobel with dx=1 detects horizontal edges by calculating differences along the x-axis.
🧠 Conceptual
intermediate
1:30remaining
Understanding Laplacian filter effect
Which statement best describes the effect of applying a Laplacian filter to a grayscale image?
AIt detects edges by calculating the first derivative only in the horizontal direction.
BIt smooths the image by averaging neighboring pixels to reduce noise.
CIt detects edges by calculating the second derivative, highlighting regions of rapid intensity change.
DIt converts the image to binary by thresholding pixel values.
Attempts:
2 left
💡 Hint
Think about what the second derivative tells us about intensity changes.
Hyperparameter
advanced
1:30remaining
Choosing kernel size for Sobel filter
What is the effect of increasing the kernel size (ksize) parameter in OpenCV's Sobel function from 3 to 7?
AIt decreases the smoothing effect, making the filter more sensitive to noise and fine details.
BIt increases the smoothing effect, resulting in detecting broader edges but less noise sensitivity.
CIt changes the filter to detect vertical edges instead of horizontal edges.
DIt converts the Sobel filter into a Laplacian filter automatically.
Attempts:
2 left
💡 Hint
Larger kernels average over more pixels.
🔧 Debug
advanced
2:00remaining
Identifying error in Laplacian filter code
What error will this code produce when applying the Laplacian filter, and why? import cv2 import numpy as np image = np.array([[1,2],[3,4]], dtype=np.uint8) laplacian = cv2.Laplacian(image, cv2.CV_8U) print(laplacian)
Computer Vision
import cv2
import numpy as np

image = np.array([[1,2],[3,4]], dtype=np.uint8)
laplacian = cv2.Laplacian(image, cv2.CV_8U)
print(laplacian)
AThe output will be incorrect due to overflow because CV_8U cannot represent negative values from the Laplacian.
BThe code will raise a SyntaxError because of missing parentheses.
CThe code will raise a TypeError because image is not a float array.
DThe output will be a zero matrix because the image is too small.
Attempts:
2 left
💡 Hint
Laplacian can produce negative values; unsigned types cannot represent them.
Model Choice
expert
2:30remaining
Best gradient method for detecting fine edges in noisy images
You want to detect fine edges in a noisy grayscale image. Which gradient method and parameter choice is best to maximize edge detection while minimizing noise?
AUse Laplacian of Gaussian (LoG) filter which combines smoothing and second derivative.
BUse Laplacian filter directly on the noisy image without smoothing.
CUse Sobel filter with kernel size 7 without any smoothing.
DUse Sobel filter with kernel size 3 and apply Gaussian blur before filtering.
Attempts:
2 left
💡 Hint
Consider methods that combine smoothing and edge detection.

Practice

(1/5)
1. What is the main purpose of using image gradients like Sobel and Laplacian in computer vision?
easy
A. To increase the color saturation of an image
B. To convert the image into grayscale
C. To blur the image and reduce noise
D. To detect edges by highlighting rapid changes in pixel brightness

Solution

  1. Step 1: Understand what image gradients do

    Image gradients detect changes in pixel brightness, which correspond to edges in images.
  2. Step 2: Match purpose with options

    Sobel and Laplacian filters highlight edges by showing where brightness changes quickly, not color or blur.
  3. Final Answer:

    To detect edges by highlighting rapid changes in pixel brightness -> Option D
  4. Quick Check:

    Image gradients = edge detection [OK]
Hint: Edges = brightness changes, gradients highlight these [OK]
Common Mistakes:
  • Confusing edge detection with color changes
  • Thinking gradients blur images
  • Assuming gradients convert images to grayscale
2. Which of the following is the correct way to apply a Sobel filter in OpenCV to detect horizontal edges?
easy
A. cv2.Laplacian(image, cv2.CV_64F)
B. cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
C. cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
D. cv2.Canny(image, 100, 200)

Solution

  1. Step 1: Recall Sobel filter parameters

    In OpenCV, Sobel's dx=1 and dy=0 detects horizontal edges (changes along x-axis).
  2. Step 2: Match parameters to options

    cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) uses dx=1, dy=0, which is correct for horizontal edge detection.
  3. Final Answer:

    cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) -> Option C
  4. Quick Check:

    dx=1, dy=0 means horizontal edges [OK]
Hint: dx=1, dy=0 for horizontal Sobel edges [OK]
Common Mistakes:
  • Swapping dx and dy values
  • Using Laplacian instead of Sobel for directional edges
  • Confusing Canny edge detector with Sobel
3. Given the following Python code using OpenCV, what will be the shape of the output image after applying the Laplacian filter?
import cv2
image = cv2.imread('photo.jpg', cv2.IMREAD_GRAYSCALE)
laplacian = cv2.Laplacian(image, cv2.CV_64F)
print(laplacian.shape)
medium
A. Same height and width as the input grayscale image
B. One channel smaller than input image
C. A 3-channel color image
D. A single pixel value

Solution

  1. Step 1: Understand Laplacian output size

    Laplacian filter outputs an image of the same size as input, preserving height and width.
  2. Step 2: Check input image type

    Input is grayscale (single channel), so output remains single channel with same dimensions.
  3. Final Answer:

    Same height and width as the input grayscale image -> Option A
  4. Quick Check:

    Laplacian output size = input size [OK]
Hint: Laplacian keeps image size same as input [OK]
Common Mistakes:
  • Expecting output to have fewer channels
  • Thinking Laplacian converts grayscale to color
  • Assuming output is a single pixel value
4. You wrote this code to apply Sobel filter but get an error:
import cv2
image = cv2.imread('img.png')
sobel = cv2.Sobel(image, cv2.CV_64F, 1, 0)
What is the likely cause of the error?
medium
A. Image path is incorrect
B. Sobel filter cannot be applied to color images
C. cv2.CV_64F is not a valid depth argument
D. Missing kernel size parameter 'ksize' in cv2.Sobel call

Solution

  1. Step 1: Check image loading

    cv2.imread('img.png') returns None if file does not exist, causing Sobel to fail.
  2. Step 2: Validate other parameters

    Sobel works on color images channel-wise, CV_64F is valid, ksize defaults to 3.
  3. Final Answer:

    Image path is incorrect -> Option A
  4. Quick Check:

    Check image is not None after imread [OK]
Hint: Always check if cv2.imread returns None [OK]
Common Mistakes:
  • Forgetting to verify image loaded
  • Assuming Sobel cannot handle color images
  • Believing ksize parameter is mandatory
5. You want to detect edges in all directions in a noisy grayscale image. Which approach is best to get clear edges while reducing noise?
hard
A. Apply Sobel filter directly without preprocessing
B. Apply Gaussian blur first, then use Laplacian filter
C. Use only Gaussian blur without edge detection
D. Apply Laplacian filter first, then Gaussian blur

Solution

  1. Step 1: Understand noise effect on edge detection

    Noise causes false edges; smoothing reduces noise before edge detection.
  2. Step 2: Choose correct filter order

    Applying Gaussian blur first smooths noise, then Laplacian detects edges in all directions clearly.
  3. Final Answer:

    Apply Gaussian blur first, then use Laplacian filter -> Option B
  4. Quick Check:

    Smooth then detect edges = clear edges [OK]
Hint: Blur noisy image before Laplacian for better edges [OK]
Common Mistakes:
  • Applying edge detection before noise reduction
  • Using Sobel only for all-direction edges
  • Skipping noise reduction step