forked from chiragnagpal/cmu-winterschool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
115 lines (68 loc) · 1.69 KB
/
image.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
'''
Copyright [2014] [Chirag Nagpal]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import cv2
import os
import cPickle as pkl
def vectorise(image):
# print image
img = cv2.imread('images/'+image)
height, width = img.shape[:2]
#print height
#print width
t_h = height/4
t_w = width/4
t = []
for i in range(4):
for j in range(4):
t.append(img[i*(t_h):(i+1)*(t_h),(j)*(t_w):(j+1)*(t_w)])
# cv2.imshow('image',t)print os.listdir('images')
# cv2.waitKey(0)
T =[]
rgb = []
for item in t:
r_t=0
g_t=0
b_t=0
htemp,wtemp = (item.shape[:2])
n = 0
temp = []
for i in range(htemp):
for j in range(wtemp):
r_t+=item[i][j][0]
g_t+=item[i][j][1]
b_t+=item[i][j][2]
n+=1
rgb.append(r_t/n)
rgb.append(g_t/n)
rgb.append(b_t/n)
return rgb
def fun(l):
for item in l:
n+=1
print n
if item in vec.keys():
continue
try:
vec[item]=vectorise(item)
# print vec
pkl.dump(vec,open('500vecdic.pkl','wb'))
except Exception as e:
print e
print "ERROR AT:" + item
continue
a = input("No. of Cores:")
vec = pkl.load(open('500vecdic.pkl','rb'))
n=0
l = os.listdir('images')
for i in range(a):
fun(l[i*(len(l)/a):(i+1)*(len(l)/a)])