Class: RImageAnalysisTools::Skeletonizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rimageanalysistools/skeletonizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(im) ⇒ Skeletonizer

implementation of skeletonization algorithm presented in Gonzalez & Woods, p 651-652



38
39
40
41
42
# File 'lib/rimageanalysistools/skeletonizer.rb', line 38

def initialize(im)

  @im = im

end

Instance Method Details

#compute_n(ic) ⇒ Object



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
# File 'lib/rimageanalysistools/skeletonizer.rb', line 44

def compute_n(ic)

  temp_ic = ImageCoordinate.cloneCoord(ic)

  n = -1  # compensate for 0,0 case

  x_off = [-1,0,1]
  y_off = [-1,0,1]

  x_off.each do |x|
    y_off.each do |y|

      temp_ic[:x] = ic[:x] + x
      temp_ic[:y] = ic[:y] + y

      if @im.inBounds(temp_ic) and @im[temp_ic] == @im[ic] then
        n += 1
      end

    end

  end

  temp_ic.recycle

  n

end

#compute_pixel_product(ic, pixel_offsets) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rimageanalysistools/skeletonizer.rb', line 110

def compute_pixel_product(ic, pixel_offsets)

  temp_ic = ImageCoordinate.cloneCoord(ic)

  pixel_offsets.each do |po|

    temp_ic[:x]= ic[:x] + po[0]
    temp_ic[:y]= ic[:y] + po[1]
    
    unless @im.inBounds(temp_ic) and @im[temp_ic] == @im[ic] then
      temp_ic.recycle
      return 0
    end

  end

  temp_ic.recycle

  1

end

#compute_t(ic) ⇒ Object



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
# File 'lib/rimageanalysistools/skeletonizer.rb', line 73

def compute_t(ic)

  temp_ic1 = ImageCoordinate.cloneCoord(ic)
  temp_ic2 = ImageCoordinate.cloneCoord(ic)

  pixel_offsets = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1]]

  t = 0

  0.upto(pixel_offsets.length - 2) do |i|
    
    p1 = pixel_offsets[i]
    p2 = pixel_offsets[i+1]

    temp_ic1[:x] = ic[:x] + p1[0]
    temp_ic2[:x] = ic[:x] + p2[0]

    temp_ic1[:y] = ic[:y] + p1[1]
    temp_ic2[:y] = ic[:y] + p2[1]

    v1 = 0
    v2 = 0

    v1 = 1 if @im.inBounds(temp_ic1) and @im[ic] == @im[temp_ic1]
    v2 = 1 if @im.inBounds(temp_ic2) and @im[ic] == @im[temp_ic2]

    t += 1 if v2 - v1 == 1

  end

  temp_ic1.recycle
  temp_ic2.recycle

  t

end

#do_iterationObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rimageanalysistools/skeletonizer.rb', line 184

def do_iteration

  flagged1 = []

  @im.each do |ic|

    next if @im[ic] == 0

    should_flag = pass1(ic)

    if should_flag then
      flagged1 << ImageCoordinate.cloneCoord(ic)
    end

  end

  flagged1.each do |ic|

    @im[ic]= 0

    ic.recycle

  end

  flagged2 = []

  @im.each do |ic|
    
    next if @im[ic] == 0

    should_flag = pass2(ic)

    if should_flag then

      flagged2 << ImageCoordinate.cloneCoord(ic)

    end

  end

  flagged2.each do |ic|
    
    @im[ic]= 0

    ic.recycle

  end

  changed = (not (flagged1.empty? and flagged2.empty?))

  changed

end

#pass1(ic) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rimageanalysistools/skeletonizer.rb', line 132

def pass1(ic)

  n = compute_n(ic)
  
  return false if n < 2 or n > 6

  t = compute_t(ic)

  return false unless t == 1

  pixels246 = [[0, -1], [1, 0], [0, 1]]

  p246 = compute_pixel_product(ic, pixels246)

  return false unless p246 == 0

  pixels468 = [[1, 0], [0, 1], [-1, 0]]

  p468 = compute_pixel_product(ic, pixels468)

  return false unless p468 == 0

  true

end

#pass2(ic) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rimageanalysistools/skeletonizer.rb', line 158

def pass2(ic)

  n = compute_n(ic)
  
  return false if n < 2 or n > 6

  t = compute_t(ic)

  return false unless t == 1

  pixels248 = [[0, -1], [1, 0], [-1, 0]]

  p248 = compute_pixel_product(ic, pixels248)

  return false unless p248 == 0

  pixels268 = [[0, -1], [0, 1], [-1, 0]]

  p268 = compute_pixel_product(ic, pixels268)

  return false unless p268 == 0

  true

end

#skeletonizeObject



238
239
240
241
242
243
244
# File 'lib/rimageanalysistools/skeletonizer.rb', line 238

def skeletonize

  while do_iteration do
    
  end

end