0% found this document useful (0 votes)
25 views2 pages

P4-Log - Power, Contrast, Histo, Threshol, Halftoning

Uploaded by

4832550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

P4-Log - Power, Contrast, Histo, Threshol, Halftoning

Uploaded by

4832550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy