Skip to content

Commit 1ee89b0

Browse files
committed
lab 7
1 parent 65bc531 commit 1ee89b0

File tree

9 files changed

+116
-0
lines changed

9 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Task 7.
2+
Task: Calculate the vector of Minkowski dimensions for the selected image when resizing the split cell.
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from PIL import Image, ImageDraw
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
delta = [1, 2]
6+
CELL_SIZES = np.arange(5, 30)
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, cell_size):
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 2 - np.polyfit(np.log(As), np.log(delta), 1)[0] * (-1)
61+
62+
def get_graph_data(image):
63+
data = []
64+
for cell_size in CELL_SIZES:
65+
image_copy = image
66+
data.append(count_dimension(image_copy, cell_size))
67+
68+
return data
69+
70+
71+
72+
if __name__ == '__main__':
73+
finished = False
74+
while not finished:
75+
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'")
76+
names = input()
77+
if not names:
78+
names = ['image.jpg']
79+
else:
80+
names = names.split(' ')
81+
try:
82+
plt.cla()
83+
fig, ax = plt.subplots()
84+
for name in names:
85+
im = Image.open(name)
86+
im = convert_image(im, 'inverted_' + name)
87+
file_data = get_graph_data(im)
88+
ax.plot(CELL_SIZES, file_data, label = name)
89+
90+
ax.set_xlabel('cell size')
91+
ax.set_ylabel('Dimension')
92+
ax.legend()
93+
fig.savefig("result_graph.png")
94+
finished = True
95+
except Exception as e:
96+
print(e)
97+
print("Could not find the file. Please try again.")
15.7 KB
Loading
8.09 KB
Loading
13.3 KB
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)