Skip to content

Commit e570434

Browse files
committed
lab 6
1 parent 829c2d2 commit e570434

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Task 6.
2+
Task: Calculate the value vector ln(𝐴𝛿)/ln(𝛿) 𝛿=1,..10. (You can use both symmetric and asymmetric methods).
3+
4+
How to run:
5+
* Go to the folder containing the program code and image
6+
7+
* Type in the console "python count_vector.py" or "python3 count_vector.py"
8+
9+
* You will be prompted to enter the name of the image file located in the same folder as the program code. If you don't enter any name, the default program will try to find a file named "image.jpg"
10+
11+
* To plot multiple files, enter multiple image names separated by spaces: "image1.jpg image2.jpg image3.jpg"
12+
13+
* The image you submit for entry may be in color. The program itself will invert it into a black and white image, saving it with the name "inverted_" + the name of the original image
14+
15+
* Next, the file "result_graph.png" will be created
16+
17+
# Author
18+
Pavel Dat
19+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from PIL import Image, ImageDraw
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
delta_global = np.arange(2, 11)
6+
CELL_SIZE = 20
7+
8+
def convert_image(image, converted_file_name):
9+
black_and_white = image.convert('1')
10+
black_and_white.save(converted_file_name)
11+
return black_and_white
12+
13+
def get_A(cell, ds):
14+
length, width = cell.size[0], cell.size[1]
15+
u = np.zeros((len(ds) + 1, length + 1, width + 1))
16+
b = np.zeros((len(ds) + 1, length + 1, width + 1))
17+
v = np.zeros((len(ds) + 1))
18+
A = np.zeros((len(ds) + 1))
19+
20+
for i in range(0, length):
21+
for j in range(0, width):
22+
u[0][i][j] = cell.getpixel((i, j))
23+
b[0][i][j] = cell.getpixel((i, j))
24+
25+
for d in ds:
26+
u[d][i][j] = max(u[d - 1][i][j] + 1, max(u[d - 1][i + 1][j + 1], u[d - 1][i - 1][j + 1], u[d - 1][i + 1][j - 1], u[d - 1][i - 1][j - 1]))
27+
b[d][i][j] = min(b[d - 1][i][j] - 1, min(b[d - 1][i + 1][j + 1], b[d - 1][i - 1][j + 1], b[d - 1][i + 1][j - 1], b[d - 1][i - 1][j - 1]))
28+
29+
for d in ds:
30+
v[d] = 0
31+
for i in range(0, length):
32+
for j in range(0, width):
33+
v[d] += u[d][i][j] - b[d][i][j]
34+
35+
for d in ds:
36+
A[d] = (v[d] - v[d-1]) / 2
37+
38+
return A
39+
40+
def count_dimension(image, delta):
41+
length, width = image.size[0], image.size[1]
42+
cell_length, cell_width = length//CELL_SIZE, width//CELL_SIZE
43+
A = np.zeros((cell_length, cell_width, len(delta)+1))
44+
for i in range(0, cell_length):
45+
for j in range(0, cell_width):
46+
area = (i*CELL_SIZE, j*CELL_SIZE, (i+1)*CELL_SIZE, (j+1)*CELL_SIZE)
47+
cell = image.crop(area)
48+
for d in delta:
49+
A[i][j][d] = get_A(cell, delta)[d]
50+
51+
52+
As = []
53+
for d in delta:
54+
s = 0
55+
for i in range(0, cell_length):
56+
for j in range(0, cell_width):
57+
s += A[i][j][d]
58+
As.append(s)
59+
60+
return np.polyfit(np.log(As), np.log(delta), 1)[0] * (-1)
61+
62+
def get_graph_data(image):
63+
data = []
64+
for d in delta_global:
65+
image_copy = image
66+
data.append(count_dimension(image_copy, np.arange(1, d+1)))
67+
return data
68+
69+
70+
71+
if __name__ == '__main__':
72+
finished = False
73+
while not finished:
74+
print("Please, type the name of the file. Or press enter to skip. Default name is image.jpg\n You can type multiple file names, separated by space\n Example: 'image1.jpg image2.jpg image3.jpg'")
75+
names = input()
76+
if not names:
77+
names = ['image.jpg']
78+
else:
79+
names = names.split(' ')
80+
try:
81+
plt.cla()
82+
fig, ax = plt.subplots()
83+
for name in names:
84+
im = Image.open(name)
85+
im = convert_image(im, 'inverted_' + name)
86+
file_data = get_graph_data(im)
87+
ax.plot(delta_global, file_data, label = name)
88+
89+
ax.set_xlabel('delta')
90+
ax.set_ylabel('ln(A delta)/ln(delta)')
91+
ax.legend()
92+
fig.savefig("result_graph.png")
93+
finished = True
94+
except Exception as e:
95+
print(e)
96+
print("Could not find the file. Please try again.")
15.7 KB
Loading
8.09 KB
Loading
13.3 KB
Loading
28.2 KB
Loading
16.6 KB
Loading
27.5 KB
Loading
38.6 KB
Loading

0 commit comments

Comments
 (0)