Image sharpening makes pictures clearer by highlighting edges and details. It helps us see important parts better.
Image sharpening in Signal Processing
Introduction
Syntax
Signal Processing
sharpened_image = original_image + alpha * (original_image - blurred_image)
This formula is called unsharp masking.
Alpha controls how strong the sharpening effect is.
Examples
Signal Processing
import cv2 import numpy as np original = cv2.imread('photo.jpg', cv2.IMREAD_GRAYSCALE) blurred = cv2.GaussianBlur(original, (5,5), 0) alpha = 1.5 sharpened = cv2.addWeighted(original, 1 + alpha, blurred, -alpha, 0)
Signal Processing
from scipy.ndimage import gaussian_filter original = image_array blurred = gaussian_filter(original, sigma=1) alpha = 1.0 sharpened = original + alpha * (original - blurred)
Sample Program
This code creates a small image, blurs it, and sharpens it using unsharp masking. It prints all steps so you can see the effect.
Signal Processing
import cv2 import numpy as np # Create a simple 5x5 grayscale image with a bright square in the center original = np.array([ [50, 50, 50, 50, 50], [50, 200, 200, 200, 50], [50, 200, 255, 200, 50], [50, 200, 200, 200, 50], [50, 50, 50, 50, 50] ], dtype=np.uint8) # Blur the image using Gaussian blur blurred = cv2.GaussianBlur(original, (3,3), 0) # Sharpening factor alpha = 1.0 # Apply unsharp masking sharpened = cv2.addWeighted(original, 1 + alpha, blurred, -alpha, 0) print('Original Image:\n', original) print('\nBlurred Image:\n', blurred) print('\nSharpened Image:\n', sharpened)
Important Notes
Too much sharpening can make images look unnatural or noisy.
Sharpening works best on images that are slightly blurry.
Use grayscale images first to understand sharpening before color images.
Summary
Image sharpening highlights edges to make pictures clearer.
Unsharp masking is a common way to sharpen images by subtracting a blurred version.
Adjust the sharpening strength carefully to avoid unwanted noise.
Practice
1. What is the main purpose of image sharpening in signal processing?
easy
Solution
Step 1: Understand image sharpening
Image sharpening enhances edges and details to make images clearer.Step 2: Compare options
Only To highlight edges and make the image clearer describes highlighting edges and clarity, which is the goal of sharpening.Final Answer:
To highlight edges and make the image clearer -> Option BQuick Check:
Image sharpening = highlight edges [OK]
Hint: Sharpening means making edges clearer, not blurring [OK]
Common Mistakes:
- Confusing sharpening with blurring
- Thinking sharpening changes image size
- Assuming sharpening converts color formats
2. Which of the following is the correct Python syntax to apply a simple sharpening kernel using convolution with OpenCV?
easy
Solution
Step 1: Recall OpenCV function for convolution
OpenCV uses cv2.filter2D() to apply kernels for filtering images.Step 2: Check options
Only cv2.filter2D(image, -1, kernel) uses cv2.filter2D with correct parameters; others are invalid functions.Final Answer:
cv2.filter2D(image, -1, kernel) -> Option CQuick Check:
OpenCV convolution = filter2D [OK]
Hint: Use cv2.filter2D for custom kernel convolution [OK]
Common Mistakes:
- Using non-existent OpenCV functions
- Confusing convolution with sharpening function names
- Incorrect parameter order
3. Given the following Python code using OpenCV, what will be the output type of
sharpened?
import cv2
import numpy as np
image = cv2.imread('photo.jpg')
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(image, -1, kernel)medium
Solution
Step 1: Understand filter2D output
cv2.filter2D returns an image as a NumPy array after applying the kernel.Step 2: Analyze kernel and output
The kernel is a sharpening kernel, so output is sharpened image, still in color and array form.Final Answer:
A sharpened image as a NumPy array -> Option AQuick Check:
filter2D output = NumPy array image [OK]
Hint: filter2D returns image array, not blurred or grayscale [OK]
Common Mistakes:
- Assuming output is blurred instead of sharpened
- Thinking output is grayscale without conversion
- Believing kernel shape causes error
4. Identify the error in this Python code snippet for image sharpening:
import cv2
import numpy as np
image = cv2.imread('img.png')
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(image, 1, kernel)
cv2.imshow('Sharpened', sharpened)
cv2.waitKey(0)
cv2.destroyAllWindows()medium
Solution
Step 1: Check ddepth parameter in filter2D
ddepth=-1 means output image has same depth as input; 1 is invalid and may cause errors.Step 2: Validate other code parts
Kernel shape is valid, cv2.imshow supports color, waitKey(0) is correct usage.Final Answer:
The ddepth parameter should be -1, not 1 -> Option AQuick Check:
ddepth=-1 for same depth [OK]
Hint: Use ddepth=-1 in filter2D to keep image depth [OK]
Common Mistakes:
- Using wrong ddepth value causing errors
- Thinking kernel shape is wrong
- Misunderstanding cv2.imshow capabilities
5. You want to sharpen an image using unsharp masking. Which sequence of steps correctly applies this method?
hard
Solution
Step 1: Understand unsharp masking process
Unsharp masking involves blurring the image, subtracting this blur from original to get edges, then adding edges back.Step 2: Evaluate options
Only Blur the image, subtract the blurred from original, then add result back to original correctly describes this sequence; others miss key steps or add unrelated actions.Final Answer:
Blur the image, subtract the blurred from original, then add result back to original -> Option DQuick Check:
Unsharp masking = blur, subtract, add [OK]
Hint: Unsharp masking = blur, subtract, add back edges [OK]
Common Mistakes:
- Skipping the blur step
- Adding noise instead of subtracting blur
- Applying sharpening kernel directly without unsharp steps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
