diff --git a/A2/images/astronauts/Catherine-Coleman.jpg b/A2/images/astronauts/Catherine-Coleman.jpg new file mode 100644 index 0000000000000000000000000000000000000000..53ad70bc1e9329123ec71503503c92d7ce7dfba1 Binary files /dev/null and b/A2/images/astronauts/Catherine-Coleman.jpg differ diff --git a/A2/images/astronauts/Eileen-Collins.jpg b/A2/images/astronauts/Eileen-Collins.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e42f76544783e7746f797f56bacc1ae86028972e Binary files /dev/null and b/A2/images/astronauts/Eileen-Collins.jpg differ diff --git a/A2/images/astronauts/Jeffrey-Shears-Ashby.jpg b/A2/images/astronauts/Jeffrey-Shears-Ashby.jpg new file mode 100644 index 0000000000000000000000000000000000000000..568629ec99fb9a6feda424e7485081f820dc917b Binary files /dev/null and b/A2/images/astronauts/Jeffrey-Shears-Ashby.jpg differ diff --git a/A2/images/astronauts/Michel-Tognini.jpg b/A2/images/astronauts/Michel-Tognini.jpg new file mode 100644 index 0000000000000000000000000000000000000000..19a5ff92d43bdabac1ae12173af51020928f6bb5 Binary files /dev/null and b/A2/images/astronauts/Michel-Tognini.jpg differ diff --git a/A2/images/astronauts/Steven-Alan-Hawley.jpg b/A2/images/astronauts/Steven-Alan-Hawley.jpg new file mode 100644 index 0000000000000000000000000000000000000000..cb49f36329d4367147c7ffc0eb03552aebd6f203 Binary files /dev/null and b/A2/images/astronauts/Steven-Alan-Hawley.jpg differ diff --git a/A2/images/chalsea/Kadse.jpeg b/A2/images/chalsea/Kadse.jpeg deleted file mode 100644 index aa6964c4b7a6dc0dd0629fab3a768a292bee8cf8..0000000000000000000000000000000000000000 Binary files a/A2/images/chalsea/Kadse.jpeg and /dev/null differ diff --git a/A2/images/martian/martian.jpg b/A2/images/martian/martian.jpg deleted file mode 100644 index fc71f5f2f030729f05583c8a7ca09d8d0f1e980f..0000000000000000000000000000000000000000 Binary files a/A2/images/martian/martian.jpg and /dev/null differ diff --git a/A2/images/result.jpeg b/A2/images/result.jpeg index 67fb23912e1d0925557aead9ba9c26d6a8c1324b..6e10d4ddc3acc8ad131e68348d5dfe39538576e0 100644 Binary files a/A2/images/result.jpeg and b/A2/images/result.jpeg differ diff --git a/A2/orb.py b/A2/orb.py index 3be8dc782983efb1f8e6ba65ffca5d6b2659e9c4..08e63763a9eb30f10bf651f2c053ed1c96e6b679 100644 --- a/A2/orb.py +++ b/A2/orb.py @@ -1,9 +1,8 @@ -import math +import os from tokenize import String -from typing import List +from typing import List, Dict import cv2 as cv -import imageio import matplotlib.pyplot as plt import numpy as np @@ -22,6 +21,22 @@ class ORB(object): self.edit_path = edit_path self.k = k + def load_images(self, dir: String) -> List: + """ + This method concats all images in a dir + + :param dir: The directory to search with all images + :return: list of all images in a directory + """ + images = [] + files = os.listdir(dir) + for file in files: + img = os.path.join(dir, file) + img = cv.imread(img, 0) + if img is not None: + images.append(img) + return images + @staticmethod def euclidean(vector1: List, vector2: List) -> float: """ @@ -31,9 +46,7 @@ class ORB(object): :param vector2: Vector as list :return: Euclidean distance between vector1 and vector2. """ - dist = [(a - b) ** 2 for a, b in zip(vector1, vector2)] - dist = math.sqrt(sum(dist)) - return dist + return np.linalg.norm(np.subtract(vector1, vector2)) def pairwise_arg_min(self, X: List, Y: List) -> np.ndarray: """ @@ -46,49 +59,78 @@ class ORB(object): return np.asarray([np.argmin([self.euclidean(x, y) for y in Y]) for x in X]) def find_clusters(self, X, n_clusters, rseed=2): - # 1. Randomly choose clusters + """ + This method finds all clusters. + + :param X: Data to be clustered + :param n_clusters: amount of clusters + :param rseed: random seed + :return: All labels for clustering and the centroids. + """ rng = np.random.RandomState(rseed) i = rng.permutation(X.shape[0])[:n_clusters] centers = X[i] while True: - # 2a. Assign labels based on closest center labels = self.pairwise_arg_min(X, centers) + new_centers = np.array([X[labels == i].mean(0) for i in range(n_clusters)]) - # 2b. Find new centers from means of points - new_centers = np.array([X[labels == i].mean(0) - for i in range(n_clusters)]) - - # 2c. Check for convergence if np.all(centers == new_centers): break centers = new_centers return centers, labels + def similar_features(self, descriptors: List, centroids: List, labels: np.ndarray) -> List: + """ + This calculates the similar features between the groundtruth and a given image. + + :param descriptors: Descriptors of the image + :param centroids: Centroids of the groundtruth + :param labels: Labels of the clusters in the groundtruth + :return: List of similar features by count + """ + labels = list(set(labels)) + zeros = [0] * len(labels) + cluster_counts = dict(zip(labels, zeros)) + + for des in descriptors: + min_index = self.pairwise_arg_min([des], centroids)[0] + cluster_counts[min_index] = cluster_counts[min_index] + 1 + + return self.dict_to_list(cluster_counts) + + @staticmethod + def dict_to_list(target: Dict) -> List: + """ + Turns a dict in a list. + + :param target: Dict to be turned into a list. + :return: A list containing all values of a dict + """ + return [value for _, value in target.items()] + def get_keypoints(self) -> None: """ This method does K-Means with the ORB Keypoints. :return: None """ - img = imageio.imread(uri=self.image_path) - plt.imshow(img) - - # Initiate ORB detector - orb = cv.ORB_create(nfeatures=1000, scoreType=cv.ORB_FAST_SCORE) - - # find the keypoints with ORB - kp = orb.detect(img, None) - # compute the descriptors with ORB - kp, des = orb.compute(img, kp) - key_points = [k.pt for k in kp] + images = self.load_images("./images/astronauts") + orb = cv.ORB_create() + descriptors = None + for img in images: + kp, des = orb.detectAndCompute(img, None) + if descriptors is None: + descriptors = des + else: + descriptors = np.concatenate((descriptors, des), axis=0) - X = np.array([list(x) for x in key_points]) + img = cv.imread(self.image_path, 0) - centers, labels = self.find_clusters(X, self.k) - plt.scatter(X[:, 0], X[:, 1], marker='.', s=10, c=labels, cmap='viridis') + kp, des = orb.detectAndCompute(img, None) + centroids, labels = self.find_clusters(descriptors, self.k) + hist_with_bins = self.similar_features(des, centroids, labels) - plt.scatter(centers[:, 0], centers[:, 1], marker='+', color='red') - plt.axis('off') - plt.savefig(self.edit_path, dpi=300, bbox_inches='tight', pad_inches=0) + plt.bar(range(len(hist_with_bins)), np.multiply(1 / sum(hist_with_bins), hist_with_bins)) + plt.savefig(self.edit_path, dpi=300, bbox_inches='tight') plt.close()