import cv2
import numpy as np
from pylab import imshow
import matplotlib.pyplot as plt
def preprocess(img_path):
img = cv2.imread(img_path)
imshow(img)
plt.figure()
# To Grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(img.shape)
threshold = 120
transform = np.vectorize(lambda x: 0 if x > threshold else 255 - x)
img = transform(img)
img = img[2 * len(img) // 5:len(img) // 2 - 200, 2 * len(img) // 5:len(img) // 2 - 200]
imshow(img)
return img
# imshow(img)
img = preprocess("/Users/ginoprasad/Downloads/IMG_3948.JPG 2022-10-02 04:37PM")
(4032, 3024)
def find(uptree, location):
stack = [location]
while type(uptree[stack[-1][0]][stack[-1][1]]) != int:
stack.append(uptree[stack[-1][0]][stack[-1][1]])
for location in stack[:-1]:
uptree[location[0]][location[1]] = stack[-1]
return stack[-1], uptree[stack[-1][0]][stack[-1][1]]
def union(uptree, location1, location2):
parent1, size1 = find(uptree, location1)
parent2, size2 = find(uptree, location2)
if parent1 == parent2:
return
elif size1 < size2:
uptree[parent1[0]][parent1[1]] = parent2
uptree[parent2[0]][parent2[1]] += size1
else:
uptree[parent2[0]][parent2[1]] = parent1
uptree[parent1[0]][parent1[1]] += size2
def segment_image(img):
plt.figure()
uptree = [[1 for _ in range(img.shape[1])] for _ in range(img.shape[0])]
for row_index in range(img.shape[0]):
for col_index in range(img.shape[1]):
val = img[row_index][col_index]
if val:
for neighbor_row, neighbor_col in ((row_index+1, col_index), (row_index, col_index+1), (row_index-1, col_index), (row_index, col_index-1)):
if 0 <= neighbor_row < img.shape[0] and 0 <= neighbor_col < img.shape[1]:
neighbor_val = img[neighbor_row][neighbor_col]
if neighbor_val:
union(uptree, (row_index, col_index), (neighbor_row, neighbor_col))
find(uptree, (row_index, col_index))
find(uptree, (neighbor_row, neighbor_col))
count = 0
colors = {}
coor = {}
pixel_count = {}
for row_index, row in enumerate(uptree):
for col_index, val in enumerate(row):
if type(val) != tuple:
val = (row_index, col_index)
if val not in colors and img[row_index][col_index]:
count += 1
coor[count] = (row_index, col_index, row_index, col_index)
uptree[row_index][col_index] = colors[(row_index, col_index)] = count
pixel_count[count] = 1
elif img[row_index][col_index]:
color = colors[val]
coor[color] = (min(row_index, coor[color][0]), min(col_index, coor[color][1]), max(row_index, coor[color][2]), max(col_index, coor[color][3]))
uptree[row_index][col_index] = colors[val]
pixel_count[colors[val]] += 1
else:
uptree[row_index][col_index] = 0
print(f"{count} Connected Components")
min_pixels = 100
valid_colors = set([color for color in pixel_count if pixel_count[color] >= min_pixels])
filter_transform = np.vectorize(lambda x: x in valid_colors)
uptree = filter_transform(uptree)
imshow(uptree)
min_pixels = 100
for color in pixel_count:
if pixel_count[color] > min_pixels:
plt.figure()
imshow(uptree[coor[color][0]:coor[color][2],coor[color][1]:coor[color][3]])
segment_image(img)
75 Connected Components