-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-size-mask.py
140 lines (102 loc) · 4.43 KB
/
auto-size-mask.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
import cv2
import numpy as np
import os
from PIL import Image
# np.set_printoptions(threshold=np.inf)
from skimage.morphology import skeletonize
from scipy.ndimage import distance_transform_edt
def one_width_corrosion(img):
img = np.asarray(img)
# print(img.max())
# print(img.min())
# 骨架化
skeleton = skeletonize(img//255)
# plt.imshow(skeleton, cmap='gray')
# plt.show()
kernel3 = np.ones((3, 3), np.uint8)
onepixel_corrosion = cv2.erode(img, kernel3, iterations=1)
onepixel_corrosion = cv2.morphologyEx(onepixel_corrosion, cv2.MORPH_CLOSE, kernel3)
# plt.imshow(onepixel_corrosion, cmap='gray')
# plt.show()
out = onepixel_corrosion + skeleton*255
out[out>=255] = 255
# plt.imshow(out, cmap='gray')
# plt.show()
# cv2.imwrite('corrosion.PNG', out)
return out
def one_width_dilation(img):
img = np.asarray(img)
# 骨架化
skeleton = skeletonize(img//255)
# 计算骨架中每个像素到最近的血管的距离
dist_transform = distance_transform_edt(img//255)
onepixel = np.zeros((584,565))
# for i in range(1, 584-1):
# for j in range(1, 565-1):
# if skeleton[i][j]==1 and dist_transform[i][j] == 1:
# onepixel[i][j] = 255
for i in range(1, 584-1):
for j in range(1, 565-1):
one_p = True
if skeleton[i][j]==1: #如果在血管中心线上
for a in [-1,0,1]: #查找附近9个像素
if one_p==False:
break
for b in [-1,0,1]:
if dist_transform[i+a][j+b] > 1: #如果附近有大于1的距离,则删除这个点
one_p = False
break
if one_p==True:
onepixel[i][j] = 255
kernel3 = np.ones((3, 3), np.uint8)
onepixel_dilation = cv2.dilate(onepixel, kernel3, iterations=1)
# onepixel_dilation = cv2.morphologyEx(onepixel, cv2.MORPH_OPEN, kernel3)
out = onepixel_dilation + img
out[out>255] = 255
return out
def dilation_median(img):
img_arr = np.asarray(img)
# 先定义一个核大小
kernel1 = np.ones((1, 1), np.uint8)
kernel2 = np.ones((2, 2), np.uint8)
kernel3 = np.ones((3, 3), np.uint8)
kernel5 = np.ones((5, 5), np.uint8)
# Opening operation 执行开操作 去除很细的边缘
img_dila2 = cv2.morphologyEx(img_arr, cv2.MORPH_OPEN, kernel3)
# Obtain edges 获得边 对边像素执行必操作,连通断裂的边缘
onepixel = img_arr - img_dila2
onepixel = cv2.morphologyEx(onepixel, cv2.MORPH_CLOSE, kernel1)
# Dilation of the repaired edges 膨胀边缘
onepixel_dilation = cv2.dilate(onepixel.astype(np.uint8), kernel2, iterations=1)
# Combine original image and dilated edges 将获得的边与原图相加 得到自适应膨胀血管
out = img_arr + onepixel_dilation
return out
# Input and output directories
input_folder = '/root/FR-UNet-master/dataset/DRIVE/training/1st_manual'
output_folder = '/root/FR-UNet-master/dataset/DRIVE/training/1st_manual_corrosion'
# output_folder1 = 'D:/pythonProject/2022/FR-UNet-master/auto_middle_vessel/DRIVE_origin'
# Create output directory if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Process each .gif file in the input folder
for filename in os.listdir(input_folder):
if filename.endswith('.gif'):
filepath = os.path.join(input_folder, filename)
# print(filepath)
# Read the gif image
# img = cv2.imread(filepath, cv2.IMREAD_UNCHANGED)
img = Image.open(filepath)
# print("origin ==",np.asarray(img))
# Apply dilation_median function
# processed_img = one_width_dilation(img)
processed_img = one_width_corrosion(img)
processed_img[processed_img > 0] = 255
# print("dilation ==",np.asarray(processed_img))
# Write the processed image to the output folder
output_filepath = os.path.join(output_folder, filename[:-3]+"PNG")
print(output_filepath)
cv2.imwrite(output_filepath, processed_img)
# output_filepath = os.path.join(output_folder1, filename[:-3] + "PNG")
# img.save(output_filepath)
# break
print("Processing completed!")