-
Notifications
You must be signed in to change notification settings - Fork 0
/
morfo.py
80 lines (61 loc) · 2.96 KB
/
morfo.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
import numpy as np
from PIL import Image
from strel import strel_line
def find(mask, cut):
list = []
output = np.array(list)
for row in range(mask.shape[0]):
for col in range(mask.shape[1]):
if mask[row, col] == 1:
output = np.append(output, [cut[row, col]])
return output
def erode(im, len, deg):
strel = strel_line(len, deg)
strel_row_around = strel.shape[0] - 1
strel_col_around = strel.shape[1] - 1
strel_row_one_side = int(strel_row_around / 2)
strel_col_one_side = int(strel_col_around / 2)
image = np.array(im)
# powiększenie rozszerzenie obrazu o jedynki
image_big = np.ones((image.shape[0] + strel_row_around, image.shape[1] + strel_col_around))
image_big[image_big > 0] = 255
image_big[strel_row_one_side:image.shape[0] + strel_row_one_side,
strel_col_one_side:image.shape[1] + strel_col_one_side] = image
# stworzenie obrazu wyjściowego
image_out = np.zeros((image.shape[0], image.shape[1]))
for row in range(strel_row_one_side, image.shape[0] + strel_row_one_side):
for col in range(strel_col_one_side, image.shape[1] + strel_col_one_side):
image_cut = image_big[row - strel_row_one_side:row + strel_row_one_side + 1,
col - strel_col_one_side:col + strel_col_one_side + 1]
values = find(strel, image_cut)
image_out[row - strel_row_one_side, col - strel_col_one_side] = np.amin(values)
if np.unique(image_out).shape[0] == 2:
image_out[image_out > 0] = 255
image_out = np.array(image_out, np.uint8)
new_image = Image.fromarray(image_out)
return new_image
def dilate(im, len, deg):
strel = strel_line(len, deg)
strel_row_around = strel.shape[0] - 1
strel_col_around = strel.shape[1] - 1
strel_row_one_side = int(strel_row_around / 2)
strel_col_one_side = int(strel_col_around / 2)
image = np.array(im)
# powiększenie rozszerzenie obrazu o zera
image_big = np.zeros((image.shape[0] + strel_row_around, image.shape[1] + strel_col_around))
# image_big[image_big > 0] = 0
image_big[strel_row_one_side:image.shape[0] + strel_row_one_side,
strel_col_one_side:image.shape[1] + strel_col_one_side] = image
# stworzenie obrazu wyjściowego
image_out = np.zeros((image.shape[0], image.shape[1]))
for row in range(strel_row_one_side, image.shape[0] + strel_row_one_side):
for col in range(strel_col_one_side, image.shape[1] + strel_col_one_side):
image_cut = image_big[row - strel_row_one_side:row + strel_row_one_side + 1,
col - strel_col_one_side:col + strel_col_one_side + 1]
values = find(strel, image_cut)
image_out[row - strel_row_one_side, col - strel_col_one_side] = np.amax(values)
if np.unique(image_out).shape[0] == 2:
image_out[image_out > 0] = 255
image_out = np.array(image_out, np.uint8)
new_image = Image.fromarray(image_out)
return new_image