Concept Flow - Image sharpening
Input: Original Image
Apply Sharpening Filter
Enhance Edges & Details
Output: Sharpened Image
Image sharpening enhances edges and details by applying a filter to the original image, producing a clearer output.
import cv2 import numpy as np kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) sharpened = cv2.filter2D(src=image, ddepth=-1, kernel=kernel)
| Step | Pixel Position (x,y) | Original Pixel Value | Kernel Applied | New Pixel Value | Explanation |
|---|---|---|---|---|---|
| 1 | (1,1) | 100 | Center * 5 | 500 | Center pixel multiplied by 5 |
| 2 | (1,1) | Neighbors | Sum of neighbors * -1 | -400 | Sum of neighbors multiplied by -1 |
| 3 | (1,1) | Calculate | 500 + (-400) | 100 | Sum center and neighbors result |
| 4 | (1,1) | Clamp | Value within 0-255 | 100 | Value clamped to valid pixel range |
| 5 | (1,2) | Repeat | Apply kernel similarly | Value updated | Process repeats for next pixel |
| ... | ... | ... | ... | ... | ... |
| N | All pixels processed | - | - | Sharpened image ready | All pixels updated with filter |
| Exit | - | - | - | - | All pixels processed, sharpening complete |
| Variable | Start | After Pixel (1,1) | After Pixel (1,2) | ... | Final |
|---|---|---|---|---|---|
| image | Original image matrix | Unchanged | Unchanged | ... | Unchanged |
| kernel | [[0,-1,0],[-1,5,-1],[0,-1,0]] | Unchanged | Unchanged | ... | Unchanged |
| sharpened | Empty matrix | Pixel (1,1) updated | Pixel (1,2) updated | ... | Complete sharpened image |
Image sharpening uses a filter kernel to enhance edges. The center pixel is emphasized; neighbors are subtracted. Apply convolution to each pixel. Clamp results to 0-255 range. Output is a sharper, clearer image.
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)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()Pass the test and this pattern moves unknown