-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjSelect.py
More file actions
executable file
·72 lines (68 loc) · 2.04 KB
/
objSelect.py
File metadata and controls
executable file
·72 lines (68 loc) · 2.04 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
import copy
import sys
from PIL import Image
from PIL import ImageFilter
from pprint import pprint
class obj:
points = []
S = 0
def checkExistance(self, ij):
return self.points.count(ij)
def selectObject(self, im, matrix, i, j):
a = copy.deepcopy(i)
b = copy.deepcopy(j)
self.points.append((a,b))
self.S += 25
matrix[a][b] = 2
im.putpixel((b*5,a*5), 100)
if a < len(matrix)-2:
if matrix[a+1][b] == 1 and self.checkExistance((a+1, b)) == 0:
self.selectObject(im, matrix, a+1, b)
if a > 0:
if matrix[a-1][b] == 1 and self.checkExistance((a-1, b)) == 0:
self.selectObject(im, matrix, a-1, b)
if b < len(matrix[0])-2:
if matrix[a][b+1] == 1 and self.checkExistance((a, b+1)) == 0:
self.selectObject(im, matrix, a, b+1)
if b > 0:
if matrix[a][b-1] == 1 and self.checkExistance((a, b-1)) == 0:
self.selectObject(im, matrix, a, b-1)
return 0
im = Image.open("images/output.jpg")
im = im.convert("P")
x = 0
y = 0
matrix = []
buf = []
pixel_sektor = 1
while y < im.size[1]:
while x < im.size[0]:
if im.getpixel((x,y)) == 0:
buf.append(1)
else:
buf.append(0)
x+=pixel_sektor
x=0
y+=pixel_sektor
matrix.append(buf)
buf = []
print len(matrix)
print len(matrix[0])
i = 0
j = 0
objects = []
flag = False
sys.setrecursionlimit(5000)
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 1:
for k in objects:
if k.checkExistance((i, j)) != 0:
flag = True
if not flag:
objects.append(obj())
objects[len(objects)-1].selectObject(im, matrix, i, j)
print "Object #", len(objects), " created!"
print "His squere is ", objects[len(objects)-1].S
flag = False
im.show()