a b/Image features calculation code/Working/shape features/CrackcodePerim.m
1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
%%%%%% New Perimeter Algorithm %%%%%%
3
%%%%%% Shu Xie                 %%%%%%
4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
function P = CrackcodePerim(I) %#ok<*INUSD,*STOUT>
7
8
%%%%  Create Binary image  %%%%
9
for xi = 1:size(I,1)
10
    for yi = 1:size(I,2)
11
        if I(xi,yi) ~= 0
12
            I(xi,yi) = 1;
13
        end
14
    end
15
end
16
        
17
counter_p=0;    % Counter for Perimeter
18
if size(I,1)>1 && size(I,2)>1 % When Image size is larger that 1*1 pixels    
19
    for x = 1:size(I,1)
20
        for y = 1:size(I,2)
21
            if I(x,y) == 1
22
                if x == 1
23
                % perimeter9(): x = 1 and y = 1
24
                    if y == 1
25
                        counter_p = counter_p + 2;  % Boundary that outside image
26
                        if I(x,y+1) == 0
27
                            counter_p = counter_p + 1;
28
                        end
29
                        if I(x+1,y) == 0
30
                            counter_p = counter_p + 1;
31
                        end
32
                % perimeter8(): x = 1 and y = max
33
                elseif y == size(I,2)
34
                    counter_p = counter_p + 2;  % Boundary that outside image
35
                    if I(x,y-1) == 0
36
                        counter_p = counter_p + 1;
37
                    end
38
                    if I(x+1,y) == 0
39
                        counter_p = counter_p + 1;
40
                    end 
41
                % perimeter7(): x = 1 and y = mid
42
                else
43
                    counter_p = counter_p + 1;  % Boundary that outside image
44
                    if I(x,y+1) == 0
45
                        counter_p = counter_p + 1;
46
                    end
47
                        if I(x,y-1) == 0
48
                            counter_p = counter_p + 1;
49
                        end
50
                        if I(x+1,y) == 0
51
                            counter_p = counter_p + 1;
52
                        end  
53
                    end
54
                elseif x == size(I,1)
55
                    % perimeter6(): x = max and y = 1
56
                    if y == 1
57
                        counter_p = counter_p + 2;  % Boundary that outside image
58
                        if I(x,y+1) == 0
59
                            counter_p = counter_p + 1;
60
                        end
61
                        if I(x-1,y) == 0
62
                            counter_p = counter_p + 1;
63
                        end  
64
                    % perimeter5(): x = max and y = max
65
                    elseif y == size(I,2)
66
                        counter_p = counter_p + 2;  % Boundary that outside image
67
                        if I(x,y-1) == 0
68
                            counter_p = counter_p + 1;
69
                        end
70
                        if I(x-1,y) == 0
71
                            counter_p = counter_p + 1;
72
                        end  
73
                    % perimeter4(): x = max and y = mid
74
                    else
75
                        counter_p = counter_p + 1;  % Boundary that outside image
76
                        if I(x,y+1) == 0
77
                            counter_p = counter_p + 1;
78
                        end
79
                        if I(x,y-1) == 0
80
                            counter_p = counter_p + 1;
81
                        end
82
                        if I(x-1,y) == 0
83
                            counter_p = counter_p + 1;
84
                        end  
85
                    end
86
                else
87
                    % perimeter3(): x = mid and y = 1
88
                    if y == 1
89
                        counter_p = counter_p + 1;  % Boundary that outside image
90
                        if I(x,y+1) == 0
91
                            counter_p = counter_p + 1;
92
                        end
93
                        if I(x+1,y) == 0
94
                            counter_p = counter_p + 1;
95
                        end
96
                        if I(x-1,y) == 0
97
                            counter_p = counter_p + 1;
98
                        end  
99
                    % perimeter2(): x = mid and y = max
100
                    elseif y == size(I,2)
101
                        counter_p = counter_p + 1;  % Boundary that outside image
102
                        if I(x,y-1) == 0
103
                            counter_p = counter_p + 1;
104
                        end
105
                        if I(x+1,y) == 0
106
                            counter_p = counter_p + 1;
107
                        end
108
                        if I(x-1,y) == 0
109
                            counter_p = counter_p + 1;
110
                        end 
111
                    % perimeter1(): x = mid and y = mid
112
                    else
113
                        if I(x,y+1) == 0
114
                            counter_p = counter_p + 1;
115
                        end
116
                        if I(x,y-1) == 0
117
                            counter_p = counter_p + 1;
118
                        end
119
                        if I(x+1,y) == 0
120
                            counter_p = counter_p + 1;
121
                        end
122
                        if I(x-1,y) == 0
123
                            counter_p = counter_p + 1;
124
                        end
125
                    end
126
                end
127
            end
128
        end
129
    end
130
end           
131
P = counter_p;