Basic opencv
Basic opencv
PRACTICE 1
BASIC - OPENCV
22TGMT
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
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:
• Output:
• Detailed Steps:
img_2d = np.uint8(img_2d)
3. Clip the pixel values to ensure they stay within the valid range [0, 255]:
bright_img = Image.fromarray(np.uint8(img_2d))
2
Digital image & video processing
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:
• 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.
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:
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.
4
Digital image & video processing
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:
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.
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
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.
7
Digital image & video processing
Input:
• For RGB images, the histogram equalization is applied separately to each color channel
(red, green, blue).
Output:
• For RGB images, the output is a color image with contrast adjustment applied to each
channel.
Algorithm:
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.
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.
Hình 6: Grayscale Image with Low Contrast Before and After Histogram Equalization.
8
Digital image & video processing
Hình 7: Grayscale Image with High Contrast Before and After Histogram Equalization.
• 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).
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:
• 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.
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
Hình 10: Low-Contrast vs High-Contrast Grayscale Images Before and After Histogram
Specification.
Hình 11: Grayscale Image Before and After Histogram Specification (One vs Multiple
Reference Histograms).
2 GEOMETRY 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:
• a0, a1, a2, a3: Coefficients for the transformation of the x-coordinate.
• b0, b1, b2, b3: Coefficients for the transformation of the y-coordinate.
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.
12
Digital image & video processing
• 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.
• 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
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:
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.
• 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.
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.
15
Digital image & video processing
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:
Transformation Coefficients:
16
Digital image & video processing
Implementation Steps:
transformed_img_nearest = nearest_neighborhood_interpolation(img, a0 , a1 , a2 , b0 , b1 , b2 )
transformed_img_linear = linear_interpolation(img, a0 , a1 , a2 , b0 , b1 , b2 )
Advantages:
• Computationally efficient.
• Simple to implement.
Disadvantages:
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:
Where:
• 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
Transformation Coefficients:
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:
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:.
Output:
– smoothed_img:.
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.
(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.
Test Cases:
19
Digital image & video processing
20
Digital image & video processing
• 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.
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
(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:
Output:
(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:
Test Cases:
22
Digital image & video processing
23
Digital image & video processing
• 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.
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:
Output:
1. Pad the image: Add zero-padding of size kernel_size//2 to the input image for border
handling.
(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.
Test Cases:
25
Digital image & video processing
26
Digital image & video processing
• 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.
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:
28