0% found this document useful (0 votes)
18 views

Lab 9 Dip Report

This document describes three tasks related to morphological operations on digital images. Task 1 performs image erosion on an input image using a structuring element to detect bright regions. Task 2 performs top-hat transformation to extract small bright details. Task 3 repeats Task 2 and saves the output.

Uploaded by

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

Lab 9 Dip Report

This document describes three tasks related to morphological operations on digital images. Task 1 performs image erosion on an input image using a structuring element to detect bright regions. Task 2 performs top-hat transformation to extract small bright details. Task 3 repeats Task 2 and saves the output.

Uploaded by

Yaqoob Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Digital Image Processing

LAB 9 MORPHOLOGICAL OPERATIONS

YAQOOB JAMAL

AITAZAZ QAISER

USMAN KHALID

MUHAMMAD SIDDIQU

Task 1
import numpy as np
import cv2
image = cv2.imread("fig1.jpg", 0)
X2, Y2 = np.shape(image)
def normalize(img):
x, y = np.shape(img)
min = np.amin(img)
max = np.amax(img)
for i in range(x):
for j in range(y):
img[i, j] = 255 * (img[i, j] - min) / (max - min)

for x in range(X2):
for y in range(Y2):
if image[x, y] == 255:
image[x, y] = 1

n = 17
n2 = np.int64(n / 2)
se = np.int64(np.ones((n, n)))
x = 1
y = 1
m = np.int64(np.zeros((X2, Y2)))
for x in range(n2, X2 - n2):
for y in range(n2, Y2 - n2):
if (np.sum(se)) == (np.sum(np.multiply(image[x - n2:x + n2 + 1, y -
n2:y + n2 + 1], se))):
m[x, y] = 1
normalize(m)
m = np.uint8(m)
cv2.imwrite("result.jpg", m)
Task 2
import numpy as np
import cv2
myy = cv2.imread("Capturefig3.jpg", 0)
X2, Y2 = np.shape(myy)

for x in range(X2):
for y in range(Y2):
if myy[x, y] == 255:
myy[x, y] = 1

n = 3
n2 = np.int64(n / 2)
se = np.int64(np.ones((n, n)))

m = np.int64(np.zeros((X2, Y2)))

m1 = np.int64(np.zeros((X2, Y2)))
for x in range(n2, X2 - n2):
for y in range(n2, Y2 - n2):
m1[x, y] = np.max(myy[x - n2:x + n2 + 1, y - n2:y + n2 + 1])
m[x, y] = np.min(myy[x - n2:x + n2 + 1, y - n2:y + n2 + 1])

for x in range(X2):
for y in range(Y2):
m[x, y] = m1[x, y] - m[x, y]

min1 = np.amin(m)
max1 = np.amax(m)
for i in range (X2):
for j in range (Y2):
m[i][j]= ((m[i][j]-min1)/(max1-min1))*255
m = np.uint8(m)
cv2.imwrite("result3.jpg", m)

Task 3
import numpy as np
import cv2
myy = cv2.imread("Capturefig3.jpg", 0)
X2, Y2 = np.shape(myy)

for x in range(X2):
for y in range(Y2):
if myy[x, y] == 255:
myy[x, y] = 1

n = 3
n2 = np.int64(n / 2)
se = np.int64(np.ones((n, n)))

m = np.int64(np.zeros((X2, Y2)))

m1 = np.int64(np.zeros((X2, Y2)))
for x in range(n2, X2 - n2):
for y in range(n2, Y2 - n2):
m1[x, y] = np.max(myy[x - n2:x + n2 + 1, y - n2:y + n2 + 1])
m[x, y] = np.min(myy[x - n2:x + n2 + 1, y - n2:y + n2 + 1])

for x in range(X2):
for y in range(Y2):
m[x, y] = m1[x, y] - m[x, y]

min1 = np.amin(m)
max1 = np.amax(m)
for i in range (X2):
for j in range (Y2):
m[i][j]= ((m[i][j]-min1)/(max1-min1))*255
m = np.uint8(m)
cv2.imwrite("result3.jpg", m)

You might also like