Created
June 14, 2019 05:50
-
-
Save zbrasseaux/b0f588b3bb26a3898d421e19376f6b10 to your computer and use it in GitHub Desktop.
simple program for reading an image, finding SURF interest points, clustering those, determining the centers of the clusters, and drawing circles around these points.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
Usage: | |
analyze_pic [image] [mode] | |
modes: | |
0 : grayscale | |
1 : color | |
''' | |
# imports | |
import cv2 | |
import sys | |
import matplotlib.pyplot as plt | |
import seaborn as sns; sns.set() | |
from sklearn.cluster import KMeans | |
def analysis(img): | |
'''all the logic and processing for the program''' | |
# initial analysis | |
surf = cv2.xfeatures2d.SURF_create(400) | |
kp, des = surf.detectAndCompute(img, None) | |
# determines the number of clusters to be searched for | |
k_clusters = int(len(kp)/100) | |
if k_clusters < 10: | |
k_clusters = 10 | |
elif k_clusters > 40: | |
k_clusters = 40 | |
# writes original image | |
cv2.imwrite(name + "_" + mode + "_1.jpg", img) | |
# extracts key points and stores them for the k-means | |
# algorithm to read cleanly | |
kp_dict = {i:kp[i].pt for i in range(len(kp))} | |
kp_x = [i[0] for i in kp_dict.values()] | |
kp_y = [i[1] for i in kp_dict.values()] | |
key_points = [[kp_x[i], kp_y[i]] for i in range(len(kp_x))] | |
# kmeans algorithm for determining cluster center points | |
kmeans = KMeans(n_clusters = k_clusters) | |
kmeans.fit(key_points) | |
centers = kmeans.cluster_centers_ | |
# draws circles on the image to visualize the keypoints | |
for i in range(len(centers[:, 0])): | |
cv2.circle(img, (int(centers[:, 0][i]), int(centers[:, 1][i])), 50, 1000) | |
print(centers[:, 0][i]) | |
# writes edited image with keypoints | |
cv2.imwrite(name + "_" + mode + "_2.jpg", img) | |
# reads user input | |
name = sys.argv[1].split(".")[0] | |
mode = sys.argv[2] | |
img = cv2.imread(sys.argv[1], int(mode)) | |
# runs main logic on the input image | |
analysis(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment