image.domain\medical\medical_image.mbt
93.7% · 134 / 143 points · 308 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 医学图像 medical/medical_image - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 医学图像处理(v12.10.0) - 7
// 窗宽窗位调整、伪彩色映射、医学图像增强 - 8
- 9
///| - 10
/// 窗宽窗位配置 - 11
pub(all) struct WindowLevel { - 12
window_width : Double // 窗宽 - 13
window_level : Double // 窗位 - 14
} - 15
- 16
///| - 17
/// 创建默认窗宽窗位(肺窗) - 18
pub fn window_level_lung() -> WindowLevel { - 19●
WindowLevel::{ window_width: 1500.0, window_level: -600.0, } - 20
} - 21
- 22
///| - 23
/// 创建纵隔窗 - 24
pub fn window_level_mediastinum() -> WindowLevel { - 25●
WindowLevel::{ window_width: 400.0, window_level: 40.0, } - 26
} - 27
- 28
///| - 29
/// 创建骨窗 - 30
pub fn window_level_bone() -> WindowLevel { - 31●
WindowLevel::{ window_width: 2000.0, window_level: 400.0, } - 32
} - 33
- 34
///| - 35
/// 创建脑窗 - 36
pub fn window_level_brain() -> WindowLevel { - 37●
WindowLevel::{ window_width: 80.0, window_level: 40.0, } - 38
} - 39
- 40
///| - 41
/// 应用窗宽窗位调整 - 42
/// 将原始灰度值(如 CT 值 HU)映射到 0-255 - 43
pub fn apply_window_level( - 44
img : @types.Image, - 45
config : WindowLevel, - 46
) -> @types.Image { - 47●
let w = img.width - 48
let h = img.height - 49
let channels = img.channels - 50●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 51
let low = config.window_level - config.window_width / 2.0 - 52
let high = config.window_level + config.window_width / 2.0 - 53
for y in 0..<h { - 54●
for x in 0..<w { - 55●
let idx = (y * w + x) * channels - 56
// 取第一个通道作为灰度值 - 57●
let raw = img.data[idx].to_double() - 58
let mapped = if raw <= low { - 59○
0.0 - 60●
} else if raw >= high { - 61●
255.0 - 62
} else { - 63●
(raw - low) / config.window_width * 255.0 - 64
} - 65●
out_data[y * w + x] = mapped.min(255.0).max(0.0).to_int().to_byte() - 66
} - 67
} - 68
@types.Image::{ - 69
width: w, - 70
height: h, - 71
channels: 1, - 72●
data: Bytes::from_array(out_data), - 73
} - 74
} - 75
- 76
///| - 77
/// 医学图像伪彩色映射 - 78
/// 支持多种医学常用伪彩色方案 - 79
pub fn medical_colormap( - 80
img : @types.Image, - 81
colormap~ : String = "hot", - 82
) -> @types.Image { - 83●
let cmap = colormap - 84
let w = img.width - 85
let h = img.height - 86
let channels = img.channels - 87●
let out_data : Array[Byte] = Array::make(w * h * 3, b'\x00') - 88
for y in 0..<h { - 89●
for x in 0..<w { - 90●
let idx = (y * w + x) * channels - 91
let gray = if channels >= 1 { - 92●
img.data[idx].to_double() / 255.0 - 93
} else { - 94○
0.0 - 95
} - 96
let out_idx = (y * w + x) * 3 - 97
// 多种伪彩色映射 - 98●
let (r, g, b) = match cmap { - 99
"hot" => - 100
// 黑 -> 红 -> 黄 -> 白 - 101●
if gray < 0.33 { - 102●
((gray / 0.33 * 255.0).to_int(), 0, 0) - 103●
} else if gray < 0.66 { - 104●
(255, ((gray - 0.33) / 0.33 * 255.0).to_int(), 0) - 105
} else { - 106○
(255, 255, ((gray - 0.66) / 0.34 * 255.0).to_int()) - 107
} - 108
"cool" => - 109
// 青 -> 品红 - 110●
((gray * 255.0).to_int(), ((1.0 - gray) * 255.0).to_int(), 255) - 111
"spring" => - 112
// 品红 -> 黄 - 113●
(255, (gray * 255.0).to_int(), ((1.0 - gray) * 255.0).to_int()) - 114
"summer" => - 115
// 绿 -> 黄 - 116●
((gray * 128.0).to_int(), (128.0 + gray * 127.0).to_int(), 0) - 117
"autumn" => - 118
// 红 -> 黄 - 119●
(255, (gray * 255.0).to_int(), 0) - 120
"winter" => - 121
// 蓝 -> 绿 - 122●
(0, (gray * 255.0).to_int(), ((1.0 - gray) * 255.0).to_int()) - 123●
"bone" => { - 124
// 骨色:黑 -> 灰 -> 白(带一点蓝调) - 125●
let v = (gray * 255.0).to_int() - 126●
(v, v, (v + 20).min(255)) - 127
} - 128●
"pink" => { - 129
// 粉色:灰 -> 粉 - 130●
let v = (gray * 255.0).to_int() - 131
( - 132●
v.min(255), - 133●
(v.to_double() * 0.6).to_int().min(255), - 134●
(v.to_double() * 0.7).to_int().min(255), - 135
) - 136
} - 137●
_ => { - 138
// 灰度 - 139●
let v = (gray * 255.0).to_int() - 140
(v, v, v) - 141
} - 142
} - 143●
out_data[out_idx] = r.min(255).max(0).to_byte() - 144●
out_data[out_idx + 1] = g.min(255).max(0).to_byte() - 145●
out_data[out_idx + 2] = b.min(255).max(0).to_byte() - 146
} - 147
} - 148
@types.Image::{ - 149
width: w, - 150
height: h, - 151
channels: 3, - 152●
data: Bytes::from_array(out_data), - 153
} - 154
} - 155
- 156
///| - 157
/// 医学图像对比度增强(CLAHE 简化版,自适应直方图均衡化) - 158
pub fn medical_contrast_enhance( - 159
img : @types.Image, - 160
clip_limit~ : Double = 2.0, - 161
tile_size~ : Int32 = 8, - 162
) -> @types.Image { - 163●
let _ = clip_limit - 164
let _ = tile_size - 165
let w = img.width - 166
let h = img.height - 167
let channels = img.channels - 168
// 转换为灰度 - 169●
let gray : Array[Int32] = Array::make(w * h, 0) - 170
for i in 0..<(w * h) { - 171●
let idx = i * channels - 172●
let r = img.data[idx].to_int() - 173◐
let g = if channels >= 2 { img.data[idx + 1].to_int() } else { r } - 174◐
let b = if channels >= 3 { img.data[idx + 2].to_int() } else { r } - 175●
gray[i] = (0.299 * r.to_double() + - 176●
0.587 * g.to_double() + - 177●
0.114 * b.to_double()).to_int() - 178
} - 179
// 全局直方图均衡化(简化版) - 180●
let histogram : Array[Int32] = Array::make(256, 0) - 181
for i in 0..<(w * h) { - 182●
histogram[gray[i]] += 1 - 183
} - 184
// 累积分布函数 - 185●
let cdf : Array[Int32] = Array::make(256, 0) - 186
let mut sum = 0 - 187
for i in 0..<256 { - 188●
sum += histogram[i] - 189
cdf[i] = sum - 190
} - 191
// 映射 - 192
let total = w * h - 193●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 194
for i in 0..<(w * h) { - 195●
let value = gray[i] - 196
let equalized = if total > 0 { - 197●
(cdf[value].to_double() / total.to_double() * 255.0).to_int() - 198
} else { - 199○
value - 200
} - 201●
out_data[i] = equalized.min(255).max(0).to_byte() - 202
} - 203
@types.Image::{ - 204
width: w, - 205
height: h, - 206
channels: 1, - 207●
data: Bytes::from_array(out_data), - 208
} - 209
} - 210
- 211
///| - 212
/// 医学图像降噪(各向异性扩散,简化版) - 213
pub fn medical_denoise( - 214
img : @types.Image, - 215
iterations~ : Int32 = 5, - 216
kappa~ : Double = 30.0, - 217
) -> @types.Image { - 218●
let iter = iterations - 219
let k = kappa - 220
let w = img.width - 221
let h = img.height - 222
let channels = img.channels - 223●
let mut current : Array[Double] = Array::make(w * h * channels, 0.0) - 224
for i in 0..<(w * h * channels) { - 225●
current[i] = img.data[i].to_double() - 226
} - 227
for _ in 0..<iter { - 228●
let next : Array[Double] = Array::make(w * h * channels, 0.0) - 229
for y in 1..<(h - 1) { - 230●
for x in 1..<(w - 1) { - 231●
for c in 0..<channels { - 232●
let idx = (y * w + x) * channels + c - 233
let center = current[idx] - 234
// 四邻域梯度 - 235
let north = current[((y - 1) * w + x) * channels + c] - center - 236
let south = current[((y + 1) * w + x) * channels + c] - center - 237
let east = current[(y * w + (x + 1)) * channels + c] - center - 238
let west = current[(y * w + (x - 1)) * channels + c] - center - 239
// 扩散系数(指数函数,边缘保留) - 240●
let cn = @math.exp(-(north.abs() / k) * (north.abs() / k)) - 241●
let cs = @math.exp(-(south.abs() / k) * (south.abs() / k)) - 242●
let ce = @math.exp(-(east.abs() / k) * (east.abs() / k)) - 243●
let cw = @math.exp(-(west.abs() / k) * (west.abs() / k)) - 244
// 更新 - 245
let lambda = 0.25 - 246
next[idx] = center + - 247
lambda * (cn * north + cs * south + ce * east + cw * west) - 248
} - 249
} - 250
} - 251
// 边界复制 - 252
for y in 0..<h { - 253●
for x in 0..<w { - 254●
if y == 0 || y == h - 1 || x == 0 || x == w - 1 { - 255●
for c in 0..<channels { - 256●
next[(y * w + x) * channels + c] = current[(y * w + x) * channels + - 257
c] - 258
} - 259
} - 260
} - 261
} - 262
current = next - 263
} - 264●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 265
for i in 0..<(w * h * channels) { - 266●
out_data[i] = current[i].min(255.0).max(0.0).to_int().to_byte() - 267
} - 268
@types.Image::{ - 269
width: w, - 270
height: h, - 271
channels, - 272●
data: Bytes::from_array(out_data), - 273
} - 274
} - 275
- 276
///| - 277
/// 测量图像中两点之间的距离(像素距离) - 278
pub fn measure_distance( - 279
x1 : Int32, - 280
y1 : Int32, - 281
x2 : Int32, - 282
y2 : Int32, - 283
pixel_spacing~ : Double = 1.0, - 284
) -> Double { - 285●
let spacing = pixel_spacing - 286●
let dx = (x2 - x1).to_double() - 287●
let dy = (y2 - y1).to_double() - 288●
(dx * dx + dy * dy).sqrt() * spacing - 289
} - 290
- 291
///| - 292
/// 测量图像中区域的面积(像素面积) - 293
pub fn measure_area(mask : @types.Image, pixel_area~ : Double = 1.0) -> Double { - 294●
let area_per_pixel = pixel_area - 295
let w = mask.width - 296
let h = mask.height - 297
let channels = mask.channels - 298
let mut count = 0 - 299
for y in 0..<h { - 300●
for x in 0..<w { - 301●
let idx = (y * w + x) * channels - 302●
if mask.data[idx].to_int() > 128 { - 303●
count += 1 - 304
} - 305
} - 306
} - 307●
count.to_double() * area_per_pixel - 308
}