-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mean.py
106 lines (86 loc) · 3.5 KB
/
Mean.py
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
import numpy as np
import cv2
import random
Image = cv2.imread('/Users/vanshika/Desktop/CVIP/PA2Data/Butterfly.jpg')
x,y,color_value=Image.shape
#print Image.shape
feature_matrix=np.zeros((x*y,5))
segmented_img=np.zeros(Image.shape,)
m=0
h=155
iterations=30
for i in range(0,x):
for j in range(0,y):
feature_matrix[m,0]=Image[i,j,0] #color storage
feature_matrix[m,1]=Image[i,j,1]
feature_matrix[m,2]=Image[i,j,2]
feature_matrix[m,3]=i #position storing
feature_matrix[m,4]=j
m=m+1
#print feature_matrix
flag=0
while(len(feature_matrix)>0):
index = random.randint(0,len(feature_matrix)-1)
#selected a point p if flag 0
if(flag==0):
mean_r=feature_matrix[index,0]
mean_g=feature_matrix[index,1]
mean_b=feature_matrix[index,2]
mean_x=feature_matrix[index,3]
mean_y=feature_matrix[index,4]
cluster=[]
eucli_dist=0
for i in range(0,len(feature_matrix)):
eucli_dist=np.sqrt((mean_r-feature_matrix[i,0])**2+(mean_g-feature_matrix[i,1])**2+(mean_b-feature_matrix[i,2])**2+(mean_x-feature_matrix[i,3])**2+(mean_y-feature_matrix[i,4])**2)
if eucli_dist<h:
cluster.append(i)
print "to check in", len(feature_matrix)
if(len(cluster)>0):
new_mean_r=0
new_mean_g=0
new_mean_b=0
new_mean_x=0
new_mean_y=0
for i in range (0,len(cluster)):
"""
new_mean_r=np.mean(feature_matrix[cluster[i]][0])
new_mean_g=np.mean(feature_matrix[cluster[i]][1])
new_mean_b=np.mean(feature_matrix[cluster[i]][2])
new_mean_x=np.mean(feature_matrix[cluster[i]][3])
new_mean_y=np.mean(feature_matrix[cluster[i]][4])
"""
new_mean_r += feature_matrix[cluster[i]][0]
new_mean_g += feature_matrix[cluster[i]][1]
new_mean_b += feature_matrix[cluster[i]][2]
new_mean_x += feature_matrix[cluster[i]][3]
new_mean_y += feature_matrix[cluster[i]][4]
new_mean_r=new_mean_r/len(cluster)
new_mean_g=new_mean_g/len(cluster)
new_mean_b=new_mean_b/len(cluster)
new_mean_x=new_mean_x/len(cluster)
new_mean_y=new_mean_y/len(cluster)
new_eucli_dist= np.sqrt((new_mean_r-mean_r)**2+(new_mean_g-mean_g)**2+(new_mean_b-mean_b)**2+(new_mean_x-mean_x)**2+(new_mean_y-mean_y)**2)
print ("eucledian dist= ",new_eucli_dist)
if(new_eucli_dist<iterations):
print "cluster.length ",len(cluster)
for i in range(0,len(cluster)):
x=feature_matrix[cluster[i]][3]
y=feature_matrix[cluster[i]][4]
segmented_img[x][y][0] = new_mean_r
segmented_img[x][y][1] = new_mean_g
segmented_img[x][y][2] = new_mean_b
feature_matrix=np.delete(feature_matrix,cluster,0)
flag=0
#print feature_matrix.shape
else:
mean_r=new_mean_r
mean_g=new_mean_g
mean_b=new_mean_b
mean_x=new_mean_x
mean_y=new_mean_y
flag=1
print "to check out" ,feature_matrix.shape
print "done"
print segmented_img
cv2.imshow("final_seg",segmented_img/segmented_img.max())
cv2.imwrite('/Users/vanshika/Desktop/CVIP/PA2Data/Segments_155_30.png',segmented_img)