Professional Documents
Culture Documents
Lab Report 3 DIP
Lab Report 3 DIP
LAB 3
SUBMITTED BY:
Amna Ahmed
SYNDICATE:
CE 42 A
REGISTERATION NO:
345952
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
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)
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')
Output: