0% found this document useful (0 votes)
15 views

Basic opencv

The document outlines a practice assignment on digital image and video processing, focusing on various techniques for color transformation, geometry transformation, and image smoothing. It includes detailed algorithms for brightness and contrast modification, nonlinear mapping functions, and histogram-based methods such as histogram equalization and specification. The document also provides test cases to illustrate the effects of these transformations on images.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Basic opencv

The document outlines a practice assignment on digital image and video processing, focusing on various techniques for color transformation, geometry transformation, and image smoothing. It includes detailed algorithms for brightness and contrast modification, nonlinear mapping functions, and histogram-based methods such as histogram equalization and specification. The document also provides test cases to illustrate the effects of these transformations on images.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNIVERSITY OF SCIENCE

VIETNAM NATIONAL UNIVERSITY, HO CHI MINH CITY

PRACTICE 1

BASIC - OPENCV

22TGMT

Subject : Digital image & video processing

Student’s Name: : Nguyễn Quốc Thắng

Student’s ID: : 22127385

Lecturer : PGS.TS Lý Quốc Ngọc


Ths Phạm Thanh Tùng
Ths Nguyễn Mạnh Hùng
Digital image & video processing

Mục lục
1 COLOR TRANSFORMATION 2
1.1 Linear Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Brightness Modification . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Contrast Modification . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3 Brightness + Contrast Modification . . . . . . . . . . . . . . . . . . . 3
1.1.4 Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Nonlinear Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 Logarithmic Mapping Function . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Exponential Mapping Function . . . . . . . . . . . . . . . . . . . . . 6
1.3 Probability Density Function-Based Mapping . . . . . . . . . . . . . . . . . 7
1.3.1 Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.2 Histogram Specification . . . . . . . . . . . . . . . . . . . . . . . . . 10

2 GEOMETRY TRANSFORMATION 11
2.1 Pixel Co-ordinate Transformations . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.1 Bilinear Transformation . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.2 Affine Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2 Brightness Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.1 Nearest-Neighborhood Interpolation . . . . . . . . . . . . . . . . . . . 16
2.2.2 Linear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.3 Test Cases: Nearest-Neighborhood vs. Linear Interpolation . . . . . . 16
2.2.4 Nearest-Neighborhood Interpolation . . . . . . . . . . . . . . . . . . . 17
2.2.5 Linear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2.6 Test Cases: Nearest-Neighborhood vs. Linear Interpolation . . . . . . 18

3 IMAGE SMOOTHING 19
3.1 Average Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Gaussian Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

4 References 28

1
Digital image & video processing

1 COLOR TRANSFORMATION

1.1 Linear Mapping

1.1.1 Brightness Modification

Brightness modification adjusts the pixel values of an image by adding a constant b to


each pixel. The transformation formula is:

f ′ (x, y) = f (x, y) + b

where f (x, y) represents the original pixel value, and f ′ (x, y) represents the modified
pixel value.

Algorithm:

• Input:

– img_2d: Input image represented as a 2D array of pixel values.


– brightness: An integer value that is added to each pixel in the image to modify
the brightness.

• Output:

– bright_img: Output image with modified brightness, returned as a PIL Image.

• Detailed Steps:

1. Convert the input image to an unsigned 8-bit integer array:

img_2d = np.uint8(img_2d)

2. Add the brightness value b to each pixel in the image:

img_2d = np.uint8(img_2d) + float(brightness)

3. Clip the pixel values to ensure they stay within the valid range [0, 255]:

img_2d = np.clip(img_2d, 0, 255)

4. Convert the modified image array back to a PIL Image:

bright_img = Image.fromarray(np.uint8(img_2d))

2
Digital image & video processing

1.1.2 Contrast Modification

Contrast modification adjusts the pixel intensities of an image by multiplying each pixel
by a constant c. The transformation formula is:

f ′ (x, y) = c · f (x, y)

where f (x, y) represents the original pixel value, and f ′ (x, y) represents the modified
pixel value.

Algorithm:

• Input:

– img_2d.
– contrast_factor.

• Output:

– contrast_img: Output image with modified contrast, returned as a PIL Image.

• Detailed Steps:

1. Convert the input image to a 32-bit float array to ensure precision during the
transformation.
2. Multiply each pixel by the contrast factor.
3. Clip the pixel values to ensure they stay within the valid range [0, 255].
4. Convert the modified image array back to an unsigned 8-bit integer format.

1.1.3 Brightness + Contrast Modification

Brightness + Contrast Modification combines both brightness and contrast adjustments.


The transformation formula is:

f ′ (x, y) = c · f (x, y) + b

where: - f (x, y) is the original pixel value, - f ′ (x, y) is the modified pixel value, - c is the
contrast factor, - b is the brightness adjustment value.

Algorithm:

• Input:

– img_2d.
– contrast_factor.
– brightness_value.

• Output:

3
Digital image & video processing

– modified_img.

• Detailed Steps:

1. Convert the input image to a 32-bit float array for precision.


2. Multiply each pixel by the contrast factor and add the brightness value:

img_2d = img_2d × contrast_factor + brightness_value

3. Clip the pixel values to ensure they remain within the valid range [0, 255].
4. Convert the modified image array back to an unsigned 8-bit integer format.

1.1.4 Test Cases

Test Case 1: Brightness Increase

• Input: Original image.

• Output: Image with brightness increased by b.

Hình 1: Brightness increased by b = 50.

Test Case 2: Contrast Increase

• Input: Original image.

• Output: Image with contrast increased by c.

4
Digital image & video processing

Hình 2: Contrast increased by c = 1.5.

Test Case 3: Combined Brightness and Contrast Adjustment

• Input: Original image.


• Output: Image with both brightness and contrast adjusted.

Hình 3: Combined brightness b = 50 and contrast adjustment c = 1.5.

1.2 Nonlinear Mapping

1.2.1 Logarithmic Mapping Function

This transformation enhances details in darker regions. The formula is:

f ′ (x, y) = c · log(1 + f (x, y))

where f (x, y) represents the intensity of the pixel at coordinates (x, y), and f ′ (x, y) is
the transformed intensity. The constant c controls the strength of the mapping.

Input:

• img_2d: Input image represented as a 2D array (numpy.ndarray) containing pixel


values.
• c: Constant multiplier for the logarithmic function, which adjusts the effect of the
transformation.

5
Digital image & video processing

Output:

• log_img: Logarithmic mapped image as a PIL Image, where pixel values are transformed
using the logarithmic function.

Algorithm:

• Convert the input image to a float32 data type to allow precise operations.

• Apply the logarithmic transformation using the formula f ′ (x, y) = c · log(1 + f (x, y)),
where log(1 + x) is computed for each pixel.

• Clip the resulting pixel values to the range [0, 255] to maintain valid image intensity
levels.

• Convert the processed image back to a uint8 format and return it as a PIL Image.

Test case 1: Given an input image and constant c = 40.

Hình 4: Logarithmic Mapped Image (c = 40)

1.2.2 Exponential Mapping Function

This transformation enhances pixel intensities in a nonlinear way. The formula is:

f ′ (x, y) = c · ef (x,y) − 1


where f (x, y) represents the intensity of the pixel at coordinates (x, y), and f ′ (x, y) is
the transformed intensity. The constant c controls the strength of the exponential mapping.

Input:

• img_2d:.

• c: Constant multiplier for the exponential function, which adjusts the effect of the
transformation.

6
Digital image & video processing

Output:

• exp_img: Exponentially mapped image as a PIL Image, where pixel values are
transformed using the exponential function.

Algorithm:

• Convert the input image to a float32 data type to allow precise operations.

• Normalize the pixel values to the range [0, 1] by dividing the image by 255.

• Apply the exponential transformation using the formula f ′ (x, y) = c·(ef (x,y) −1), where
ex is computed for each pixel.

• Scale the result back to the range [0, 255] and clip the values to avoid overflow.

• Convert the processed image back to uint8 format and return it as a PIL Image.

Test case 2: c = 3

Hình 5: Exponentially Mapped Image with c = 3

1.3 Probability Density Function-Based Mapping

Probability density function-based mapping methods are used to manipulate image intensities
based on statistical properties of their histograms. These techniques help enhance image
contrast or match a specific intensity distribution.

1.3.1 Histogram Equalization

Idea: Histogram Equalization is a technique used to improve the contrast of an image by


redistributing pixel intensities to achieve a uniform histogram. This method is particularly
useful for enhancing the details in dark regions and reducing overexposed bright areas,
making the image details more visible.

7
Digital image & video processing

Input:

• The input image can be grayscale or RGB.

• For RGB images, the histogram equalization is applied separately to each color channel
(red, green, blue).

Output:

• An image with enhanced contrast, after applying histogram equalization.

• For RGB images, the output is a color image with contrast adjustment applied to each
channel.

Algorithm:

1. Convert the image to grayscale if it is a color image.

2. Create a histogram array H with size nG = 256, where each element in the array stores
the frequency of each pixel intensity in the image.

3. Compute the cumulative histogram T based on H, which represents the cumulative


sum of pixel frequencies.

4. Create a lookup table T that maps the original pixel intensities to new values, rescaled
to the range [0, nG − 1].

5. Apply the lookup table T to the original image to obtain the equalized image by
mapping the original pixel intensities to their new values.

Test Case 1: Grayscale Image (Low-Contrast)

• Input: A low-contrast grayscale image.

• Output: Histogram equalization applied, enhancing the image contrast.

Hình 6: Grayscale Image with Low Contrast Before and After Histogram Equalization.

8
Digital image & video processing

Test Case 2: Grayscale Image (High-Contrast)

• Input: A high-contrast grayscale image.

• Output: Histogram equalization applied, further enhancing the contrast.

Hình 7: Grayscale Image with High Contrast Before and After Histogram Equalization.

Test Case 3: RGB Image

• Input: An RGB image.

• Output: Histogram equalization applied to each of the RGB channels individually,


enhancing the contrast.

Hình 8: RGB Image Before and After Histogram Equalization.

Test Case 4: Uniform Intensity Image

• Input: A uniform intensity image (where all pixels have the same intensity value).

• Output: Histogram equalization should not cause any significant change as the image
has uniform intensity.

9
Digital image & video processing

Hình 9: Uniform Intensity Image Before and After Histogram Equalization (No Significant
Change).

1.3.2 Histogram Specification

Idea: The goal of histogram specification is to adjust the histogram of a given image such
that it closely resembles the histogram of a reference image or a set of reference images.
This is achieved by matching the cumulative distribution function (CDF) of the input image
to that of the reference image(s). The resulting image has a similar distribution of pixel
intensities as the reference.

Input:

• img_2d: The input image (2D array of pixel intensities).

• ref_imgs_2d: A list of reference images (2D arrays of pixel intensities) to guide the
transformation.

Output: The output is an image where the histogram of the input image has been transformed
to match the histogram of the reference images.

Algorithm: The histogram specification algorithm involves the following steps:

1. Convert the input image to grayscale if it is not already.

2. Compute the histogram of the input image and the average histogram of the reference
images.

3. Compute the cumulative distribution function (CDF) for both the input image and
the reference images.

4. Construct lookup tables from the CDFs of the input and reference images.

5. Map the input image histogram to match the reference histogram using inverse CDF
lookups.

6. Produce the final output image with the desired histogram specification.

10
Digital image & video processing

Test Case 1: Grayscale Image (Low-Contrast vs High-Contrast)

• Input: A low-contrast grayscale image and a high-contrast grayscale image.


• Output: The histograms of the input image(s) are transformed to match the histogram
of the reference image(s), improving the contrast in the low-contrast image and potentially
reducing extreme contrast in the high-contrast image.

Hình 10: Low-Contrast vs High-Contrast Grayscale Images Before and After Histogram
Specification.

Test Case 2: Grayscale Image (One vs Multiple Reference Histograms)

• Input: A grayscale image and one or multiple reference grayscale images.


• Output: The histogram of the input image is transformed to match the histogram
of the single reference image in the first case and the average histogram of multiple
reference images in the second case. The transformation will be influenced by the
reference image(s).

Hình 11: Grayscale Image Before and After Histogram Specification (One vs Multiple
Reference Histograms).

2 GEOMETRY TRANSFORMATION

2.1 Pixel Co-ordinate Transformations

2.1.1 Bilinear Transformation

Idea: The inverse bilinear transformation computes the new pixel coordinates in the transformed
image using the following formulas:

11
Digital image & video processing

x ′ = a0 + a1 · j + a2 · i + a3 · j · i
y ′ = b0 + b1 · j + b2 · i + b3 · j · i

where: - (x′ , y ′ ) are the transformed coordinates of the pixel in the output image, - (i, j)
are the pixel coordinates in the input image, - a0 , a1 , a2 , a3 and b0 , b1 , b2 , b3 are the coefficients
that define the transformation.
The algorithm applies these formulas for each pixel in the output image and then uses
bilinear interpolation to obtain the pixel values corresponding to the new coordinates.

Input:

• img: The input image represented as a 3D numpy array (RGB).

• a0, a1, a2, a3: Coefficients for the transformation of the x-coordinate.

• b0, b1, b2, b3: Coefficients for the transformation of the y-coordinate.

Output: The output is a transformed image, represented as a 3D numpy array (RGB),


after applying the inverse bilinear transformation to the input image.

Algorithm: The algorithm proceeds as follows:

1. Create an empty output image with the same dimensions as the input image.

2. For each pixel in the output image, compute its corresponding transformed coordinates
(x, y) using the transformation coefficients.

3. If the computed coordinates are valid (within the bounds of the image), apply bilinear
interpolation to compute the pixel value at that position.

4. Store the computed pixel value in the output image.

5. Return the transformed image.

Test Case 1: Scaling (Reduce the image)

• Input: A RGB image and transformation coefficients (a0 , a1 , a2 , a3 ) = (0, 2, 0, 0) and


(b0 , b1 , b2 , b3 ) = (0, 0, 2, 0) for reducing the image size.

• Output: The image will be reduced in size by a factor of 2.

12
Digital image & video processing

Hình 12: Scaling Transformation: Reduced Image.

Test Case 2: Scaling (Enlarge the image)

• Input: A RGB image and transformation coefficients (a0 , a1 , a2 , a3 ) = (0, 0.5, 0, 0) and
(b0 , b1 , b2 , b3 ) = (0, 0, 0.5, 0) for enlarging the image size.

• Output: The image will be enlarged by a factor of 2.

Hình 13: Scaling Transformation: Enlarged Image.

Test Case 3: Shearing

• Input: A RGB image and transformation coefficients (a0 , a1 , a2 , a3 ) = (1, 0, 0.5, 0) and
(b0 , b1 , b2 , b3 ) = (0, 1, 0, 0) for applying a shear transformation.

• Output: The image will be sheared, distorting the horizontal and vertical alignments.

13
Digital image & video processing

Hình 14: Shearing Transformation: Sheared Image.

2.1.2 Affine Transform

Affine transformation is a linear mapping method that preserves points, straight lines,
and planes. In the context of image processing, it involves applying a combination of scaling,
translation, rotation, and skewing to an image. The transformation uses a set of coefficients
to map each pixel’s position in the original image to its new position in the transformed
image.

Idea: The affine transformation for an image is defined by the following equations:

x ′ = a0 + a1 · j + a2 · i
y ′ = b0 + b 1 · j + b 2 · i

where: - (x′ , y ′ ) are the transformed coordinates of the pixel in the output image, -
(i, j) are the pixel coordinates in the input image, - a0 , a1 , a2 are the coefficients for the
transformation of the x-coordinate, - b0 , b1 , b2 are the coefficients for the transformation of
the y-coordinate.

Input:

• img: The input image represented as a 3D numpy array (RGB).

• a0, a1, a2: Coefficients for the x-coordinate transformation.

• b0, b1, b2: Coefficients for the y-coordinate transformation.

Output: The output is a transformed image, represented as a 3D numpy array (RGB),


after applying the affine transformation to the input image.

Algorithm: The algorithm proceeds as follows:

1. Create an empty output image with the same dimensions as the input image.

2. For each pixel in the input image, calculate its corresponding transformed coordinates
(x′ , y ′ ) using the affine transformation equations.

14
Digital image & video processing

3. If the transformed coordinates are valid (within the bounds of the image), copy the
pixel value from the input image to the output image.

4. Return the transformed image.

Test Case 1: Skew Transformation

• Input: A RGB image and transformation coefficients (a0 , a1 , a2 ) = (0, 1, 0.5) and
(b0 , b1 , b2 ) = (0, 0.5, 1) for applying a skew transformation.

• Output: The image will undergo a skew effect, where the x and y coordinates are
modified, but the image remains the same in overall shape.

Hình 15: Skew Transformation: Skewed Image.

Test Case 2: Left Rotation Transformation For a left rotation by 60◦ , the affine
transformation can be represented by the following coefficients:

a0 = 0, a1 = cos(60◦ ), a2 = − sin(60◦ )
b0 = 0, b1 = sin(60◦ ), b2 = cos(60◦ )

This transformation will rotate the image counterclockwise by 60◦ . In this case: - The
x-coordinate will be inverted and shifted vertically. - The y-coordinate will remain within
the image’s bounds.

• Input: A RGB image and transformation coefficients (a0 , a1 , a2 ) = (0, cos(60◦ ), − sin(60◦ ))
and (b0 , b1 , b2 ) = (0, sin(60◦ ), cos(60◦ )) for rotating the image by 60◦ counterclockwise.

• Output: The image will be rotated by 60◦ counterclockwise.

15
Digital image & video processing

Hình 16: Left Rotation Transformation: Rotated Image.

2.2 Brightness Interpolation

2.2.1 Nearest-Neighborhood Interpolation

Nearest-neighborhood interpolation assigns the intensity value of the nearest neighboring


pixel to the new pixel position. It is computationally fast but may result in a blocky
appearance.

Formula: The nearest-neighborhood interpolation is defined as:


xnearest = round(x), ynearest = round(y)

2.2.2 Linear Interpolation

Linear interpolation calculates the intensity of a new pixel as a weighted average of the
neighboring pixels. It provides smoother results compared to nearest-neighborhood interpolation.

Formula: If the pixel coordinates are (k, l) with fractional offsets a and b, the interpolated
pixel value is calculated as:
transformed_img[i, j] = (1−a)(1−b)·img[k, l]+a(1−b)·img[k, l+1]+(1−a)b·img[k+1, l]+ab·img[k+1, l+1]
where:

• a and b are fractional parts of the pixel coordinates.


• k and l are the integer parts of the pixel coordinates.

2.2.3 Test Cases: Nearest-Neighborhood vs. Linear Interpolation

To compare the two methods, the following test case is defined:

Transformation Coefficients:

• Horizontal skew: a0 = 0, a1 = 1, a2 = 0.5


• Vertical skew: b0 = 0, b1 = 0.5, b2 = 1

16
Digital image & video processing

Implementation Steps:

1. Apply the nearest-neighborhood interpolation:

transformed_img_nearest = nearest_neighborhood_interpolation(img, a0 , a1 , a2 , b0 , b1 , b2 )

2. Apply the linear interpolation:

transformed_img_linear = linear_interpolation(img, a0 , a1 , a2 , b0 , b1 , b2 )

3. Display the results side by side for comparison.

2.2.4 Nearest-Neighborhood Interpolation

Nearest-neighborhood interpolation assigns the intensity value of the nearest neighboring


pixel to the new pixel position. It is computationally fast but may result in a blocky
appearance.

Formula: The nearest-neighborhood interpolation is defined as:

xnearest = round(x), ynearest = round(y)

Advantages:

• Computationally efficient.

• Simple to implement.

Disadvantages:

• May result in a blocky appearance, especially in enlarged images.

2.2.5 Linear Interpolation

Linear interpolation calculates the intensity of a new pixel as a weighted average of the
neighboring pixels. It provides smoother results compared to nearest-neighborhood interpolation.

Formula: If the pixel coordinates are (k, l) with fractional offsets a and b, the interpolated
pixel value is calculated as:

I ′ (x′ , y ′ ) = (1 − a)(1 − b) · I(x1 , y1 ) + a(1 − b) · I(x2 , y1 ) + (1 − a)b · I(x1 , y2 ) + ab · I(x2 , y2 )

Where:

• I ′ (x′ , y ′ ): The intensity value at the transformed pixel (x′ , y ′ ).

• I(x1 , y1 ), I(x2 , y1 ), I(x1 , y2 ), I(x2 , y2 ): The intensity values at the neighboring pixels in
the original image, where:

17
Digital image & video processing

– x1 , x2 : Adjacent horizontal coordinates (left and right).


– y1 , y2 : Adjacent vertical coordinates (top and bottom).

• a = x′ − x1 : The horizontal offset relative to the left neighbor.

• b = y ′ − y1 : The vertical offset relative to the top neighbor.

2.2.6 Test Cases: Nearest-Neighborhood vs. Linear Interpolation

To compare the two methods, the following test case is defined:

Transformation Coefficients:

• Horizontal skew: a0 = 0, a1 = 1, a2 = 0.5

• Vertical skew: b0 = 0, b1 = 0.5, b2 = 1

Hình 17: Comparison of Nearest-Neighborhood and Linear Interpolation. Left: Nearest-


Neighborhood. Right: Linear Interpolation.

18
Digital image & video processing

3 IMAGE SMOOTHING
Image smoothing is a technique used to reduce noise and detail in an image. The following
algorithms are commonly applied for image smoothing:

3.1 Average Filter

Idea: An average filter is defined as a kernel where all elements are equal to 1/(k 2 ), where k
is the size of the kernel. The intensity value of each pixel in the smoothed image is computed
as the average of its neighboring pixels, weighted by the kernel.

Input:

• img:.

• kernel_size: Size of the kernel filter (must be an odd number).

Output:

– smoothed_img:.

Algorithm: The algorithm proceeds as follows:

1. Define the averaging filter: Create a kernel of size k × k, with all elements equal
to 1/(k 2 ).

2. Pad the image: Add zero-padding of size k//2 to the image, ensuring the kernel can
be applied at the borders.

3. Perform convolution: For each channel of the image:

(a) Extract the region of interest centered around the current pixel.
(b) Multiply the region by the kernel and sum the resulting values.
(c) Assign the computed value to the corresponding pixel in the output image.

4. Convert the resulting image to an appropriate data type.

5. Return the smoothed image.

Test Cases:

Test Case 1: Kernel Size Variation

• Input: Original image and kernel sizes k = 3, 5, 7.

• Output: Smoothed images corresponding to each kernel size.

19
Digital image & video processing

Hình 18: Smoothed Image (Kernel Size: 3).

Hình 19: Smoothed Image (Kernel Size: 5).

Hình 20: Smoothed Image (Kernel Size: 7).

20
Digital image & video processing

Test Case 2: Image with Noise (Salt and Pepper)

• Input: Noisy image with salt and pepper noise and kernel sizes k = 3, 5, 7.

• Output: Smoothed images that reduce the noise for each kernel size.

Hình 21: Smoothed Image (Kernel Size: 3).

Hình 22: Smoothed Noisy Image (Kernel Size: 5).

Hình 23: Smoothed Noisy Image (Kernel Size: 7).

3.2 Gaussian Filter

Gaussian filtering is a widely-used technique for image smoothing, particularly for reducing
noise while preserving edges. It applies a weighted average of neighboring pixels, with weights
determined by a Gaussian function.

21
Digital image & video processing

Idea: A Gaussian filter uses a Gaussian kernel, defined by the formula:

(x − µx )2 + (y − µy )2
 
1
G(x, y) = exp − ,
2πσ 2 2σ 2

where σ is the standard deviation, and µx , µy are the mean values, typically centered in the
kernel. The kernel’s weights emphasize the central pixel and gradually decrease outward,
leading to a smoothing effect.

Input:

• img: Input image represented as a 3D array (RGB).

• kernel_size: Size of the Gaussian kernel (must be odd).

• sigma: Standard deviation of the Gaussian distribution. Default is σ = 1.0.

Output:

• smoothed_img: Smoothed image represented as a 3D array (RGB).

Algorithm: The Gaussian filter algorithm involves the following steps:

1. Generate the Gaussian kernel:

(a) Compute the kernel weights using the Gaussian formula for each position (x, y)
in the kernel.
(b) Normalize the kernel so that its total sum equals 1.

2. Pad the image: Add zero-padding of size kernel_size//2 to the input image for border
handling.

3. Convolve the kernel with the image: For each channel of the image:

(a) Extract a region of interest (ROI) centered around each pixel.


(b) Multiply the Gaussian kernel with the ROI and compute the sum of the products.
(c) Assign the resulting value to the corresponding pixel in the output image.

4. Return the smoothed image.

Test Cases:

Test Case 1: Kernel Size Variation

• Input: Original image and kernel sizes k = 3, 5, 7.

• Output: Smoothed images corresponding to each kernel size.

22
Digital image & video processing

Hình 24: Smoothed Image (Kernel Size: 3).

Hình 25: Smoothed Image (Kernel Size: 5).

Hình 26: Smoothed Image (Kernel Size: 7).

23
Digital image & video processing

Test Case 2: Image with Noise (Salt and Pepper)

• Input: Noisy image with salt and pepper noise and kernel sizes k = 3, 5, 7.

• Output: Smoothed images that reduce the noise for each kernel size.

Hình 27: Smoothed Image (Kernel Size: 3).

Hình 28: Smoothed Noisy Image (Kernel Size: 5).

Hình 29: Smoothed Noisy Image (Kernel Size: 7).

3.3 Median Filter

Median filtering is a non-linear technique for image smoothing, particularly effective for
removing salt-and-pepper noise while preserving edges.

24
Digital image & video processing

Idea: A median filter computes the median value of the pixel intensities in a neighborhood
defined by a square kernel. The median replaces the central pixel value, effectively smoothing
the image by reducing extreme variations while preserving edges.

Input:

• img: Input image represented as a 3D array (RGB).

• kernel_size: Size of the median filter kernel (must be an odd number).

Output:

• smoothed_img: Smoothed image represented as a 3D array (RGB).

Algorithm: The median filter algorithm involves the following steps:

1. Pad the image: Add zero-padding of size kernel_size//2 to the input image for border
handling.

2. Apply the median filter:

(a) For each pixel in the image, extract a region of interest (ROI) centered around
the pixel.
(b) Compute the median of the pixel intensities within the ROI for each color channel.
(c) Assign the median value to the corresponding pixel in the output image.

3. Return the smoothed image.

Test Cases:

Test Case 1: Kernel Size Variation

• Input: Original image and kernel sizes k = 3, 5, 7.

• Output: Smoothed images corresponding to each kernel size.

25
Digital image & video processing

Hình 30: Smoothed Image (Kernel Size: 3).

Hình 31: Smoothed Image (Kernel Size: 5).

Hình 32: Smoothed Image (Kernel Size: 7).

26
Digital image & video processing

Test Case 2: Image with Noise (Salt and Pepper)

• Input: Noisy image with salt and pepper noise and kernel sizes k = 3, 5, 7.

• Output: Smoothed images that reduce the noise for each kernel size.

Hình 33: Smoothed Image (Kernel Size: 3).

Hình 34: Smoothed Noisy Image (Kernel Size: 5).

Hình 35: Smoothed Noisy Image (Kernel Size: 7).

27
Digital image & video processing

4 References
1. Digital Image Processing (3rd Edition) by Rafael C. Gonzalez and Richard E.
Woods, University of Tennessee, MedData Interactive:

• Section 2.4: Image Sampling and Quantization (p. 52)


• Section 3.1.1: The Basics of Intensity Transformations and Spatial Filtering (p.
105)
• Section 3.3: Histogram Processing (p. 120)
• Section 3.5: Smoothing Spatial Filters (p. 152)
• Section 4.8: Image Smoothing Using Frequency Domain Filters (p. 269)
• Section 5.2.2: Some Important Noise Probability Density Functions (p. 314)
• Section 6.5: Color Transformations (p. 426)
• Section 6.6: Smoothing and Sharpening (p. 439)
• Section 6.8: Noise in Color Images (p. 451)

2. Lecture slides by Professor Lý Quốc Ngọc

28

You might also like