-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3.py
executable file
·183 lines (170 loc) · 5.88 KB
/
lab3.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import sys, os
import json
import random
import math
import argparse
from PIL import Image
# import numpy
from imageSegmentation import histogram
def main(file):
img = Image.open(file)
size = img.size
img = img.convert(mode='L')
img.show()
histogram.histogramSegmentation(img, size, 'otsu', 100)
img.show()
cutObjectsFromImage(img, size)
def getAperturePosition(x, y, imgSize, i, j, filterSize):
pixelPosX = x + i - 1
pixelPosY = y + j - 1
if pixelPosX < 0:
pixelPosX += 1
if pixelPosY < 0:
pixelPosY += 1
if pixelPosX > imgSize[0] or pixelPosY > imgSize[1]:
return -1, -1
if pixelPosX == imgSize[0]:
pixelPosX = imgSize[0] - 2
if pixelPosY == imgSize[1]:
pixelPosY = imgSize[1] - 2
return pixelPosX, pixelPosY
def findPointsInApperture(pixels, x, y, imgSize, filterSize, img, imageFigures):
whiteColor = 255
pointList = [getAperturePosition(x, y, imgSize, 0, 0, filterSize)]
# for i in range(filterSize):
# for j in range(filterSize):
pointTemp = []
i = j = 0
img2 = img.convert(mode='RGB')
pixels2 = img2.load()
goBack = True
lastX = []
lastY = []
while True:
if i >= filterSize:
i = 0
if goBack >= len(lastX):
break
else:
goBack += 1
x = lastX[-goBack]
y = lastY[-goBack]
while True:
if j >= filterSize:
j = 0
break
pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
if pixelPosX == -1 or pixelPosY == -1:
j += 1
continue
if (pixelPosX, pixelPosY) in pointList:
j += 1
continue
if pixels[pixelPosX, pixelPosY] == whiteColor:
pixels2[pixelPosX, pixelPosY] = (255, 0, 0)
pointList.append((pixelPosX, pixelPosY))
x = pixelPosX
y = pixelPosY
lastX.append(x)
lastY.append(y)
i = j = 0
# wasApertureCenterList = wasApertureCenterList + findPointsInApperture(
# pixels, x, y, imgSize, filterSize, wasApertureCenterList)
j += 1
i += 1
img2.show()
return pointList
def cutObjectsFromImage(img, imgSize):
pixels = img.load()
filterSize = 3
objectID = 0
imageFigures = {
'figures': []
}
x = 0
y = 0
whiteColor = 255
apertureTempX = 0
apertureTempY = 0
figurePointList = []
# for apertureTempX in range(imgSize[0]):
# for apertureTempY in range(imgSize[1]):
while True:
if apertureTempX >= imgSize[0]:
apertureTempX = 0
break
while True:
if apertureTempY >= imgSize[1]:
apertureTempY = 0
break
x = apertureTempX
y = apertureTempY
flag = False
if pixels[x, y] == whiteColor:
if not imageFigures['figures']:
flag = True
else:
countFigure = 0
for figure in imageFigures['figures']:
if (x, y) in figure['pontList']:
break
else:
countFigure += 1
if countFigure == len(imageFigures['figures']):
flag = True
if flag:
figurePointList = findPointsInApperture(pixels, x, y, imgSize, filterSize, img, imageFigures)
# print(figurePointList)
# return
imageFigures['figures'].append({
'pontList': figurePointList
})
apertureTempY += filterSize
apertureTempX += filterSize
# print(imageFigures)
# return
print(imageFigures)
# for point in pointList:
# if !point['wasApertureCenter']:
# x = point['point'][0]
# y = point['point'][1]
# wasApertureCenterList.append(point['point'])
# print(pointList)
#
# for i in range(filterSize):
# for j in range(filterSize):
# pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
# if pixelPosX == -1 or pixelPosY == -1:
# continue
# if pixels[pixelPosX, pixelPosY] == whiteColor:
# x = pixelPosX
# y = pixelPosY
# imageFigures['figures'][-1]['pontList'].append({
# 'x': x,
# 'y': y,
# 'wasApertureCenter': True
# })
# else:
# minValue = pixels[pixelPosX, pixelPosY]
# avg = (minValue + maxValue) / 2
# for i in range(filterSize):
# for j in range(filterSize):
# pixelPosX, pixelPosY = getAperturePosition(x, y, imgSize, i, j, filterSize)
# if pixelPosX == -1 or pixelPosY == -1:
# continue
# if pixels[pixelPosX,pixelPosY] < avg:
# pixels[pixelPosX,pixelPosY] = 0
# else:
# pixels[pixelPosX,pixelPosY] = 255
img.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Find similar images.')
parser.add_argument('-f', '--file',
nargs='?',
metavar='file',
dest='file',
const='./lab3test.png',
default='./lab3test.png',
help='a path to image')
args = parser.parse_args()
sys.exit(main(args.file))