-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathps1_fail.py
More file actions
141 lines (117 loc) · 4.06 KB
/
ps1_fail.py
File metadata and controls
141 lines (117 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# print("WELCOME ")
# print("READING IMAGE...")
# ############### READING IN PHOTO ##############################
# rawimg = cv2.imread(inPath + photo1, cv2.IMREAD_UNCHANGED)
# #test data
# X[m,n]
# m = # pixels
# n = dimensions of color
# K = # clusters
# iters = # of iterations
# numpix,colors = (36,3)
# K = 3
# iters = 3
# 6x6 pixel photo, 3 color density
# set each pixel itensity value to a random value
# make our input matrix
# X = [random.sample(range(0,255),3) for b in range(numpix)]
#
# def kmeans_single(X, K, iters):
# # returns ids[m] = list 1-K
# # means[k,n] = list of means around each cluster
# # ssd = sum of sqared ditances between points and assigned means over all clusters
# # ^ yikes
# numpix = len(X)
# reds = []
# greens = []
# blues = []
# if (K == 3):
# for i in range(numpix):
# #isolate colors for sanity reasons
# reds.append(X[i][0])
# greens.append(X[i][1])
# blues.append(X[i][2])
# # generate cluster centers
# # generating a random int to be the center based on the min/max
# # of the respective color K(num of clusters) times, in this case 3
# redCenters = [random.randint(min(reds), max(reds)) for b in range(K)]
# greenCenters = [random.randint(min(greens), max(greens)) for c in range(K)]
# blueCenters = [random.randint(min(blues), max(blues)) for d in range(K)]
# print("X : ")
# print(X)
# print(" REDS : ")
# print(reds)
# print(" GREENS : ")
# print(greens)
# print(" BLUES : ")
# print(blues)
# print(" RED CENTERS : ")
# print(redCenters)
# print(" BLUE CENTERS : ")
# print(blueCenters)
# print(" GREEN CENTERS : ")
# print(greenCenters)
# elif(K == 1):
# # todo
# pass
# else:
# print("ERROR: K OUT OF SCOPE!")
# return -1
#LIBRARIES
import cv2
import os
import numpy as np
import time
import pandas as pd
import matplotlib.pyplot as plt
import random
def makeData(pts = 100, rad = 1, min = 0, max = 1, xcenter = 0, ycenter = 0):
pi = np.pi
r = rad * np.sqrt(np.random.uniform(min, max, size = pts))
theta = np.random.uniform(min, max, size= pts)*2*pi
x = xcenter + np.cos(theta) * r
y = ycenter + np.sin(theta) * r
x = np.round(x,3)
y = np.round(y,3)
df = np.column_stack([x,y])
df = pd.DataFrame(df)
df.columns = ['x','y']
return(df)
def init_centers(X, k):
#grab min/max and dims
n_dims = X.shape[1]
cmin = X.min().min()
cmax = X.max().max()
centers = []
# generate k number of rando nums for each dim
for centers in range(k):
center = np.random.uniform(cmin, cmax, n_dims)
centers= np.append(centers, center)
centers = pd.DataFrame(centers, columns= X.columns)
return centers
def calcDist(a,b):
# calculating the error is the same as calculating distance
# these formulas are the same but in this case
# are not performing the same type of distance
# ergo distance from center value between X and centers
# dist=(x2–x1)2+(y2–y1)2
dist = np.square(np.sum((a-b)**2))
return dist
inPath = r'./input/'
outPath = "C:\\Users\\simaj\\Documents\\School\\CV\\ps1_python_Sima_John\\output"
photo1 = r"ps1-1-a-1.png"
reds = makeData(pts= 25, rad= 10, xcenter= 5,ycenter= 5)
greens = makeData(pts= 25, rad= 10, xcenter= 20,ycenter= 8)
blues = makeData(pts= 25, rad= 10, xcenter= 10,ycenter= 20)
X = reds.append(greens).append(blues)
X.head()
K = 10
distances = np.array([])
plt.scatter(reds['x'], reds['y'], c= 'r')
plt.scatter(greens['x'], greens['y'], c= 'g')
plt.scatter(blues['x'], blues['y'], c= 'b')
plt.show()
centers = init_centers(X,K)
for center in range(centers.shape[0]):
dist = calcDist(centers.iloc[center, :2], X.iloc[0,:2])
distances = np.append(distances, dist)