-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvexhull2d.py
47 lines (35 loc) · 1.38 KB
/
convexhull2d.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
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from skimage.measure import label
from skimage.morphology import disk, binary_closing, binary_opening, convex_hull_image
from skimage.color import label2rgb
plt.rcParams["font.size"] = 16
def convex_hull_2d(input_img):
# Threshold
output_img = np.where(input_img>100, 1, 0)
# closing
output_img = binary_closing( output_img, disk(3) )
# opening
output_img = binary_opening( output_img, disk(2) )
# labeling
output_img = label( output_img )
max_label = np.max( output_img )
print("max_label:", max_label)
labeled_result_img = label2rgb( output_img, bg_label=0 )
# convex hull
for i in range(1,max_label+1):
label_img = np.where( output_img==i, 1, 0 )
label_img = convex_hull_image( label_img )
output_img = np.where( label_img==1, i, output_img )
output_rgb = label2rgb( output_img, bg_label=0 )
# Plot the input and output images.
plt.figure(figsize=(12, 4), dpi=50)
plt.subplot(1,3,1),plt.title("original"),plt.imshow(input_img, cmap="gray")
plt.subplot(1,3,2),plt.title("labeled result"),plt.imshow(labeled_result_img)
plt.subplot(1,3,3),plt.title("convex hull result"),plt.imshow(output_rgb)
plt.show()
def main() :
print("This is an example of convex hull usiong scikit-image.")
if __name__ == "__main__":
main()