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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
157
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
183
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// 霍夫圆检测 16-bit/float 变体

///|
/// 霍夫圆检测 16-bit 版本。
pub fn hough_circles_16(
  img : @types.Image16,
  dp? : Int = 1,
  min_dist? : Int = 10,
  param1? : Int = 100,
  param2? : Int = 30,
  min_radius? : Int = 0,
  max_radius? : Int = 0,
) -> Array[Circle] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: 仅支持 channels=1/3/4",
    )
  }
  let expected_size = img.width * img.height * img.channels * 2
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if dp < 1 {
    raise @types.LoadError::DecodeFailed("hough_circles_16: dp必须 >= 1")
  }
  if min_dist < 1 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: min_dist必须 >= 1",
    )
  }
  if param1 <= 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: param1必须大于0",
    )
  }
  if param2 <= 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: param2必须大于0",
    )
  }
  if min_radius < 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: min_radius必须 >= 0",
    )
  }
  if max_radius < 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_16: max_radius必须 >= 0",
    )
  }
  let w = img.width
  let h = img.height
  let n = w * h
  if n == 0 {
    return []
  }
  ignore(dp)
  let ch = img.channels
  let gray : Array[Int] = Array::make(n, 0)
  for i in 0..<n {
    gray[i] = if ch == 1 {
      img.data[i * 2].to_int() | (img.data[i * 2 + 1].to_int() << 8)
    } else {
      let r = img.data[i * ch * 2].to_int() |
        (img.data[i * ch * 2 + 1].to_int() << 8)
      let g = img.data[(i * ch + 1) * 2].to_int() |
        (img.data[(i * ch + 1) * 2 + 1].to_int() << 8)
      let b = img.data[(i * ch + 2) * 2].to_int() |
        (img.data[(i * ch + 2) * 2 + 1].to_int() << 8)
      (r * 299 + g * 587 + b * 114) / 1000
    }
  }
  let gx : Array[Int] = Array::make(n, 0)
  let gy : Array[Int] = Array::make(n, 0)
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      gx[idx] = gray[(y - 1) * w + (x + 1)] +
        2 * gray[y * w + (x + 1)] +
        gray[(y + 1) * w + (x + 1)] -
        gray[(y - 1) * w + (x - 1)] -
        2 * gray[y * w + (x - 1)] -
        gray[(y + 1) * w + (x - 1)]
      gy[idx] = gray[(y + 1) * w + (x - 1)] +
        2 * gray[(y + 1) * w + x] +
        gray[(y + 1) * w + (x + 1)] -
        gray[(y - 1) * w + (x - 1)] -
        2 * gray[(y - 1) * w + x] -
        gray[(y - 1) * w + (x + 1)]
    }
  }
  let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 }
  let r_min = min_radius
  if r_max <= r_min {
    return []
  }
  let acc_w = w
  let acc_h = h
  let num_r = r_max - r_min + 1
  let acc : Array[Int] = Array::make(acc_w * acc_h * num_r, 0)
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx]
      if mag < param1 * param1 {
        continue
      }
      let dx = gx[idx]
      let dy = gy[idx]
      let mag_sqrt = Float::from_int(mag).sqrt().to_int()
      if mag_sqrt == 0 {
        continue
      }
      for r in r_min..<=r_max {
        let cx_pos = x + dx * r / mag_sqrt
        let cy_pos = y + dy * r / mag_sqrt
        let cx_neg = x - dx * r / mag_sqrt
        let cy_neg = y - dy * r / mag_sqrt
        if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h {
          acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1
        }
        if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h {
          acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1
        }
      }
    }
  }
  let candidates : Array[Circle] = []
  for r in r_min..<=r_max {
    for y in 0..<acc_h {
      for x in 0..<acc_w {
        let votes = acc[(y * acc_w + x) * num_r + (r - r_min)]
        if votes >= param2 {
          candidates.push(Circle::{ x, y, radius: r, votes })
        }
      }
    }
  }
  candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  let result : Array[Circle] = []
  for c in candidates {
    let mut ok = true
    for r in result {
      let dx = c.x - r.x
      let dy = c.y - r.y
      if dx * dx + dy * dy < min_dist * min_dist {
        ok = false
        break
      }
    }
    if ok {
      result.push(c)
    }
  }
  result
}

///|
/// 霍夫圆检测 float 版本。
pub fn hough_circles_f(
  img : @types.ImageF,
  dp? : Int = 1,
  min_dist? : Int = 10,
  param1? : Int = 100,
  param2? : Int = 30,
  min_radius? : Int = 0,
  max_radius? : Int = 0,
) -> Array[Circle] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_f: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_f: 仅支持 channels=1/3/4",
    )
  }
  let expected_size = img.width * img.height * img.channels * 4
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_f: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if dp < 1 {
    raise @types.LoadError::DecodeFailed("hough_circles_f: dp必须 >= 1")
  }
  if min_dist < 1 {
    raise @types.LoadError::DecodeFailed("hough_circles_f: min_dist必须 >= 1")
  }
  if param1 <= 0 {
    raise @types.LoadError::DecodeFailed("hough_circles_f: param1必须大于0")
  }
  if param2 <= 0 {
    raise @types.LoadError::DecodeFailed("hough_circles_f: param2必须大于0")
  }
  if min_radius < 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_f: min_radius必须 >= 0",
    )
  }
  if max_radius < 0 {
    raise @types.LoadError::DecodeFailed(
      "hough_circles_f: max_radius必须 >= 0",
    )
  }
  let w = img.width
  let h = img.height
  let n = w * h
  if n == 0 {
    return []
  }
  ignore(dp)
  let ch = img.channels
  let gray : Array[Float] = Array::make(n, 0.0F)
  for i in 0..<n {
    gray[i] = if ch == 1 {
      let bits = img.data[i * 4].to_int() |
        (img.data[i * 4 + 1].to_int() << 8) |
        (img.data[i * 4 + 2].to_int() << 16) |
        (img.data[i * 4 + 3].to_int() << 24)
      Float::reinterpret_from_int(bits)
    } else {
      let bits_r = img.data[i * ch * 4].to_int() |
        (img.data[i * ch * 4 + 1].to_int() << 8) |
        (img.data[i * ch * 4 + 2].to_int() << 16) |
        (img.data[i * ch * 4 + 3].to_int() << 24)
      let bits_g = img.data[(i * ch + 1) * 4].to_int() |
        (img.data[(i * ch + 1) * 4 + 1].to_int() << 8) |
        (img.data[(i * ch + 1) * 4 + 2].to_int() << 16) |
        (img.data[(i * ch + 1) * 4 + 3].to_int() << 24)
      let bits_b = img.data[(i * ch + 2) * 4].to_int() |
        (img.data[(i * ch + 2) * 4 + 1].to_int() << 8) |
        (img.data[(i * ch + 2) * 4 + 2].to_int() << 16) |
        (img.data[(i * ch + 2) * 4 + 3].to_int() << 24)
      Float::reinterpret_from_int(bits_r) * 0.299F +
      Float::reinterpret_from_int(bits_g) * 0.587F +
      Float::reinterpret_from_int(bits_b) * 0.114F
    }
  }
  let gx : Array[Float] = Array::make(n, 0.0F)
  let gy : Array[Float] = Array::make(n, 0.0F)
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      gx[idx] = gray[(y - 1) * w + (x + 1)] +
        2.0F * gray[y * w + (x + 1)] +
        gray[(y + 1) * w + (x + 1)] -
        gray[(y - 1) * w + (x - 1)] -
        2.0F * gray[y * w + (x - 1)] -
        gray[(y + 1) * w + (x - 1)]
      gy[idx] = gray[(y + 1) * w + (x - 1)] +
        2.0F * gray[(y + 1) * w + x] +
        gray[(y + 1) * w + (x + 1)] -
        gray[(y - 1) * w + (x - 1)] -
        2.0F * gray[(y - 1) * w + x] -
        gray[(y - 1) * w + (x + 1)]
    }
  }
  let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 }
  let r_min = min_radius
  if r_max <= r_min {
    return []
  }
  let acc_w = w
  let acc_h = h
  let num_r = r_max - r_min + 1
  let acc : Array[Int] = Array::make(acc_w * acc_h * num_r, 0)
  let param1_f = Float::from_int(param1) / 255.0F
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx]
      if mag < param1_f * param1_f {
        continue
      }
      let dx = gx[idx]
      let dy = gy[idx]
      let mag_sqrt = mag.sqrt()
      if mag_sqrt == 0.0F {
        continue
      }
      for r in r_min..<=r_max {
        let cx_pos = x + (dx * Float::from_int(r) / mag_sqrt).to_int()
        let cy_pos = y + (dy * Float::from_int(r) / mag_sqrt).to_int()
        let cx_neg = x - (dx * Float::from_int(r) / mag_sqrt).to_int()
        let cy_neg = y - (dy * Float::from_int(r) / mag_sqrt).to_int()
        if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h {
          acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1
        }
        if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h {
          acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1
        }
      }
    }
  }
  let candidates : Array[Circle] = []
  for r in r_min..<=r_max {
    for y in 0..<acc_h {
      for x in 0..<acc_w {
        let votes = acc[(y * acc_w + x) * num_r + (r - r_min)]
        if votes >= param2 {
          candidates.push(Circle::{ x, y, radius: r, votes })
        }
      }
    }
  }
  candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) })
  let result : Array[Circle] = []
  for c in candidates {
    let mut ok = true
    for r in result {
      let dx = c.x - r.x
      let dy = c.y - r.y
      if dx * dx + dy * dy < min_dist * min_dist {
        ok = false
        break
      }
    }
    if ok {
      result.push(c)
    }
  }
  result
}