You are on page 1of 10

DEPARTMENT OF COMPUTER & SOFTWARE

ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

EC312 Digital Image Processing


Lab Report - 04

SUBMITTED TO:
LE Ma’am Sundas Ashraf

SUBMITTED BY:
Uzair Waheed
Reg # 371828
DE- 43 CE

Submission Date:10/03/24

1|Page
Lab-04: Transformation Operation
Lab Tasks

Task-1:
Apply following transformation techniques on image provided and observe the
output image of each transformation.

I. Negative transformation
II. Log transformation
Code:

import cv2
import numpy as np

img = cv2.imread("input_img.png", cv2.IMREAD_GRAYSCALE)


# negative transformation
neg_trans = 255 - img

# logarithmic transformation
max_pixel = np.max(img)
c = 255 / np.log(1 + max_pixel)
log_trans = c * np.log(img + 1)
log_trans = log_trans.astype(np.uint8)

cv2.imshow("Input Image", img)


cv2.imshow("Negative Transformation ", neg_trans)
cv2.imshow("Logarithmic Transformation ", log_trans)
cv2.waitKey(0)

2|Page
Output:

Task-2:

Code:

import cv2
import numpy as np

img = cv2.imread("input_img.png", 0)
mean_ = np.mean(img)
rows, cols= img.shape

# a)
img_a = np.zeros([rows, cols], dtype=np.uint8)
for x in range(rows):
for y in range(cols):
if img[x][y] <= mean_:
img_a[x][y] = 0

3|Page
else:
img_a[x][y] = 255

# b)
img_b = np.zeros([rows, cols], dtype=np.uint8)
for x in range(rows):
for y in range(cols):
if img[x][y] <= mean_:
img_b[x][y] = 255
else:
img_b[x][y] = 0

# c)
img_c = np.zeros([rows, cols], dtype=np.uint8)
for x in range(rows):
for y in range(cols):
if mean_ - 20 <= img[x][y] <= mean_ + 20:
img_c[x][y] = 0
else:
img_c[x][y] = 255

cv2.imshow("Input Image", img)


cv2.imshow("a)", img_a)
cv2.imshow("b)", img_b)
cv2.imshow("c)", img_c)
cv2.waitKey(0)

Output:

4|Page
Task-3:
Apply Power Law transformation for the following values of γ (0.2, 0.5, 1.2 and
1.8). Make sure to adjust data types accordingly

Code:

import cv2
import numpy as np

img = cv2.imread("input_img.png", 0)

def powerLaw(img, gamma):


gamma_img = 255 * (np.power((img / 255), gamma))
gamma_img = gamma_img.astype(np.uint8)
return gamma_img

cv2.imshow("Real Image", img)

new_img = powerLaw(img, 0.2)


cv2.imshow("Gamma = 0.2", new_img)

new_img = powerLaw(img, 0.5)


cv2.imshow("Gamma = 0.5", new_img)

new_img = powerLaw(img, 1.2)


cv2.imshow("Gamma = 1.2", new_img)

5|Page
new_img = powerLaw(img, 1.8)
cv2.imshow("Gamma = 1.8", new_img)

cv2.waitKey(0)

Output:

6|Page
Task-4:
Apply Gray level slicing using lower limit 100 and upper limit 200. Set all these
values to 210.
Code:

import cv2
import numpy as np

img = cv2.imread("input_img.png", 0)

def grayLvlSlicing(img):
rows, cols = img.shape
img_ = np.zeros([rows, cols], dtype=np.uint8)
for x in range(rows):
for y in range(cols):
if 100 <= img[x][y] <= 200:
img_[x][y] = 210
else:
img_[x][y] = img[x][y]
return img_

cv2.imshow("Input Image ", img)


new_img = grayLvlSlicing(img)
cv2.imshow("Img after Gray slicing", new_img)
cv2.waitKey(0)

7|Page
Output:

Task-5:
Calculate histogram of gray scale mage.(Don’t use build in function) .Make
your own algorithm.
Code:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("input_img.png", 0)

def histogram(img):
rows, cols = img.shape
hist = np.zeros(256)
for x in range(rows):
for y in range(cols):
hist[img[x, y]] += 1
return hist

hist = histogram(img)

8|Page
plt.figure()
plt.bar(range(256), hist, color='lightblue')
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Output:

Task-6:
Enhance given image by using local histogram statistics.
E=4 .0, K0=0.4, K1=0.02, K2=0.4

9|Page
Code:

import cv2 as cv
import numpy as np

img = cv.imread('tungsten_original.tif', 0)
cv.imshow('image', img)
# calculate the global mean of imag
Gm = np.mean(img)
# calculate the stadard deviation of image
G_std = np.std(img)
E = 4.0
K0 = 0.4
K1 = 0.02
K2 = 0.4

# calculate the local mean and lcal standard deviation of of pixle value
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i - 1 >= 0 and j - 1 >= 0 and i + 1 < img.shape[0] and j + 1 <
img.shape[1]:
Lm = np.mean(img[i - 1:i + 1, j - 1:j + 1])
L_std = np.std(img[i - 1:i + 1, j - 1:j + 1])
if Lm <= K0 * Gm and K2 * G_std >= L_std >= K1 * G_std:
img[i][j] = E * img[i][j]

cv.imshow('local enhancement', img)


cv.waitKey(0)

Output:

10 | P a g e

You might also like