0% found this document useful (0 votes)
11 views11 pages

KevinCesarJacobo Exer3

Uploaded by

Jhon Loui
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)
11 views11 pages

KevinCesarJacobo Exer3

Uploaded by

Jhon Loui
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
You are on page 1/ 11

Exercise #3

Implementation of Transformations of an Image

Name: Kevin Cesar J. Jacobo


Year/Block: 3rd Year Block 1
Application/Software: Matplotlib/Python

1. Codes
import cv2
import matplotlib.pyplot as plt

I = cv2.imread('sheesh.jpg')

plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
plt.title('Original Image')

s = float(input('Enter Scaling Factor: '))

j = cv2.resize(I, None, fx=s, fy=s)

plt.subplot(2, 2, 2)
plt.imshow(cv2.cvtColor(j, cv2.COLOR_BGR2RGB))
plt.title('Scaled Image')

kangle = 60
rangle = 45

height, width = j.shape[:2]

k_rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), kangle,


1)
r_rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), rangle,
1)

K = cv2.warpAffine(j, k_rotation_matrix, (width, height))


R = cv2.warpAffine(j, r_rotation_matrix, (width, height))

plt.subplot(2, 2, 3)
plt.imshow(cv2.cvtColor(K, cv2.COLOR_BGR2RGB))
plt.title('Rotated Image 60deg')

plt.subplot(2, 2, 4)
plt.imshow(cv2.cvtColor(R, cv2.COLOR_BGR2RGB))
plt.title('Rotated Image 45deg')

plt.show()

import cv2
import matplotlib.pyplot as plt

# Load the original image


original_image = cv2.imread('sheesh.jpg')

# Resize the image using different interpolation methods


bilinear_image = cv2.resize(original_image, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_LINEAR)
nearest_image = cv2.resize(original_image, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_NEAREST)
bicubic_image = cv2.resize(original_image, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_CUBIC)

# Display the images


plt.figure(figsize=(12, 8))

plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')

plt.subplot(2, 2, 2)
plt.imshow(cv2.cvtColor(bilinear_image, cv2.COLOR_BGR2RGB))
plt.title('Bilinear Interpolation')

plt.subplot(2, 2, 3)
plt.imshow(cv2.cvtColor(nearest_image, cv2.COLOR_BGR2RGB))
plt.title('Nearest Neighbor Interpolation')

plt.subplot(2, 2, 4)
plt.imshow(cv2.cvtColor(bicubic_image, cv2.COLOR_BGR2RGB))
plt.title('Bicubic Interpolation')

plt.show()

from PIL import Image, ImageOps


import numpy as np
import matplotlib.pyplot as plt

# Read the original image


original_image = Image.open('sheesh.jpg')

# 1. Intensity Transformations
# Linear Scaling
a = 1.5
b = 30
linear_scaled_image = np.array(original_image) * a + b
linear_scaled_image = Image.fromarray(np.uint8(linear_scaled_image))
plt.figure(figsize=(10, 10))
plt.subplot(3, 4, 1)
plt.imshow(linear_scaled_image)
plt.title('Linear Scaled Image')

# Logarithmic Transformation
c = 50
log_transformed_image = c * np.log(1 + np.array(original_image))
log_transformed_image = Image.fromarray(np.uint8(log_transformed_image))
plt.subplot(3, 4, 2)
plt.imshow(log_transformed_image)
plt.title('Log Transformed Image')

# Power-law Transformation
gamma = 0.5
power_law_transformed_image = c * (np.array(original_image) ** gamma)
power_law_transformed_image =
Image.fromarray(np.uint8(power_law_transformed_image))
plt.subplot(3, 4, 3)
plt.imshow(power_law_transformed_image)
plt.title('Power-law Transformed Image')
# 2. Spatial Transformations
# Translation
tx = 50
ty = 30
translated_image = original_image.transform(original_image.size,
Image.AFFINE, (1, 0, tx, 0, 1, ty))
plt.subplot(3, 4, 4)
plt.imshow(translated_image)
plt.title('Translated Image')

# Rotation
rotated_image = original_image.rotate(-45, expand=True)
plt.subplot(3, 4, 5)
plt.imshow(rotated_image)
plt.title('Rotated Image 45deg')

# Scaling
scale_factor = 2
scaled_image = original_image.resize((int(original_image.width *
scale_factor), int(original_image.height * scale_factor)))
plt.subplot(3, 4, 6)
plt.imshow(scaled_image)
plt.title('Scaled Image')

# 3. Geometric Transformations
# Affine Transformation
a = 0.5
b = 0.3
c = -0.2
d = 0.8
tx = 50
ty = -20
affine_matrix = (a, b, tx, c, d, ty)
affine_transformed_image = original_image.transform(original_image.size,
Image.AFFINE, affine_matrix)
plt.subplot(3, 4, 7)
plt.imshow(affine_transformed_image)
plt.title('Affine Transformed Image')

# Projective Transformation (Homography)


H = np.array([[1, 0.1, 0], [0.1, 1, 0], [0.001, 0.002, 1]])
projective_transformed_image =
original_image.transform(original_image.size, Image.PERSPECTIVE,
H.flatten(), Image.BICUBIC)
plt.subplot(3, 4, 8)
plt.imshow(projective_transformed_image)
plt.title('Projective Transformed Image')

# 4. Color Transformations
# Grayscale Conversion
grayscale_image = original_image.convert('L')
plt.subplot(3, 4, 9)
plt.imshow(grayscale_image, cmap='gray')
plt.title('Grayscale Image')

# Color Space Conversion


hsv_image = original_image.convert('HSV')
plt.subplot(3, 4, 10)
plt.imshow(hsv_image)
plt.title('HSV Image')

# 5. Frequency Domain Transformations


# Fourier Transform
fourier_transform = np.fft.fft2(np.array(original_image))
plt.subplot(3, 4, 11)
plt.imshow(np.log(1 + np.abs(np.fft.fftshift(fourier_transform))),
cmap='gray')
plt.title('Fourier Transform')

# 6. Histogram Transformations
# Histogram Equalization
equalized_image = ImageOps.equalize(original_image)
plt.subplot(3, 4, 12)
plt.imshow(equalized_image)
plt.title('Histogram Equalized Image')

#Display original Image


plt.figure()
plt.imshow(original_image)
plt.title('Original Image')
plt.tight_layout()
plt.show()

2. Output
3. Answer the following questions:
A. Intensity Transformations: - Explain the importance of intensity transformations in
image processing. How might linear scaling and logarithmic transformations be used to
enhance features such as terrain or vegetation?

Intensity transformations are essential in image processing as they enable the


adjustment of pixel intensity values, enhancing specific features or characteristics
within an image. For instance, linear scaling is commonly used to adjust brightness and
contrast, improving the visibility of details and overall image appearance. Additionally,
logarithmic transformations are valuable for enhancing low-contrast regions, such as
terrain or vegetation, by amplifying subtle intensity variations and bringing out finer
details, which is particularly useful in satellite imagery analysis and environmental
monitoring.

B. Spatial Transformations: - Discuss the significance of spatial transformations in image


analysis. How could translation, rotation, and scaling be applied to align images from
different passes or correct for geometric distortions?

Spatial transformations are important in image analysis as they allow for adjusting the
position, orientation, and size of images to align them or correct for distortions.
Translation, rotation, and scaling can be applied to align images from different passes in
satellite imaging or correct for geometric distortions caused by camera lenses.

C. Geometric Transformations: - Elaborate on the role of geometric transformations,


especially affine transformations and projective transformations (homography), in image
processing. Provide examples of real-world scenarios where these transformations are
crucial for accurate analysis.

Geometric transformations, like affine and projective transformations, play a crucial role
in image processing by allowing for more complex transformations of images. For
example, affine transformations can correct for perspective distortions in aerial images,
while projective transformations (homography) are essential for tasks like panorama
stitching or augmented reality.

D. Color Transformations: - In the context of satellite imagery, explain why color


transformations might be relevant. Discuss scenarios where converting images to
grayscale or transitioning between different color spaces (e.g., RGB to HSV) could aid in
better interpretation and analysis.
In satellite imagery, color transformations are relevant for various analysis tasks.
Converting images to grayscale can simplify analysis by removing color variations, while
transitioning between different color spaces like RGB to HSV can help in identifying
specific features such as vegetation or water bodies more accurately.

E. Frequency Domain Transformations: - Explore the potential applications of Fourier


Transform in image processing. How might analyzing images in the frequency domain
help identify patterns, anomalies, or specific environmental features?

Fourier Transform is a powerful tool in image processing that allows for analyzing
images in the frequency domain, helping to identify patterns, anomalies, or
environmental features that may not be easily discernible in the spatial domain. This
technique finds applications in tasks like image compression, image filtering, and
pattern recognition.

F. Histogram Transformations: - Describe how histogram equalization could be beneficial


in enhancing contrast in images. Discuss any potential challenges or limitations when
applying this technique to large datasets.

Histogram equalization is beneficial for enhancing contrast in images by redistributing


pixel intensities. However, it may face challenges when applied to large datasets due to
increased computational complexity and potential over-amplification of noise, which
can lead to undesirable artifacts in the enhanced images.

You might also like