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

DIGITAL IMAGE PROCESSING

PRACTICAL

NAME – AMAN KUMAR SINGH


ENROLLMENT NO. – A45605220003
COURSE – B.TECH (CSE)
SEMESTER- 6TH
BATCH- 2020-2024
GUIDED BY – PROF. DEVENDRA PRASAD
Experiment 01:
Introduction to Python Programming / Introduction to python environment

# list
l = [2,3,4,5]
print(l[0], l[-1])
l.pop()
l.append(100)
print(l)
print(l[1:])
print(l[:3])
# tuple
t1 = (1,2)
t2 = (3,4)
print(t1, t2, t1+t2)
# dictionary
d = {1: 'apple', 2: 'banana', 4: 'papaya'}
print(d)
d[3] = 'kiwi'
print(d)
# loop
for i in range(5):
print(i)
# conditional
if 3 > 4:
print('3 is greater than 4')
else:
print('3 is not greater than 4')
#numpy
import numpy as np
a = np.array([1,2,3])
print(a)
print(a[1:])
print(a[:2])
b = np.array([2,3,4])
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(3*a)
c = np.zeros((3,4))
print(c.shape)
print(c)
d = np.random.random((2,3))
print(d)
e = d.reshape(-1)
print(e)
#pandas
import pandas as pd
df = pd.read_csv(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\
shera.jpg')
print(df)
print(df[1:])
print(df.iloc[[0],[1]])
print(df.loc[[0,1], ['Name']])
Experiment 02:
Vectors, Matrices operations using Python Command Window, Writing Python
Scripts /basic operations on image.

import cv2
img=cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg')
gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print('shape',img.shape)
print('size:',img.size)
print('dtype:',img.dtype)
print('itemsize:',img.itemsize)
print('max:',img.max())
print('min:',img.min())
print('mean:',img.mean())
Experiment 03:
Perform various point processing operations like negative, log and power law
transformation on an image

import cv2
import numpy as np
import math
img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\
shera.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

L = 256
row, col = gray.shape

negative_img = np.zeros((row, col))


negative_img_log = np.zeros((row, col))
negative_img_power = np.zeros((row, col))
c = 0.1
y=1

for i in range(row):
for j in range(col):
negative_img[i][j] = L - 1 - gray[i][j]
negative_img_log[i][j] = c*math.log(1+gray[i][j])
negative_img_power[i][j] = c * gray[i][j]**y
cv2.imshow('gray', gray)
cv2.imshow('negative_img', negative_img)
cv2.imshow('negative_img_log', negative_img_log)
cv2.imshow('negative_img_power', negative_img_power)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment 04:
Read an Image and display its histogram.

import cv2
img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
import matplotlib.pyplot as plt
cv2.imshow('gray', gray)
fig, ax = plt.subplots(figsize = (10, 7))
ax.set_title('Histogram')
ax.hist(gray.ravel(), 256, [0,256])
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment 05:
To display a histogram of an Image and to perform histogram equalization on an
Image.

import cv2
img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
equ = cv2.equalizeHist(gray)

import matplotlib.pyplot as plt


cv2.imshow('gray', gray)
cv2.imshow('equ', equ)
fig, ax = plt.subplots(figsize=(5,2))
# ax.hist(gray.ravel(), 256, [0,256])
ax.hist(gray.ravel(), 256, [0,256])

fig2, ax2 = plt.subplots(figsize=(5,2))


ax2.hist(equ.ravel(), 256, [0,256])
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment 06:
Enhance an Image and remove noise and other artifacts from an Image

import cv2
img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
row, col = gray.shape
img_resized = cv2.resize(gray,(col//2, row//2),interpolation=cv2.INTER_AREA)
row, col = img_resized.shape
M = cv2.getRotationMatrix2D(((col-1)/2.0, (row-1)/2.0), 90, -1)
img_rotated = cv2.warpAffine(gray, M, (col, row))

cv2.imshow('gray', gray)
cv2.imshow('img_resized', img_resized)
cv2.imshow('img_rotated', img_rotated)

import numpy as np
kernel = np.ones((5,5), np.float32)/25 # 5x5 kernel for mean filter
mean_img = cv2.filter2D(img, -1, kernel)
_, binary_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # binary threshold
cv2.imshow('Mean Image', mean_img)
cv2.imshow('Binary Image', binary_img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment 07:
Write a Program to implement various edge detection operators.
#sobel
import cv2
import numpy as np
img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 2, 0, ksize=5)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 2, ksize=5)
cv2.imshow('img',img)
cv2.imshow('gray',gray)
cv2.imshow('sobelx',sobelx)
cv2.imshow('sobely',sobely)

#prewitt
img_gaussian = cv2.GaussianBlur(gray, (3,3), 0)
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)
cv2.imshow('prewittx', img_prewittx)
cv2.imshow('prewitty', img_prewitty)
#canny
canny = cv2.Canny(img, 100, 200)
cv2.imshow('canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment 08:
Perform texture analysis using GLCM in python.
import cv2

import numpy as np
from skimage.feature import greycomatrix, greycoprops

img = cv2.imread(r'C:\Users\Aman Singh Rajput\OneDrive\Pictures\Camera Roll\shera.jpg', 0)

# calculate GLCM properties


glcm = greycomatrix(img, [2], [0], 256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')
dissimilarity = greycoprops(glcm, 'dissimilarity')
homogeneity = greycoprops(glcm, 'homogeneity')
energy = greycoprops(glcm, 'energy')
correlation = greycoprops(glcm, 'correlation')
print('contrast:', contrast[0][0])
print('dissimilarity:', dissimilarity[0][0])
print('homogeneity:', homogeneity[0][0])
print('energy:', energy[0][0])
print('correlation:', correlation[0][0])

You might also like