-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsegmentation_tools.py
104 lines (80 loc) · 3.32 KB
/
segmentation_tools.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
import pdb
import numpy as np
from scipy.ndimage import label
from skimage.util import img_as_float
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
# helpful slic tool
def FSLIC(IMAGE, IM, NumSLIC, ComSLIC, SigSLIC, Initial=False):
IMAGE = img_as_float(IMAGE)
Segments = slic(IMAGE, n_segments=NumSLIC, sigma=SigSLIC, compactness=ComSLIC)
if Initial == True: # if initial is true, it returns the fusedimage showing the segments
Fusied_Image = mark_boundaries(IM, Segments, color = (1, 1, 1))
return (Segments, Fusied_Image[...,0])
else:
return (Segments)
# normalize image between zero and range provided
def Normalize_Image(IMAGE, Range, Min=None, Max=None, flag_max_edition=None, flag_min_edition=None, bits_conversion=None):
IMAGE = IMAGE.astype('float')
if Min==None: Min = IMAGE.min()
if Max==None: Max = IMAGE.max()
if Min != Max:
Out_Img = (IMAGE-Min)/(Max-Min)
else:
Out_Img = np.ones(Out_Img.shape)
Out_Img = Out_Img*Range
if flag_max_edition == None:
try:
Out_Img[Out_Img>Range] = Range
except:
Out_Img = Out_Img
if flag_min_edition == None:
try:
Out_Img[Out_Img<0] = 0
except:
Out_Img = Out_Img
if bits_conversion == None:
if Range == 2**16-1:
Out_Img = Out_Img.astype('uint16')
else:
Out_Img = Out_Img.astype('uint8')
else:
Out_Img = Out_Img.astype(bits_conversion)
return(Out_Img)
# trimming the image and its mask if it is given
def cutting_image(IMG, skipping_rows, MASK=[], x_ratio=1, y_ratio=1, skip_columns_to=[]):
if len(skip_columns_to) == 0:
if round(IMG.shape[0]*x_ratio) * round(IMG.shape[1]*y_ratio) %2 == 0:
IMG = IMG[skipping_rows:round(IMG.shape[0]*x_ratio),
:round(IMG.shape[1]*y_ratio)]
if len(MASK)>0:
MASK = MASK[skipping_rows:round(MASK.shape[0]*x_ratio),
:round(MASK.shape[1]*y_ratio)]
else:
IMG = IMG[skipping_rows:round(IMG.shape[0]*x_ratio),
:round(IMG.shape[1]*y_ratio)-1]
if len(MASK)>0:
MASK = MASK[skipping_rows:round(MASK.shape[0]*x_ratio),
:round(MASK.shape[1]*y_ratio)-1]
else:
IMG = IMG[skipping_rows:round(IMG.shape[0]*x_ratio),
:int(skip_columns_to[0])]
if len(MASK)>0:
MASK = MASK[skipping_rows:round(MASK.shape[0]*x_ratio),
:int(skip_columns_to[0])]
return(IMG, MASK)
# in a mask find the largest object
def find_largest_obj(Mask):
# one shows background and zero objects
temp_mask = (np.logical_not(Mask)).astype("int")
# make the first and last row zero to make sure it is not affected by noise
temp_mask[0,:] = 0
labeled_obj = label(temp_mask)[0]
if labeled_obj.max()>1:
BG_Label = labeled_obj[0, -1]
Unique_labels, counts = np.unique(labeled_obj, return_counts=True)
counts = np.delete(counts, np.where(Unique_labels==BG_Label), None)
Unique_labels = np.delete(Unique_labels, np.where(Unique_labels==BG_Label), None)
Max = Unique_labels[counts.argmax()]
Mask[labeled_obj!=Max] = True
return(Mask)