image.domain\medical\medical_image.mbt

93.7% · 134 / 143 points · 308 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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