image.process\segment\segmentation_16f.mbt
100% · 47 / 47 points · 165 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像分割 16-bit/float 变体 Image segmentation 16-bit/float variants - 3
/// - 4
/// 16位分水岭/SLIC watershed_16 / slic_16 - 5
/// 16位/浮点分水岭/SLIC 分割 16-bit/float watershed/SLIC segmentation - 6
// 图像分割 16-bit/float 变体 - 7
// watershed / kmeans / SLIC 支持 Image16 / ImageF - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_seg(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn read_float_le_seg(data : Bytes, offset : Int32) -> Float { - 18●
let b0 = data[offset].to_int() - 19●
let b1 = data[offset + 1].to_int() - 20●
let b2 = data[offset + 2].to_int() - 21●
let b3 = data[offset + 3].to_int() - 22
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 23●
Float::reinterpret_from_int(bits) - 24
} - 25
- 26
///| - 27
fn img16_to_img8(img : @types.Image16) -> @types.Image { - 28●
let w = img.width - 29
let h = img.height - 30
let ch = img.channels - 31●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 32
for i in 0..<(w * h * ch) { - 33●
let v16 = read_u16_le_seg(img.data, i * 2) - 34●
data8[i] = (v16 / 257).to_byte() - 35
} - 36
@types.Image::{ - 37
width: w, - 38
height: h, - 39
channels: ch, - 40●
data: Bytes::from_array(data8), - 41
} - 42
} - 43
- 44
///| - 45
fn imgf_to_img8(img : @types.ImageF) -> @types.Image { - 46●
let w = img.width - 47
let h = img.height - 48
let ch = img.channels - 49●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 50
for i in 0..<(w * h * ch) { - 51●
let vf = read_float_le_seg(img.data, i * 4) - 52●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 53●
data8[i] = v8.to_byte() - 54
} - 55
@types.Image::{ - 56
width: w, - 57
height: h, - 58
channels: ch, - 59●
data: Bytes::from_array(data8), - 60
} - 61
} - 62
- 63
// ========== Watershed 16-bit ========== - 64
- 65
///| - 66
/// 16-bit Watershed 分割。 - 67
/// @see watershed 8-bit 版本,本函数为 Image16 变体 - 68
pub fn watershed_16( - 69
img : @types.Image16, - 70
markers : Array[Int32], - 71
) -> Array[Int32] raise @types.LoadError { - 72●
let img8 = img16_to_img8(img) - 73●
watershed(img8, markers) - 74
} - 75
- 76
///| - 77
/// 16-bit 自动 Watershed 分割。 - 78
/// @see watershed_auto 8-bit 版本,本函数为 Image16 变体 - 79
pub fn watershed_auto_16( - 80
img : @types.Image16, - 81
) -> (Array[Int32], Int32) raise @types.LoadError { - 82●
let img8 = img16_to_img8(img) - 83●
watershed_auto(img8) - 84
} - 85
- 86
// ========== Watershed float ========== - 87
- 88
///| - 89
/// float Watershed 分割。 - 90
/// @see watershed 8-bit 版本,本函数为 ImageF 变体 - 91
pub fn watershed_f( - 92
img : @types.ImageF, - 93
markers : Array[Int32], - 94
) -> Array[Int32] raise @types.LoadError { - 95●
let img8 = imgf_to_img8(img) - 96●
watershed(img8, markers) - 97
} - 98
- 99
///| - 100
/// float 自动 Watershed 分割。 - 101
/// @see watershed_auto 8-bit 版本,本函数为 ImageF 变体 - 102
pub fn watershed_auto_f( - 103
img : @types.ImageF, - 104
) -> (Array[Int32], Int32) raise @types.LoadError { - 105●
let img8 = imgf_to_img8(img) - 106●
watershed_auto(img8) - 107
} - 108
- 109
// ========== K-means 16-bit ========== - 110
- 111
///| - 112
/// 16-bit K-means 图像分割。 - 113
/// @see kmeans_segment 8-bit 版本,本函数为 Image16 变体 - 114
pub fn kmeans_segment_16( - 115
img : @types.Image16, - 116
k : Int32, - 117
max_iters~ : Int32 = 20, - 118
) -> (SegmentLabelImage, Array[SegmentRegion]) raise @types.LoadError { - 119●
let img8 = img16_to_img8(img) - 120●
kmeans_segment(img8, k, max_iters~) - 121
} - 122
- 123
// ========== K-means float ========== - 124
- 125
///| - 126
/// float K-means 图像分割。 - 127
/// @see kmeans_segment 8-bit 版本,本函数为 ImageF 变体 - 128
pub fn kmeans_segment_f( - 129
img : @types.ImageF, - 130
k : Int32, - 131
max_iters~ : Int32 = 20, - 132
) -> (SegmentLabelImage, Array[SegmentRegion]) raise @types.LoadError { - 133●
let img8 = imgf_to_img8(img) - 134●
kmeans_segment(img8, k, max_iters~) - 135
} - 136
- 137
// ========== SLIC 16-bit ========== - 138
- 139
///| - 140
/// 16-bit SLIC 超像素分割。 - 141
/// @see slic 8-bit 版本,本函数为 Image16 变体 - 142
pub fn slic_16( - 143
img : @types.Image16, - 144
k : Int32, - 145
m : Int32, - 146
max_iters : Int32, - 147
) -> SuperpixelResult raise @types.LoadError { - 148●
let img8 = img16_to_img8(img) - 149●
slic(img8, k, m, max_iters) - 150
} - 151
- 152
// ========== SLIC float ========== - 153
- 154
///| - 155
/// float SLIC 超像素分割。 - 156
/// @see slic 8-bit 版本,本函数为 ImageF 变体 - 157
pub fn slic_f( - 158
img : @types.ImageF, - 159
k : Int32, - 160
m : Int32, - 161
max_iters : Int32, - 162
) -> SuperpixelResult raise @types.LoadError { - 163●
let img8 = imgf_to_img8(img) - 164●
slic(img8, k, m, max_iters) - 165
}