Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#AIM pr4

# Write program to implement point/pixel intensity transformations such as


# 1. Log and Power-law transformations
# 2. Contrast adjustments
# 3. Histogram equalization
# 4. Thresholding, and halftoning operations
import numpy as np
import cv2
from matplotlib import pyplot as plt

def log_transformation(image):
epsilon = 1e-10 # Small value to avoid division by zero
c = 255 / np.log(1 + np.max(image))
image_log = np.log(1 + image + epsilon)
log_transformed = c * image_log
log_transformed = np.array(log_transformed, dtype=np.uint8)
return log_transformed

def power_law_transformation(image, gamma):


power_transformed = np.power(image, gamma)
power_transformed = np.array(power_transformed, dtype=np.uint8)
return power_transformed

def contrast_adjustment(image, alpha, beta):


adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
return adjusted

def histogram_equalization(image):
equalized = cv2.equalizeHist(image)
return equalized

def thresholding(image, threshold):


_, thresholded = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
return thresholded

def halftoning(image):
_, binary = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)
return binary

# Read an image
image_path = r'C:\Users\Lenovo\Downloads\flower.jpeg'
image = cv2.imread(image_path, 0) # Read as grayscale
if image is None:
print("Error: Unable to read the image file.")
exit() # Exit the program or handle the error appropriately

# Apply transformations
log_transformed = log_transformation(image)
power_transformed = power_law_transformation(image, gamma=0.5) # Example gamma
value
adjusted_contrast = contrast_adjustment(image, alpha=1.5, beta=0) # Example alpha
and beta values
equalized = histogram_equalization(image)
thresholded = thresholding(image, threshold=128) # Example threshold value
halftoned = halftoning(image)

# Save transformed images to files


cv2.imwrite('log_transformed.jpg', log_transformed)
cv2.imwrite('power_transformed.jpg', power_transformed)
cv2.imwrite('adjusted_contrast.jpg', adjusted_contrast)
cv2.imwrite('equalized.jpg', equalized)
cv2.imwrite('thresholded.jpg', thresholded)
cv2.imwrite('halftoned.jpg', halftoned)

# Define transformations dictionary


transformations = {
'Log Transformed': 'log_transformed.jpg',
'Power Law Transformed': 'power_transformed.jpg',
'Contrast Adjusted': 'adjusted_contrast.jpg',
'Histogram Equalized': 'equalized.jpg',
'Thresholded': 'thresholded.jpg',
'Halftoned': 'halftoned.jpg'
}

# Display original image and paths to the transformed images


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

# Calculate number of rows and columns based on the number of transformations


num_transformations = len(transformations)
num_rows = (num_transformations + 2) // 3 # Add 2 to round up to the nearest
multiple of 3
num_cols = 3

for i, (transformation, path) in enumerate(transformations.items(), start=1):


plt.subplot(num_rows, num_cols, i) # Adjust index to start from 1
transformed_image = cv2.imread(path, 0)
plt.imshow(transformed_image, cmap='gray')
plt.title(transformation)

plt.tight_layout()
plt.show()

You might also like