Skip to content
Snippets Groups Projects
Commit 51843207 authored by Marc Feger's avatar Marc Feger
Browse files

Add A1

parent d2f5ca43
No related branches found
No related tags found
No related merge requests found
A1/A1.pdf 0 → 100644
File added
A2/images/astronauts/Eileen-Collins.jpg

46.4 KiB

# Program to count islands in boolean 2D matrix
import copy
import string
import numpy as np
class Graph:
def __init__(self, row, col, g):
self.ROW = row
self.COL = col
self.graph = g
self.islands = [[None for _ in range(col)] for _ in range(row)]
def isSafe(self, i, j, visited, g):
return 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and g[i][j]
def DFS(self, i, j, visited, g):
rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]
colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]
visited[i][j] = True
for k in range(8):
if self.isSafe(i + rowNbr[k], j + colNbr[k], visited, g):
self.DFS(i + rowNbr[k], j + colNbr[k], visited, g)
def countIslands(self):
visited = [[False for _ in range(self.COL)] for _ in range(self.ROW)]
count = 0
for numb in [1, 2, 3]:
g = self.highlight(numb, self.graph, self.ROW, self.COL)
print(np.array(g))
for i in range(self.ROW):
for j in range(self.COL):
if visited[i][j] is False and g[i][j] == 1:
self.DFS(i, j, visited, g)
for x in range(self.ROW):
for y in range(self.COL):
if self.islands[x][y] is None and visited[x][y] is True:
self.islands[x][y] = (list(string.ascii_lowercase)[count], numb)
count += 1
print(np.array(self.islands))
return count
def highlight(self, number, g, r, c):
adjacency_matrix = copy.deepcopy(g)
for i in range(r):
for j in range(c):
adjacency_matrix[i][j] = 1 if adjacency_matrix[i][j] == number else 0
return adjacency_matrix
def bins(self):
bins = {}
for x in range(self.ROW):
for y in range(self.COL):
if self.islands[x][y] not in bins:
bins[self.islands[x][y]] = 1
else:
bins[self.islands[x][y]] += 1
return bins
if __name__ == '__main__':
graph = [[2, 1, 2, 2, 1, 1],
[2, 2, 1, 2, 1, 1],
[2, 1, 3, 2, 1, 1],
[2, 2, 2, 1, 1, 2],
[2, 2, 1, 1, 2, 2],
[2, 2, 1, 1, 2, 2]]
row = len(graph)
col = len(graph[0])
g = Graph(row, col, graph)
print("Number of islands is:")
g.countIslands()
print(g.bins())
# Program to count islands in boolean 2D matrix
import copy
import string
import sys
import numpy as np
from enum import Enum
from skimage import io
from matplotlib import pyplot as plt
class KEYS(Enum):
LABEL = 'label'
COLOR = 'color'
COUNT = 'count'
ALPHA = 'alpha'
BETA = 'beta'
PIXELS = 'pixels'
class Graph:
def __init__(self, row, col, g, bins, threshold):
self.ROW = row
self.COL = col
self.graph = g
self.connected_components = [[None for _ in range(col)] for _ in range(row)]
self.bins = bins
self.threshold = threshold
def isSafe(self, i, j, visited, g):
return 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and g[i][j]
def DFS(self, i, j, visited, g):
rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]
colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]
visited[i][j] = True
for k in range(8):
if self.isSafe(i + rowNbr[k], j + colNbr[k], visited, g):
self.DFS(i + rowNbr[k], j + colNbr[k], visited, g)
def label_connected_components(self, visited, label, count):
for x in range(self.ROW):
for y in range(self.COL):
if self.connected_components[x][y] is None and visited[x][y] is True:
self.connected_components[x][y] = {
KEYS.LABEL.name: count, # list(string.ascii_lowercase)[count],
KEYS.COLOR.name: label
}
def detect_connected_components(self):
visited = [[False for _ in range(self.COL)] for _ in range(self.ROW)]
count = 0
for numb in range(1, self.bins + 1):
g = self.binary_matrix(numb, self.graph, self.ROW, self.COL)
for i in range(self.ROW):
for j in range(self.COL):
if visited[i][j] is False and g[i][j] == 1:
self.DFS(i, j, visited, g)
self.label_connected_components(visited, numb, count)
count += 1
print(np.array(visited))
return count
def binary_matrix(self, number, g, r, c):
adjacency_matrix = copy.deepcopy(g)
for i in range(r):
for j in range(c):
adjacency_matrix[i][j] = 1 if adjacency_matrix[i][j] == number else 0
return adjacency_matrix
def ccv(self):
results = {}
for x in range(self.ROW):
for y in range(self.COL):
label = self.connected_components[x][y][KEYS.LABEL.name]
color = self.connected_components[x][y][KEYS.COLOR.name]
if label not in results:
results[label] = {
KEYS.COUNT.name: 1,
KEYS.COLOR.name: color,
KEYS.PIXELS.name: [(x, y)]
}
else:
results[label][KEYS.COUNT.name] += 1
results[label][KEYS.PIXELS.name] += [(x, y)]
vector = {}
for i in range(1, self.bins + 1):
vector[i] = {
KEYS.ALPHA.name: 0,
KEYS.BETA.name: 0
}
for result in results:
color = results[result][KEYS.COLOR.name]
if results[result][KEYS.COUNT.name] >= self.threshold:
vector[color][KEYS.ALPHA.name] += results[result][KEYS.COUNT.name]
else:
vector[color][KEYS.BETA.name] += results[result][KEYS.COUNT.name]
return results, vector
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
def visualize(img, results, threshold, alpha):
rows = len(img)
columns = len(img[0, 0:])
above = copy.deepcopy(img)
# above[i, j, :] = [0, 255, 0]
coherent_pixels = []
for result in results:
if results[result][KEYS.COUNT.name] >= threshold:
coherent_pixels += results[result][KEYS.PIXELS.name]
print(len(coherent_pixels))
for x, y in coherent_pixels:
above[x, y, :] = [0, 255, 0]
plt.imshow(img)
plt.imshow(above, interpolation='none', alpha=alpha)
plt.show()
if __name__ == '__main__':
sys.setrecursionlimit(2000000000)
print(sys.getrecursionlimit())
graph = np.array([[2, 1, 2, 2, 1, 1],
[2, 2, 1, 2, 1, 1],
[2, 1, 3, 2, 1, 1],
[2, 2, 2, 1, 1, 2],
[2, 2, 1, 1, 2, 2],
[2, 2, 1, 1, 2, 2]])
b = 3
t = 50
a = 0.5
img = np.array(io.imread('./A2/images/astronauts/Eileen-Collins-2.jpg')) # [:100, :100]
gray = np.array(np.round(np.divide(rgb2gray(img), 256 / (b - 1))).astype(np.int))
gray = np.array(np.add(gray, 1))
print(type(gray))
print(np.array(gray))
row = len(gray)
col = len(gray[0])
g = Graph(row, col, gray, b, t)
print("Connected components: ")
g.detect_connected_components()
# print(g.connected_components)
res, vec = g.ccv()
print(res)
print(vec)
visualize(img, res, t, a)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment