image.process\segment\watershed.mbt
86.9% · 73 / 84 points · 258 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 分水岭 watershed - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 分水岭分割(纯 MoonBit) - 7
- 8
///| - 9
/// 分水岭分割。 - 10
/// 基于沉浸式分水岭算法(Vincent & Soille, 1991)。 - 11
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。 - 12
/// markers 为种子标记图(0=未标记,1..N=不同区域标记)。 - 13
/// 返回分割结果,每个像素的标签对应其所属区域。 - 14
pub fn watershed( - 15
img : @types.Image, - 16
markers : Array[Int32], - 17
) -> Array[Int32] raise @types.LoadError { - 18●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 19●
raise @types.LoadError::DecodeFailed("watershed 仅支持 channels=1/3/4") - 20
} - 21
let w = img.width - 22
let h = img.height - 23
let n = w * h - 24●
if markers.length() != n { - 25●
raise @types.LoadError::DecodeFailed( - 26
"markers 长度必须等于 width * height", - 27
) - 28
} - 29
if n == 0 { - 30○
return [] - 31
} - 32
// 转灰度 - 33●
let gray : Array[Int32] = Array::make(n, 0) - 34
let ch = img.channels - 35
for i in 0..<n { - 36●
if ch == 1 { - 37●
gray[i] = img.data[i].to_int() - 38
} else { - 39○
let r = img.data[i * ch].to_int() - 40○
let g = img.data[i * ch + 1].to_int() - 41○
let b = img.data[i * ch + 2].to_int() - 42
gray[i] = (r * 299 + g * 587 + b * 114) / 1000 - 43
} - 44
} - 45
// 复制 markers - 46●
let labels : Array[Int32] = Array::make(n, 0) - 47
for i in 0..<n { - 48●
labels[i] = markers[i] - 49
} - 50
// 沉浸式分水岭:按灰度值从低到高处理 - 51
// 创建像素索引按灰度值排序的列表 - 52●
let indices : Array[Int32] = Array::make(n, 0) - 53
for i in 0..<n { - 54●
indices[i] = i - 55
} - 56
// 按灰度值排序(基数排序,0-255) - 57●
let sorted : Array[Array[Int32]] = Array::make(256, []) - 58
for i in 0..<256 { - 59●
sorted[i] = [] - 60
} - 61
for i in 0..<n { - 62●
sorted[gray[i]].push(i) - 63
} - 64
// FIFO 队列 - 65
let wshed = -1 // 分水岭线标记 - 66
let mut current_label = 0 - 67
for i in 0..<n { - 68●
if labels[i] > current_label { - 69●
current_label = labels[i] - 70
} - 71
} - 72
// 按灰度级别处理 - 73
for level in 0..<256 { - 74●
let queue : Array[Int32] = [] - 75
// 将当前级别的已标记像素加入队列 - 76
for idx in sorted[level] { - 77
// 检查邻居是否有已标记的 - 78●
let x = idx % w - 79
let y = idx / w - 80
let mut has_labeled_neighbor = false - 81
for dy in -1..<=1 { - 82●
for dx in -1..<=1 { - 83●
if dx == 0 && dy == 0 { - 84●
continue - 85
} - 86
let nx = x + dx - 87
let ny = y + dy - 88
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 89●
let nidx = ny * w + nx - 90
if labels[nidx] > 0 || labels[nidx] == wshed { - 91●
has_labeled_neighbor = true - 92
break - 93
} - 94
} - 95
} - 96
if has_labeled_neighbor { - 97●
break - 98
} - 99
} - 100
if has_labeled_neighbor { - 101●
queue.push(idx) - 102
} - 103
} - 104
// BFS 扩展(head 指针队列,O(1) dequeue 避免 remove(0) 的 O(n) 移压) - 105
let mut q_head = 0 - 106●
while q_head < queue.length() { - 107●
let idx = queue[q_head] - 108
q_head += 1 - 109
let x = idx % w - 110
let y = idx / w - 111
let mut label = 0 - 112
let mut is_wshed = false - 113
// 查看邻居标签 - 114
for dy in -1..<=1 { - 115●
for dx in -1..<=1 { - 116●
if dx == 0 && dy == 0 { - 117●
continue - 118
} - 119
let nx = x + dx - 120
let ny = y + dy - 121
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 122●
let nidx = ny * w + nx - 123
let nl = labels[nidx] - 124
if nl > 0 { - 125●
if label == 0 { - 126●
label = nl - 127●
} else if label != nl { - 128●
is_wshed = true - 129
} - 130
} - 131
} - 132
} - 133
} - 134
if is_wshed { - 135●
labels[idx] = wshed - 136●
} else if label > 0 { - 137●
labels[idx] = label - 138
// 将同级别未标记邻居加入队列 - 139
for dy in -1..<=1 { - 140●
for dx in -1..<=1 { - 141●
if dx == 0 && dy == 0 { - 142●
continue - 143
} - 144
let nx = x + dx - 145
let ny = y + dy - 146
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 147●
let nidx = ny * w + nx - 148
if labels[nidx] == 0 && gray[nidx] == level { - 149●
labels[nidx] = -2 // 临时标记,在队列中 - 150●
queue.push(nidx) - 151
} - 152
} - 153
} - 154
} - 155
} - 156
} - 157
// 清除临时标记 - 158
for idx in sorted[level] { - 159●
if labels[idx] == -2 { - 160○
labels[idx] = 0 - 161
} - 162
} - 163
} - 164
labels - 165
} - 166
- 167
///| - 168
/// 自动分水岭分割(使用局部最小值作为种子点)。 - 169
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。 - 170
/// 返回标签图和区域数量。 - 171
pub fn watershed_auto( - 172
img : @types.Image, - 173
) -> (Array[Int32], Int32) raise @types.LoadError { - 174●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 175●
raise @types.LoadError::DecodeFailed( - 176
"watershed_auto 仅支持 channels=1/3/4", - 177
) - 178
} - 179
let w = img.width - 180
let h = img.height - 181
let n = w * h - 182
if n == 0 { - 183○
return ([], 0) - 184
} - 185
// 转灰度 - 186●
let gray : Array[Int32] = Array::make(n, 0) - 187
let ch = img.channels - 188
for i in 0..<n { - 189●
if ch == 1 { - 190●
gray[i] = img.data[i].to_int() - 191
} else { - 192○
let r = img.data[i * ch].to_int() - 193○
let g = img.data[i * ch + 1].to_int() - 194○
let b = img.data[i * ch + 2].to_int() - 195
gray[i] = (r * 299 + g * 587 + b * 114) / 1000 - 196
} - 197
} - 198
// 寻找局部最小值作为种子点 - 199●
let markers : Array[Int32] = Array::make(n, 0) - 200
let mut current_label = 0 - 201
for y in 0..<h { - 202●
for x in 0..<w { - 203●
let idx = y * w + x - 204
let val = gray[idx] - 205
let mut is_min = true - 206
for dy in -1..<=1 { - 207●
for dx in -1..<=1 { - 208●
if dx == 0 && dy == 0 { - 209●
continue - 210
} - 211
let nx = x + dx - 212
let ny = y + dy - 213
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 214●
let nidx = ny * w + nx - 215
if gray[nidx] < val { - 216●
is_min = false - 217
break - 218
} - 219
} - 220
} - 221
if !is_min { - 222●
break - 223
} - 224
} - 225
if is_min && markers[idx] == 0 { - 226●
current_label += 1 - 227
// BFS 标记连通的等值像素(head 指针队列,避免 remove 开销) - 228
let stack : Array[Int32] = [idx] - 229
let mut sp = 0 - 230
markers[idx] = current_label - 231●
while sp < stack.length() { - 232●
let pidx = stack[sp] - 233
sp += 1 - 234
let px = pidx % w - 235
let py = pidx / w - 236
for dy in -1..<=1 { - 237●
for dx in -1..<=1 { - 238●
if dx == 0 && dy == 0 { - 239●
continue - 240
} - 241
let nx = px + dx - 242
let ny = py + dy - 243
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 244●
let nidx = ny * w + nx - 245
if markers[nidx] == 0 && gray[nidx] == val { - 246●
markers[nidx] = current_label - 247●
stack.push(nidx) - 248
} - 249
} - 250
} - 251
} - 252
} - 253
} - 254
} - 255
} - 256●
let labels = watershed(img, markers) - 257
(labels, current_label) - 258
}