Name: Dipanjan Mandal
UID: TNU2018020100016
Stream: B.tech in Computer Science Engineering with Specialization in Cyber Security, 4 th Year, 7th
Semester
Batch:2018-2022
Subject: Image Processing Lab Report
Experiment 2: Implement different types of image enhancement/transformation likes Image negative, log
transformation, power law transformation, intensity level slicing etc. in any programming language.
Input (Original image) used:
# load dependencies
import cv2
import matplotlib.pyplot as plt
import numpy as np
import math
#read original image
img = cv2.imread(‘D:\\image_processing\\Sample Images\\ nature.jpg.jpg)
Image negative:
Code:
# negative transformation of the image
img_neg = 1 - img
# result of negtaive transformation
cv2.imwrite(‘D:\\image_processing\\Sample Images\\img_neg.jpg', img_neg)
cv2.imshow("Output",img_neg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Image for Negative Transformation:
Log transformation:
Code:
#log transformation of the image
#applying lof transformation method
c = 255 / np.log(1 + np.max(img))
log_img = c * (np.log(img + 1))
log_img = np.array(log_img, dtype = np.uint8)
#result for log transformation
cv2.imwrite(‘D:\\image_processing\\Sample Images\\log_img.jpg', log_img)
cv2.imshow("Output",log_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Image for Log Transformation:
Power law transformation:
Code:
#power law transformation ( gamma trandformation)
#trying 3 gamma values
for gamma in [0.5, 1.2, 2.2, 3.2]:
#gamma correction
gamma_img = np.array(255 * (img / 255) ** gamma, dtype = 'uint8')
#save edited images
cv2.imwrite(‘D:\\image_processing\\Sample Images\\gamma_img'+str(gamma)+'.jpg', gamma_img)
cv2.imshow("Output",gamma_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output image for Power law Transformation:
Gamma = 0.5
Gamma = 1.2
Gamma = 2.2
Gamma = 3.2
Intensity level slicing:
Code
# image convert into gray scale
img = cv2.imread(‘D:\\image_processing\\Sample Images\\ nature.jpg,0)
# Save the grayscale original image
cv2.imwrite(‘D:\\image_processing\\Sample Images\\img.jpg', img)
# Display the image
cv2.imshow('original image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Find width and height of image
row, column = img.shape
# Create an zeros array to store the sliced image
img2 = np.zeros((row,column),dtype = 'uint8')
# Specify the min and max range
min_range = 10
max_range = 60
# Loop over the input image and if pixel value lies in desired range set it to 255 otherwise set it to 0.
for i in range(row):
for j in range(column):
if img[i,j]>min_range and img[i,j]<max_range:
img2[i,j] = 255
else:
img2[i,j] = 0
# Display the image
cv2.imshow('sliced image', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Save the sliced image
cv2.imwrite(‘D:\\image_processing\\Sample Images\\img2.jpg', img2)
Output image for Intensity Level Slicing