0% found this document useful (0 votes)
2 views7 pages

lab3

Chapter 3 focuses on image enhancement techniques in the spatial domain, including intensity transformation functions, histogram processing, and spatial filtering. Key methods discussed include contrast stretching, histogram equalization, and various filtering techniques for noise reduction and edge sharpening. The chapter provides practical code examples using OpenCV and Python to implement these enhancements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

lab3

Chapter 3 focuses on image enhancement techniques in the spatial domain, including intensity transformation functions, histogram processing, and spatial filtering. Key methods discussed include contrast stretching, histogram equalization, and various filtering techniques for noise reduction and edge sharpening. The chapter provides practical code examples using OpenCV and Python to implement these enhancements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/24/25, 7:57 PM Ch3_Image_Processing.

ipynb - Colab

keyboard_arrow_down 🧠 Chapter 3 – Image Enhancement in the Spatial Domain


🔹 What Is Image Enhancement?
Image enhancement means improving an image to make it more useful or visually clear.

Goals:

Highlight important details.


Remove noise.
Make images more visually appealing.

🟩 1. Intensity Transformation Functions


These functions modify pixel intensity values to improve contrast or brightness.

1.1 Contrast Stretching


Expands the range of gray levels to make the image clearer.

import cv2, numpy as np, [Link] as plt


img = [Link]('[Link]', 0)
min_val, max_val = [Link](img), [Link](img)
stretched = (img - min_val) * (255 / (max_val - min_val))
stretched = [Link](np.uint8)

[Link](1,2,1); [Link](img, cmap='gray'); [Link]('Original')


[Link](1,2,2); [Link](stretched, cmap='gray'); [Link]('Contrast Stretched')
[Link]()

📘 Idea: Expands intensity range from low contrast → full 0–255 range.

1.2 Intensity Level Slicing


Highlights a specific range of intensity values (useful to emphasize certain features).

low, high = 100, 180


sliced = [Link]((img >= low) & (img <= high), 255, 50)

[Link](sliced, cmap='gray'); [Link]('Intensity Level Slicing')


[Link]()

📘 Idea: Pixels inside the chosen range become bright; others are darkened.

1.3 Bit-Plane Slicing


Separates image bits to show how each bit contributes to the image.

bit_planes = [(img >> i) & 1 for i in range(8)]


[Link](figsize=(12,6))
for i in range(8):
[Link](2,4,i+1)
[Link](bit_planes[i]*255, cmap='gray')
[Link](f'Bit plane {i}')
[Link]()

📘 Idea:
Higher-order bits → main image details.
Lower-order bits → fine details or noise.

🟩 2. Histogram Processing
A histogram shows how pixel intensities are distributed.

2.1 Histogram Equalization


Automatically improves contrast by spreading pixel intensity values evenly.

eq = [Link](img)
[Link](1,2,1); [Link](img, cmap='gray'); [Link]('Original')
[Link](1,2,2); [Link](eq, cmap='gray'); [Link]('Histogram Equalized')
[Link]()

📘 Idea: Redistributes pixel values → clearer contrast and more balanced brightness.

2.2 Histogram Matching (Specification)


Adjusts one image’s histogram to match another image.

def match_histogram(src, ref):


src_hist, bins = [Link]([Link](), 256, [0,256])
ref_hist, _ = [Link]([Link](), 256, [0,256])

[Link] 1/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab
src_cdf = [Link](src_hist) / [Link](src_hist)
ref_cdf = [Link](ref_hist) / [Link](ref_hist)
mapping = [Link](src_cdf, ref_cdf, [Link](256))
return mapping[src].astype(np.uint8)

ref = [Link]('[Link]', 0)
matched = match_histogram(img, ref)
[Link](matched, cmap='gray'); [Link]('Histogram Matched')
[Link]()

📘 Idea: Makes one image look like another in terms of brightness and contrast.

2.3 Local Histogram Processing


Performs enhancement on small local areas (not globally).

📘 Idea: Enhances local contrast in small regions, useful for images with uneven lighting.

🟩 3. Spatial Filtering
Processes an image by operating on a neighborhood around each pixel.

3.1 Smoothing Spatial Filters (Low-pass)


Used for blurring or noise reduction.

(a) Averaging Filter

blur = [Link](img, (5,5))


[Link](blur, cmap='gray'); [Link]('Averaging Filter')
[Link]()

📘 Idea: Each pixel becomes the average of its neighbors.

(b) Median Filter

median = [Link](img, 5)
[Link](median, cmap='gray'); [Link]('Median Filter')
[Link]()

📘 Idea:
Sorts neighboring pixels.
Replaces the center pixel with the median value.
Removes salt-and-pepper noise while preserving edges.

🟩 4. Sharpening Spatial Filters (High-pass)


Used to highlight edges and fine details.

4.1 Laplacian Filter

lap = [Link](img, cv2.CV_64F)


sharp = [Link](img - lap)
[Link](sharp, cmap='gray'); [Link]('Laplacian Sharpened')
[Link]()

📘 Idea: Computes the second derivative to detect rapid intensity changes (edges).

4.2 Sobel Operator


Detects edges in horizontal and vertical directions.

sobelx = [Link](img, cv2.CV_64F, 1, 0, ksize=3)


sobely = [Link](img, cv2.CV_64F, 0, 1, ksize=3)
sobel = [Link]([Link](sobelx, sobely))
[Link](sobel, cmap='gray'); [Link]('Sobel Edge Detection')
[Link]()

📘 Idea: Measures the first derivative → strong response where brightness changes quickly.

4.3 Unsharp Masking / High-Boost Filtering


Used to make images sharper and more detailed.

blurred = [Link](img, (5,5), 0)


mask = img - blurred
A = 1.5 # boost factor
high_boost = [Link](img, A, mask, 1, 0)
[Link](high_boost, cmap='gray'); [Link]('High-Boost Sharpening')
[Link]()

📘 Idea:

[Link] 2/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab
Subtract the blurred image from the original → keep only edges.
Add back (boosted) edges to enhance details.
If A > 1 → stronger sharpening

Double-click (or enter) to edit

1 import cv2, numpy as np, [Link] as plt


2 img = [Link]("/content/images (3).jpeg")
3 min_val, max_val = [Link](img), [Link](img)
4 stretched = (img - min_val) * (255 / (max_val - min_val))
5 stretched = [Link](np.uint8)
6
7 [Link](1,2,1); [Link](img,cmap='gray'); [Link]('Original')
8 [Link](1,2,2); [Link](stretched, cmap='gray'); [Link]('Contrast Stretched')
9 [Link]()

1 low, high = 100, 180


2 sliced = [Link]((img >= low) & (img <= high), 255, 50)
3
4 [Link](sliced, cmap='gray'); [Link]('Intensity Level Slicing')
5 [Link]()

1 bit_planes = [(img >> i) & 1 for i in range(8)]


2 [Link](figsize=(12,6))
3 for i in range(8):
4 [Link](2,4,i+1)
5 [Link](bit_planes[i]*255, cmap='gray')
6 [Link](f'Bit plane {i}')
7 [Link]()

[Link] 3/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab
1 img1 = [Link]("/content/images (3).jpeg")
2 gray = [Link](img, cv2.COLOR_BGR2GRAY)
3
4 # 2. Convert to float32
5 # gray = np.float32(gray)
6 eq = [Link](gray)
7 [Link](1,2,1); [Link](img, cmap='gray'); [Link]('Original')
8 [Link](1,2,2); [Link](eq, cmap='gray'); [Link]('Histogram Equalized')
9 [Link]()

1 def match_histogram(src, ref):


2 src_hist, bins = [Link]([Link](), 256, [0,256])
3 ref_hist, _ = [Link]([Link](), 256, [0,256])
4 src_cdf = [Link](src_hist) / [Link](src_hist)
5 ref_cdf = [Link](ref_hist) / [Link](ref_hist)
6 mapping = [Link](src_cdf, ref_cdf, [Link](256))
7 return mapping[src].astype(np.uint8)
8 img1 = [Link]("/content/[Link]")
9 ref = [Link]('/content/images (4).jpeg', 0)
10 matched = match_histogram(img1, ref)
11 [Link](matched, cmap='gray'); [Link]('Histogram Matched')
12 [Link]()

1 img3=[Link]("/content/[Link]")
2 blur = [Link](img3, (3,3))
3 [Link](blur, cmap='gray'); [Link]('Averaging Filter')
4 [Link]()

1 median = [Link](img3, 3)
2 [Link](median, cmap='gray'); [Link]('Median Filter')
3 [Link]()

[Link] 4/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab

1 lap = [Link](img, cv2.CV_64F)


2 sharp = [Link](img - lap)
3 [Link](sharp, cmap='gray'); [Link]('Laplacian Sharpened')
4 [Link]()

1 sobelx = [Link](img, cv2.CV_64F, 1, 0, ksize=3)


2 sobely = [Link](img, cv2.CV_64F, 0, 1, ksize=3)
3 sobel = [Link]([Link](sobelx, sobely))
4 [Link](sobel, cmap='gray'); [Link]('Sobel Edge Detection')
5 [Link]()

1 blurred = [Link](img, (5,5), 0)


2 mask = img - blurred
3 A = 2 # boost factor
4 high_boost = [Link](img, A, mask, 1, 0)
5 [Link](high_boost, cmap='gray'); [Link]('High-Boost Sharpening')
6 [Link]()

[Link] 5/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab

keyboard_arrow_down New section

1
2 import cv2
3 import numpy as np
4 import [Link] as plt
5
6 # Step 1: Read the image
7 img = [Link]('/content/[Link]', 0)
8
9 # Step 2: Apply Laplacian filter for edge sharpening
10 lap = [Link](img, cv2.CV_64F)
11 lap_abs = [Link](lap)
12 sharpened = [Link](img, lap_abs)
13
14 # Step 3: Apply histogram equalization for better contrast
15 equalized = [Link](sharpened)
16
17 # Step 4: Apply smoothing to reduce any noise
18 smoothed = [Link](equalized, (3,3), 0)
19
20 # Step 5: Combine original + processed image (weighted sum)
21 combined = [Link](img, 0.5, smoothed, 0.5, 0)
22
23 # Show all steps
24 titles = ['Original', 'Laplacian Sharpened', 'Equalized', 'Smoothed', 'Final Combined']
25 images = [img, sharpened, equalized, smoothed, combined]
26
27 [Link](figsize=(12,6))
28 for i in range(5):
29 [Link](2,3,i+1)
30 [Link](images[i], cmap='gray')
31 [Link](titles[i])
32 [Link]('off')
33 plt.tight_layout()
34 [Link]()

[Link] 6/7
10/24/25, 7:57 PM Ch3_Image_Processing.ipynb - Colab

[Link] 7/7

You might also like