Edge detection helps find where things change sharply in images. It shows outlines and shapes clearly.
Edge detection (Sobel, Laplacian) in Signal Processing
import cv2 import numpy as np # Read image in grayscale img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Sobel edge detection sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) sobel = cv2.magnitude(sobelx, sobely) # Laplacian edge detection laplacian = cv2.Laplacian(img, cv2.CV_64F)
Sobel detects edges by calculating gradients in horizontal and vertical directions.
Laplacian detects edges by finding areas where pixel intensity changes quickly in all directions.
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
laplacian = cv2.Laplacian(img, cv2.CV_64F)
This code creates a simple image with a white square on black background. It then applies Sobel and Laplacian edge detection. Finally, it prints the maximum and average edge strength values detected.
import cv2 import numpy as np # Create a simple black image with a white square img = np.zeros((100, 100), dtype=np.uint8) img[30:70, 30:70] = 255 # Sobel edge detection sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) sobel = cv2.magnitude(sobelx, sobely) # Convert to uint8 for display sobel_uint8 = np.uint8(np.absolute(sobel)) # Laplacian edge detection laplacian = cv2.Laplacian(img, cv2.CV_64F) laplacian_uint8 = np.uint8(np.absolute(laplacian)) # Print summary statistics to show edges detected print(f'Sobel edges - max value: {sobel_uint8.max()}, mean value: {sobel_uint8.mean():.2f}') print(f'Laplacian edges - max value: {laplacian_uint8.max()}, mean value: {laplacian_uint8.mean():.2f}')
Edges appear as bright lines where pixel values change sharply.
Sobel uses directional gradients, so combining horizontal and vertical results gives full edges.
Laplacian is sensitive to noise; smoothing the image first can help.
Edge detection finds sharp changes in images to highlight shapes.
Sobel detects edges by gradients in x and y directions.
Laplacian detects edges by looking at second derivatives in all directions.
Practice
Solution
Step 1: Understand Sobel operator function
The Sobel operator calculates the gradient of image intensity in horizontal and vertical directions.Step 2: Identify purpose in edge detection
Gradients highlight sharp changes in intensity, which correspond to edges.Final Answer:
To find edges by calculating gradients in horizontal and vertical directions -> Option CQuick Check:
Sobel = gradient edge detection [OK]
- Confusing Sobel with smoothing filters
- Thinking Sobel detects color changes
- Mixing Sobel with noise reduction methods
Solution
Step 1: Recall Sobel vertical kernel
The Sobel vertical kernel detects edges by emphasizing horizontal intensity changes and is defined as [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]].Step 2: Compare options
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] matches the known vertical Sobel kernel; others are horizontal or Laplacian kernels.Final Answer:
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] -> Option AQuick Check:
Vertical Sobel kernel = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] [OK]
- Confusing vertical and horizontal Sobel kernels
- Selecting Laplacian kernel instead
- Mixing signs in kernel values
[[10, 10, 10], [10, 0, 10], [10, 10, 10]]What is the approximate output of applying the Laplacian kernel
[[0, 1, 0], [1, -4, 1], [0, 1, 0]] at the center pixel?Solution
Step 1: Overlay kernel on image patch
Center pixel [1,1]=0. Relevant positions: top-center [0,1]=10, mid-left [1,0]=10, mid-right [1,2]=10, bottom-center [2,1]=10. Others have kernel weight 0.Step 2: Compute weighted sum
(1*10) + (1*10) + (-4*0) + (1*10) + (1*10) = 10 + 10 + 0 + 10 + 10 = 40.Final Answer:
40 -> Option DQuick Check:
Laplacian = sum(neighbors) - 4*center = 40 - 0 = 40 [OK]
- Ignoring kernel sign and weights
- Mixing neighbor and center pixel values
- Calculating sum incorrectly
import numpy as np
image = np.array([[1,2,1],[0,1,0],[2,1,2]])
sobel_x = np.array([[1,0,-1],[2,0,-2],[1,0,-1]])
result = np.zeros_like(image)
for i in range(1,2):
for j in range(1,2):
region = image[i-1:i+2, j-1:j+2]
result[i,j] = np.sum(region * sobel_x)
print(result[1,1])What is the output and is there any error?
Solution
Step 1: Analyze loop ranges and region extraction
Loops run for i=1 and j=1 only, extracting 3x3 region around center pixel (1,1).Step 2: Calculate convolution sum
Region = [[1,2,1],[0,1,0],[2,1,2]]
sobel_x = [[1,0,-1],[2,0,-2],[1,0,-1]]
Element-wise multiply and sum:
(1*1)+(2*0)+(1*-1)+(0*2)+(1*0)+(0*-2)+(2*1)+(1*0)+(2*-1) = 1+0-1+0+0+0+2+0-2 = 0Step 3: Check for errors
Loop ranges are correct for this small image to avoid index errors.
No error occurs.Final Answer:
0; no error -> Option BQuick Check:
Convolution sum = 0; loops correct [OK]
- Using wrong loop ranges causing index errors
- Miscomputing convolution sum
- Assuming output is non-zero without calculation
Solution
Step 1: Understand noise effect on edge detection
Noisy images cause false edges; smoothing reduces noise before edge detection.Step 2: Combine smoothing and edge detection
Applying Gaussian smoothing before Sobel reduces noise impact.
Then applying Laplacian on Sobel result enhances edge detection.Step 3: Evaluate other options
Applying Laplacian first without smoothing amplifies noise.
Using Sobel only misses benefits of combined filters.
Median filter after Laplacian may remove edges, not sharpen.Final Answer:
Apply Gaussian smoothing before Sobel, then use Laplacian on result -> Option AQuick Check:
Smoothing before edge detection improves results [OK]
- Skipping smoothing step on noisy images
- Applying filters in wrong order
- Assuming median filter sharpens edges
Ready to prove it?
Pass the test and this pattern moves unknown
