You are on page 1of 5

College of Electrical & Mechanical Engineering, NUST

Department of Computer & Software Engineering

DIGITAL IMAGE PROCESSING

LAB 3

SUBMITTED BY:

Amna Ahmed

SYNDICATE:

CE 42 A

REGISTERATION NO:

345952

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering

LAB # 02 BASIC IMAGE


PROCESSING USING PYTHON
PACKAGES
Lab Objective:
To introduce students with python programming language and its packages.

LAB TASKS:

1. For the image given below (provided with the lab handout), apply the connected
component labelling and count the total number of white objects. First threshold the images
and then perform connected component analysis algorithm.
Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
#a =np.array([[1,1,0,0,0,1],[0,0,1,1,0,0],[1,1,1,0,1,0],[0,0,1,1,0,1],
[1,1,1,0,1,0],[0,0,1,1,0,1]])
z=cv2.imread("lab3.png")
#print(a)
ret, a = cv2.threshold(z, 127, 1, cv2.THRESH_BINARY)
new_matrix=np.zeros([1120,1306])
#print(new_matrix)
label=0;
for i in range(a.shape[0]):
for j in range(a.shape[1]):
if i == 0:
if j == 0:
if a[i,j] == 0:
new_matrix[i,j] = 0
else:
label=label+1
new_matrix[i,j]=label
else:
if a[i,j]==0:
new_matrix[i,j]=0
else:
if a[i,j-1] == 1:
new_matrix[i,j]=label
else:
label = label + 1
new_matrix[i,j]=label
else:
if j == 0:
if a[i,j] == 0:
new_matrix[i,j]=0

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering
else:
if a[i-1,j]==0:
label=label+1
new_matrix[i,j]=label
else:
if a[i,j]==label:
new_matrix[i,j]=label
else:
new_matrix[i,j]=a[i,j]
else:
if a[i,j]==1:
if a[i-1,j] == 0:
if a[i,j-1]==0:
label=label+1
new_matrix[i,j]=label
else:
if a[i,j]==label:
new_matrix[i,j]=label
else:
new_matrix[i,j]=label
else:
if a[i,j-1]==0:
if a[i,j]==label:
new_matrix[i,j]=label
else:
new_matrix[i,j]=a[i,j]
else:
#if a[i,j]==label:
#new_matrix[i,j]=label
if a[i-1,j]>a[i,j-1]:
new_matrix[i,j]=a[i,j-1]
else:
new_matrix[i,j]=a[i-1,j]

else:

new_matrix[i,j]=0

print(new_matrix)

for ii in range(w):
for jj in range(h):
if img_pad[ii, jj] == 255:
threshed[ii, jj] = 1
img_pad[ii, jj] = 1
_, eq, labels = eight_connected(img_pad)
print("eq list:")
print(eq)
print("total labels:")
print(labels)
cv2.imshow('img',img)

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering
Output:
c.

b.

a.

Task 2:
Code:
def distance_map(d):
img = cv2.imshow("lab3.png")
center = (250, 250)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if d == 'euclidean':
z = np.sqrt((i - center[0]) ** 2 + (j - center[1]) ** 2)
elif d == 'cityblock':
z = abs(i - center[0]) + abs(j - center[1])
elif d == 'chessboard':
z = max(abs(i - center[0]), abs(j - center[1]))
img[i, j] = z
print("Distance is: ")
print(z)
distance_map('euclidean')

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering
def distance_map(d):
img = cv2.imshow("lab3.png")
center = (250, 250)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if d == 'euclidean':
z = np.sqrt((i - center[0]) ** 2 + (j - center[1]) ** 2)
elif d == 'cityblock':
z = abs(i - center[0]) + abs(j - center[1])
elif d == 'chessboard':
z = max(abs(i - center[0]), abs(j - center[1]))
img[i, j] = z
print("Distance is: ")
print(z)
distance_map('euclidean')

Output:

Digital Image Processing Amna Ahmed - 345952

You might also like