You are on page 1of 2

import cv2

import numpy as np
import matplotlib.pyplot as plt
m= cv2.imread("lab3.png")
#print(m)
#plt.imshow(m)
#plt.show()
ret, thresh = cv2.threshold(m, 127, 1, cv2.THRESH_BINARY)
#print(thresh)
#plt.imshow(thresh)
#plt.show()
#x=np.ndarray(thresh)
label=[]
a=0
for rows in range(thresh.shape[0]):
for col in range(thresh.shape[1]):
#print(thresh[rows,col])
if rows ==0:
if col == 0:
label.append(thresh[rows,col]
else:
if thresh(col) == thresh(col - 1):
label.append(thresh[rows,col])
elif thresh(col) != thresh (col-1):
print("z")

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]])
#print(a)
new_matrix=np.zeros([6,6])
#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:
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]=a[i,j]
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]=label

print(new_matrix)

You might also like