File size: 3,150 Bytes
6c0075d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


import numpy as np
from skimage import morphology, measure
from AV.Tools.Remove_small_holes import remove_small_holes
import scipy.ndimage.morphology as scipyMorphology


def binaryPostProcessing(BinaryImage, removeArea):
    """

    Post process the binary segmentation

    :param BinaryImage:

    :param removeArea:

    :return: Img_BW

    """

    BinaryImage[BinaryImage > 0] = 1

    ###9s
    # Img_BW = pymorph.binary(BinaryImage)
    # Img_BW = pymorph.areaopen(Img_BW, removeArea)
    # Img_BW = pymorph.areaclose(Img_BW, 50)
    # Img_BW = np.uint8(Img_BW)

    ###2.5 s
    # Img_BW = np.uint8(BinaryImage)
    # Img_BW = ITK_LabelImage(Img_BW, removeArea)
    # Img_BW[Img_BW >0] = 1

    Img_BW = BinaryImage.copy()
    BinaryImage_Label = measure.label(Img_BW)
    for i, region in enumerate(measure.regionprops(BinaryImage_Label)):
        if region.area < removeArea:
            Img_BW[BinaryImage_Label == i + 1] = 0
        else:
            pass

    Img_BW = morphology.binary_closing(Img_BW, morphology.disk(3))
    Img_BW = remove_small_holes(Img_BW, 50)
    Img_BW = np.uint8(Img_BW)

    return Img_BW


################Three parameters
def binaryPostProcessing3(BinaryImage, removeArea, fillArea):
    """

    Post process the binary image

    :param BinaryImage:

    :param removeArea:

    :param fillArea:

    :return: Img_BW

    """

    BinaryImage[BinaryImage>0]=1

    ####takes 0.9s, result is good
    Img_BW = BinaryImage.copy()
    BinaryImage_Label = measure.label(Img_BW)
    for i, region in enumerate(measure.regionprops(BinaryImage_Label)):
        if region.area < removeArea:
            Img_BW[BinaryImage_Label == i + 1] = 0
        else:
            pass

    # ####takes 0.01s, result is bad
    # temptime = time.time()
    # Img_BW = morphology.remove_small_objects(BinaryImage, removeArea)
    # print "binaryPostProcessing3, ITK_LabelImage time:", time.time() - temptime


    Img_BW = morphology.binary_closing(Img_BW, morphology.square(3))
    # Img_BW = remove_small_holes(Img_BW, fillArea)

    Img_BW_filled = scipyMorphology.binary_fill_holes(Img_BW)
    Img_BW_dif = np.uint8(Img_BW_filled) - np.uint8(Img_BW)
    Img_BW_difLabel = measure.label(Img_BW_dif)
    FilledImg = np.zeros(Img_BW.shape)
    for i, region in enumerate(measure.regionprops(Img_BW_difLabel)):
        if region.area < fillArea:
            FilledImg[Img_BW_difLabel == i + 1] = 1
        else:
            pass
        Img_BW[FilledImg > 0] = 1

    Img_BW = np.uint8(Img_BW)
    return Img_BW


def removeSmallBLobs(BinaryImage, removeArea):
    """

    Post process the binary image

    :param BinaryImage:

    :param removeArea:

    """

    BinaryImage[BinaryImage>0]=1

    ####takes 0.9s, result is good
    Img_BW = BinaryImage.copy()
    BinaryImage_Label = measure.label(Img_BW)
    for i, region in enumerate(measure.regionprops(BinaryImage_Label)):
        if region.area < removeArea:
            Img_BW[BinaryImage_Label == i + 1] = 0
        else:
            pass
    return np.uint8(Img_BW)