You are on page 1of 5

Experiment 5

Name : Aryan Singh


Enrolment No. : 0901AM211015
Branch : AIML VI
Batch : A

Write python program to perform point operations on images.


In [ ]: import cv2
import numpy as np
from PIL import Image

IMAGE_SHAPE = (350,450)

Original Image

In [ ]: Oimg = Image.open("image6.jpg").resize(IMAGE_SHAPE)
Oimg

Out[ ]:

In [ ]: # fuction to calculate the Level of image


def calculate_level(image):
max_val = np.max(image)
n = np.ceil(np.log2(max_val))
L = 2**n
return int(L)

1. Digital Negation: s= L-1-r


The negative of an image with intensity levels in the range [0, L-1] is obtained by using the
negative transformation.
Negative images are useful for enhancing white or grey detail embedded in dark regions of an
image

In [ ]: # perform color negative transformation


img = np.array(Oimg)
L = calculate_level(img)
I = L - 1
img = I - img
display(Image.fromarray(img))

In [ ]: # perform grayscale negative transformation


img = np.array(Oimg)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = I - img

display(Image.fromarray(img))
2. log transformation: The general form of the log transformation is 's = c * log(1 + r)'
•The log transformation maps a narrow range of low input grey level values into a wider range of
output values
•The inverse log transformation performs the opposite transformation

In [ ]: img = np.array(Oimg)
grey_img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

# Set the constant for scaling


c = 100

# Perform logarithmic transformation using formula


log_transformed_img = c * np.log1p(grey_img)
log_transformed_img = np.uint8(log_transformed_img)

# Display the original and log transformed images


display("Original",Oimg)
display("log transformed image",Image.fromarray(log_transformed_img))

'Original'
'log transformed image'

3. Power law transformation: Power law transformations have the following form s = c * r γ
Map a narrow range of dark input values into a wider range of output values or vice versa
Varying γ gives a whole family of curves

In [ ]: img = np.array(Oimg)
grey_img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

# Set the constants for scaling and gamma


c = 100
gamma = 0.6 # Adjust gamma as needed

# Perform power-law transformation using formula


power_law_transformed_img = c * (img ** gamma)
power_law_transformed_img = np.uint8(power_law_transformed_img)

# Display the original and power-law transformed images


display("Original",Oimg)
display("power law transformed img",Image.fromarray(power_law_transformed_img))

'Original'

'power law transformed img'

You might also like