image.domain\retrieval\content_based_retrieval.mbt

93.2% · 150 / 161 points · 696 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/// # 基于内容检索 retrieval/content_based_retrieval
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像检索增强(v22.0.0)
  7. 7// 基于内容的图像检索、特征提取、相似度计算、图像索引
  8. 8
  9. 9///|
  10. 10/// 图像特征类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq)
  13. 13#non_exhaustive
  14. 14pub(all) enum FeatureType {
  15. 15 ColorHistogram
  16. 16 ColorCorrelogram
  17. 17 Texture
  18. 18 Shape
  19. 19 SIFT
  20. 20 SURF
  21. 21 ORB
  22. 22 HOG
  23. 23 LBP
  24. 24 Gabor
  25. 25 DeepFeature
  26. 26 Combined
  27. 27 Unknown
  28. 28}
  29. 29
  30. 30///|
  31. 31pub extend FeatureType with Eq::{not_equal, equal}
  32. 32
  33. 33///|
  34. 34/// 图像特征
  35. 35pub(all) struct CBIRImageFeature {
  36. 36 feature_type : FeatureType
  37. 37 feature_vector : Array[Double]
  38. 38 feature_length : Int32
  39. 39 image_id : String
  40. 40 image_path : String
  41. 41 width : Int32
  42. 42 height : Int32
  43. 43 extraction_time_ms : Double
  44. 44}
  45. 45
  46. 46///|
  47. 47/// 检索结果
  48. 48#derive(Eq, Default, @debug.Debug)
  49. 49pub(all) struct CBIRRetrievalResult {
  50. 50 image_id : String
  51. 51 image_path : String
  52. 52 similarity : Double
  53. 53 distance : Double
  54. 54 rank : Int32
  55. 55 width : Int32
  56. 56 height : Int32
  57. 57}
  58. 58
  59. 59///|
  60. 60pub extend CBIRRetrievalResult with @debug.Debug::{to_repr}
  61. 61
  62. 62///|
  63. 63pub extend CBIRRetrievalResult with Eq::{not_equal, equal}
  64. 64
  65. 65///|
  66. 66pub extend CBIRRetrievalResult with Default::{default}
  67. 67
  68. 68///|
  69. 69/// 检索结果列表
  70. 70#derive(Eq, Default, @debug.Debug)
  71. 71pub(all) struct CBIRRetrievalResultList {
  72. 72 results : Array[CBIRRetrievalResult]
  73. 73 num_results : Int32
  74. 74 query_image_id : String
  75. 75 retrieval_time_ms : Double
  76. 76 total_images_in_index : Int32
  77. 77}
  78. 78
  79. 79///|
  80. 80pub extend CBIRRetrievalResultList with @debug.Debug::{to_repr}
  81. 81
  82. 82///|
  83. 83pub extend CBIRRetrievalResultList with Eq::{not_equal, equal}
  84. 84
  85. 85///|
  86. 86pub extend CBIRRetrievalResultList with Default::{default}
  87. 87
  88. 88///|
  89. 89/// 图像索引
  90. 90pub(all) struct CBIRImageIndex {
  91. 91 features : Array[CBIRImageFeature]
  92. 92 num_images : Int32
  93. 93 index_type : String
  94. 94 built : Bool
  95. 95 build_time_ms : Double
  96. 96}
  97. 97
  98. 98///|
  99. 99/// 相似度度量类型
  100. 100/// `#non_exhaustive` 表示未来可能添加新构造器
  101. 101#derive(Eq)
  102. 102#non_exhaustive
  103. 103pub(all) enum SimilarityMetric {
  104. 104 Cosine
  105. 105 Euclidean
  106. 106 Manhattan
  107. 107 Chebyshev
  108. 108 Correlation
  109. 109 Intersection
  110. 110 ChiSquare
  111. 111 Hellinger
  112. 112 JensenShannon
  113. 113 Unknown
  114. 114}
  115. 115
  116. 116///|
  117. 117pub extend SimilarityMetric with Eq::{not_equal, equal}
  118. 118
  119. 119///|
  120. 120/// 颜色直方图配置
  121. 121#derive(Eq, Default, @debug.Debug)
  122. 122pub(all) struct ColorHistogramConfig {
  123. 123 num_bins : Int32
  124. 124 color_space : String // "RGB" / "HSV" / "Gray"
  125. 125 normalize : Bool
  126. 126 cumulative : Bool
  127. 127}
  128. 128
  129. 129///|
  130. 130pub extend ColorHistogramConfig with @debug.Debug::{to_repr}
  131. 131
  132. 132///|
  133. 133pub extend ColorHistogramConfig with Eq::{not_equal, equal}
  134. 134
  135. 135///|
  136. 136pub extend ColorHistogramConfig with Default::{default}
  137. 137
  138. 138///|
  139. 139/// LBP 特征配置
  140. 140#derive(Eq, Default, @debug.Debug)
  141. 141pub(all) struct LBPConfig {
  142. 142 radius : Int32
  143. 143 num_points : Int32
  144. 144 uniform : Bool
  145. 145 rotation_invariant : Bool
  146. 146}
  147. 147
  148. 148///|
  149. 149pub extend LBPConfig with @debug.Debug::{to_repr}
  150. 150
  151. 151///|
  152. 152pub extend LBPConfig with Eq::{not_equal, equal}
  153. 153
  154. 154///|
  155. 155pub extend LBPConfig with Default::{default}
  156. 156
  157. 157///|
  158. 158/// HOG 特征配置
  159. 159#derive(Eq, Default, @debug.Debug)
  160. 160pub(all) struct HOGConfig {
  161. 161 cell_size : Int32
  162. 162 block_size : Int32
  163. 163 num_bins : Int32
  164. 164 normalize : Bool
  165. 165}
  166. 166
  167. 167///|
  168. 168pub extend HOGConfig with @debug.Debug::{to_repr}
  169. 169
  170. 170///|
  171. 171pub extend HOGConfig with Eq::{not_equal, equal}
  172. 172
  173. 173///|
  174. 174pub extend HOGConfig with Default::{default}
  175. 175
  176. 176///|
  177. 177/// Gabor 特征配置
  178. 178#derive(Eq, Default, @debug.Debug)
  179. 179pub(all) struct GaborConfig {
  180. 180 num_scales : Int32
  181. 181 num_orientations : Int32
  182. 182 wavelength : Double
  183. 183 aspect_ratio : Double
  184. 184}
  185. 185
  186. 186///|
  187. 187pub extend GaborConfig with @debug.Debug::{to_repr}
  188. 188
  189. 189///|
  190. 190pub extend GaborConfig with Eq::{not_equal, equal}
  191. 191
  192. 192///|
  193. 193pub extend GaborConfig with Default::{default}
  194. 194
  195. 195///|
  196. 196/// 创建默认颜色直方图配置
  197. 197pub fn create_default_color_histogram_config() -> ColorHistogramConfig {
  198. 198● ColorHistogramConfig::{
  199. 199 num_bins: 256,
  200. 200 color_space: "Gray",
  201. 201 normalize: true,
  202. 202 cumulative: false,
  203. 203 }
  204. 204}
  205. 205
  206. 206///|
  207. 207/// 创建默认 LBP 配置
  208. 208pub fn create_default_lbp_config() -> LBPConfig {
  209. 209● LBPConfig::{
  210. 210 radius: 1,
  211. 211 num_points: 8,
  212. 212 uniform: true,
  213. 213 rotation_invariant: false,
  214. 214 }
  215. 215}
  216. 216
  217. 217///|
  218. 218/// 创建默认 HOG 配置
  219. 219pub fn create_default_hog_config() -> HOGConfig {
  220. 220● HOGConfig::{ cell_size: 8, block_size: 2, num_bins: 9, normalize: true, }
  221. 221}
  222. 222
  223. 223///|
  224. 224/// 提取颜色直方图特征
  225. 225pub fn extract_color_histogram(
  226. 226 img : @types.Image,
  227. 227 config : ColorHistogramConfig,
  228. 228) -> CBIRImageFeature {
  229. 229● let w = img.width
  230. 230 let h = img.height
  231. 231 let total = w * h
  232. 232 let histogram : Array[Double] = []
  233. 233 for _ in 0..<config.num_bins {
  234. 234● histogram.push(0.0)
  235. 235 }
  236. 236
  237. 237 for i in 0..<total {
  238. 238● let pixel = img.data[i].to_int()
  239. 239 let bin = if config.num_bins > 0 {
  240. 240● pixel * config.num_bins / 256
  241. 241 } else {
  242. 242○ 0
  243. 243 }
  244. 244 if bin >= 0 && bin < config.num_bins {
  245. 245● histogram[bin] += 1.0
  246. 246 }
  247. 247 }
  248. 248
  249. 249 // 归一化
  250. 250 if config.normalize && total > 0 {
  251. 251● for i in 0..<config.num_bins {
  252. 252● histogram[i] /= total.to_double()
  253. 253 }
  254. 254 }
  255. 255
  256. 256 CBIRImageFeature::{
  257. 257 feature_type: FeatureType::ColorHistogram,
  258. 258 feature_vector: histogram,
  259. 259 feature_length: config.num_bins,
  260. 260 image_id: "",
  261. 261 image_path: "",
  262. 262 width: w,
  263. 263 height: h,
  264. 264 extraction_time_ms: 0.0,
  265. 265 }
  266. 266}
  267. 267
  268. 268///|
  269. 269/// 提取 LBP 特征
  270. 270pub fn extract_lbp_feature(
  271. 271 img : @types.Image,
  272. 272 config : LBPConfig,
  273. 273) -> CBIRImageFeature {
  274. 274● ignore(config)
  275. 275 let w = img.width
  276. 276 let h = img.height
  277. 277 let _ = w * h
  278. 278 let num_bins = 256
  279. 279 let histogram : Array[Double] = []
  280. 280 for _ in 0..<num_bins {
  281. 281● histogram.push(0.0)
  282. 282 }
  283. 283
  284. 284 for y in 1..<(h - 1) {
  285. 285● for x in 1..<(w - 1) {
  286. 286● let center = img.data[y * w + x].to_int()
  287. 287 let mut lbp_value = 0
  288. 288 let neighbors = [
  289. 289● img.data[(y - 1) * w + (x - 1)].to_int(),
  290. 290● img.data[(y - 1) * w + x].to_int(),
  291. 291● img.data[(y - 1) * w + (x + 1)].to_int(),
  292. 292● img.data[y * w + (x + 1)].to_int(),
  293. 293● img.data[(y + 1) * w + (x + 1)].to_int(),
  294. 294● img.data[(y + 1) * w + x].to_int(),
  295. 295● img.data[(y + 1) * w + (x - 1)].to_int(),
  296. 296● img.data[y * w + (x - 1)].to_int(),
  297. 297 ]
  298. 298 for i in 0..<8 {
  299. 299● if neighbors[i] >= center {
  300. 300● lbp_value = lbp_value | (1 << i)
  301. 301 }
  302. 302 }
  303. 303 histogram[lbp_value] += 1.0
  304. 304 }
  305. 305 }
  306. 306
  307. 307 // 归一化
  308. 308 let valid_pixels = (w - 2) * (h - 2)
  309. 309 if valid_pixels > 0 {
  310. 310● for i in 0..<num_bins {
  311. 311● histogram[i] /= valid_pixels.to_double()
  312. 312 }
  313. 313 }
  314. 314
  315. 315 CBIRImageFeature::{
  316. 316 feature_type: FeatureType::LBP,
  317. 317 feature_vector: histogram,
  318. 318 feature_length: num_bins,
  319. 319 image_id: "",
  320. 320 image_path: "",
  321. 321 width: w,
  322. 322 height: h,
  323. 323 extraction_time_ms: 0.0,
  324. 324 }
  325. 325}
  326. 326
  327. 327///|
  328. 328/// 提取 HOG 特征(简化版)
  329. 329pub fn extract_hog_feature(
  330. 330 img : @types.Image,
  331. 331 config : HOGConfig,
  332. 332) -> CBIRImageFeature {
  333. 333● let w = img.width
  334. 334 let h = img.height
  335. 335 let cell_size = config.cell_size
  336. 336 let num_cells_x = w / cell_size
  337. 337 let num_cells_y = h / cell_size
  338. 338 let num_bins = config.num_bins
  339. 339 let feature_length = num_cells_x * num_cells_y * num_bins
  340. 340 let feature : Array[Double] = []
  341. 341 for _ in 0..<feature_length {
  342. 342● feature.push(0.0)
  343. 343 }
  344. 344
  345. 345 for cy in 0..<num_cells_y {
  346. 346● for cx in 0..<num_cells_x {
  347. 347● let cell_hist : Array[Double] = []
  348. 348 for _ in 0..<num_bins {
  349. 349● cell_hist.push(0.0)
  350. 350 }
  351. 351 for y in 0..<cell_size {
  352. 352● for x in 0..<cell_size {
  353. 353● let px = cx * cell_size + x
  354. 354 let py = cy * cell_size + y
  355. 355 if px > 0 && px < w - 1 && py > 0 && py < h - 1 {
  356. 356● let gx = img.data[py * w + (px + 1)].to_double() -
  357. 357● img.data[py * w + (px - 1)].to_double()
  358. 358● let gy = img.data[(py + 1) * w + px].to_double() -
  359. 359● img.data[(py - 1) * w + px].to_double()
  360. 360● let magnitude = (gx * gx + gy * gy).sqrt()
  361. 361◐ let mut angle = if gx != 0.0 { gy / gx * 0.1 } else { 1.5708 }
  362. 362 if angle < 0.0 {
  363. 363● angle += 3.14159
  364. 364 }
  365. 365● let bin = (angle / 3.14159 * num_bins.to_double()).to_int() %
  366. 366 num_bins
  367. 367 cell_hist[bin] += magnitude
  368. 368 }
  369. 369 }
  370. 370 }
  371. 371 let base_idx = (cy * num_cells_x + cx) * num_bins
  372. 372 for b in 0..<num_bins {
  373. 373● feature[base_idx + b] = cell_hist[b]
  374. 374 }
  375. 375 }
  376. 376 }
  377. 377
  378. 378 CBIRImageFeature::{
  379. 379 feature_type: FeatureType::HOG,
  380. 380 feature_vector: feature,
  381. 381 feature_length,
  382. 382 image_id: "",
  383. 383 image_path: "",
  384. 384 width: w,
  385. 385 height: h,
  386. 386 extraction_time_ms: 0.0,
  387. 387 }
  388. 388}
  389. 389
  390. 390///|
  391. 391/// 计算余弦相似度
  392. 392pub fn cosine_similarity_cbir(
  393. 393 feat1 : Array[Double],
  394. 394 feat2 : Array[Double],
  395. 395) -> Double {
  396. 396● let len = if feat1.length() < feat2.length() {
  397. 397○ feat1.length()
  398. 398 } else {
  399. 399● feat2.length()
  400. 400 }
  401. 401 let mut dot = 0.0
  402. 402 let mut norm1 = 0.0
  403. 403 let mut norm2 = 0.0
  404. 404
  405. 405 for i in 0..<len {
  406. 406● dot += feat1[i] * feat2[i]
  407. 407 norm1 += feat1[i] * feat1[i]
  408. 408 norm2 += feat2[i] * feat2[i]
  409. 409 }
  410. 410
  411. 411 if norm1 > 0.0 && norm2 > 0.0 {
  412. 412● dot / (norm1 * norm2).sqrt()
  413. 413 } else {
  414. 414● 0.0
  415. 415 }
  416. 416}
  417. 417
  418. 418///|
  419. 419/// 计算欧氏距离
  420. 420pub fn euclidean_distance_cbir(
  421. 421 feat1 : Array[Double],
  422. 422 feat2 : Array[Double],
  423. 423) -> Double {
  424. 424● let len = if feat1.length() < feat2.length() {
  425. 425○ feat1.length()
  426. 426 } else {
  427. 427● feat2.length()
  428. 428 }
  429. 429 let mut sum = 0.0
  430. 430
  431. 431 for i in 0..<len {
  432. 432● let diff = feat1[i] - feat2[i]
  433. 433 sum += diff * diff
  434. 434 }
  435. 435
  436. 436● sum.sqrt()
  437. 437}
  438. 438
  439. 439///|
  440. 440/// 计算曼哈顿距离
  441. 441pub fn manhattan_distance_cbir(
  442. 442 feat1 : Array[Double],
  443. 443 feat2 : Array[Double],
  444. 444) -> Double {
  445. 445● let len = if feat1.length() < feat2.length() {
  446. 446○ feat1.length()
  447. 447 } else {
  448. 448● feat2.length()
  449. 449 }
  450. 450 let mut sum = 0.0
  451. 451
  452. 452 for i in 0..<len {
  453. 453● sum += (feat1[i] - feat2[i]).abs()
  454. 454 }
  455. 455
  456. 456 sum
  457. 457}
  458. 458
  459. 459///|
  460. 460/// 计算直方图交集
  461. 461pub fn histogram_intersection_cbir(
  462. 462 feat1 : Array[Double],
  463. 463 feat2 : Array[Double],
  464. 464) -> Double {
  465. 465● let len = if feat1.length() < feat2.length() {
  466. 466○ feat1.length()
  467. 467 } else {
  468. 468● feat2.length()
  469. 469 }
  470. 470 let mut intersection = 0.0
  471. 471 let mut sum1 = 0.0
  472. 472
  473. 473 for i in 0..<len {
  474. 474● intersection += if feat1[i] < feat2[i] { feat1[i] } else { feat2[i] }
  475. 475 sum1 += feat1[i]
  476. 476 }
  477. 477
  478. 478 if sum1 > 0.0 {
  479. 479● intersection / sum1
  480. 480 } else {
  481. 481○ 0.0
  482. 482 }
  483. 483}
  484. 484
  485. 485///|
  486. 486/// 计算相似度(根据度量类型)
  487. 487pub fn calculate_similarity(
  488. 488 feat1 : Array[Double],
  489. 489 feat2 : Array[Double],
  490. 490 metric : SimilarityMetric,
  491. 491) -> Double {
  492. 492● if metric == SimilarityMetric::Cosine {
  493. 493● cosine_similarity_cbir(feat1, feat2)
  494. 494● } else if metric == SimilarityMetric::Euclidean {
  495. 495● let dist = euclidean_distance_cbir(feat1, feat2)
  496. 496 1.0 / (1.0 + dist)
  497. 497● } else if metric == SimilarityMetric::Manhattan {
  498. 498● let dist = manhattan_distance_cbir(feat1, feat2)
  499. 499 1.0 / (1.0 + dist)
  500. 500● } else if metric == SimilarityMetric::Intersection {
  501. 501● histogram_intersection_cbir(feat1, feat2)
  502. 502 } else {
  503. 503● cosine_similarity_cbir(feat1, feat2)
  504. 504 }
  505. 505}
  506. 506
  507. 507///|
  508. 508/// 创建图像索引
  509. 509pub fn create_image_index() -> CBIRImageIndex {
  510. 510● CBIRImageIndex::{
  511. 511 features: [],
  512. 512 num_images: 0,
  513. 513 index_type: "flat",
  514. 514 built: false,
  515. 515 build_time_ms: 0.0,
  516. 516 }
  517. 517}
  518. 518
  519. 519///|
  520. 520/// 向索引添加图像特征
  521. 521pub fn add_to_index(
  522. 522 index : CBIRImageIndex,
  523. 523 feature : CBIRImageFeature,
  524. 524) -> CBIRImageIndex {
  525. 525● let features = index.features
  526. 526● features.push(feature)
  527. 527 CBIRImageIndex::{
  528. 528 features,
  529. 529● num_images: features.length(),
  530. 530 index_type: index.index_type,
  531. 531 built: true,
  532. 532 build_time_ms: index.build_time_ms,
  533. 533 }
  534. 534}
  535. 535
  536. 536///|
  537. 537/// 图像检索
  538. 538pub fn retrieve_images(
  539. 539 query_feature : CBIRImageFeature,
  540. 540 index : CBIRImageIndex,
  541. 541 num_results : Int32,
  542. 542 metric : SimilarityMetric,
  543. 543) -> CBIRRetrievalResultList {
  544. 544● let similarities : Array[Double] = []
  545. 545 for i in 0..<index.num_images {
  546. 546● let sim = calculate_similarity(
  547. 547 query_feature.feature_vector,
  548. 548 index.features[i].feature_vector,
  549. 549 metric,
  550. 550 )
  551. 551● similarities.push(sim)
  552. 552 }
  553. 553
  554. 554 // 按相似度排序(冒泡排序,降序)
  555. 555 let indices : Array[Int32] = []
  556. 556 for i in 0..<index.num_images {
  557. 557● indices.push(i)
  558. 558 }
  559. 559 for i in 0..<index.num_images {
  560. 560● for j in 0..<(index.num_images - i - 1) {
  561. 561● if similarities[indices[j]] < similarities[indices[j + 1]] {
  562. 562● let temp = indices[j]
  563. 563 indices[j] = indices[j + 1]
  564. 564 indices[j + 1] = temp
  565. 565 }
  566. 566 }
  567. 567 }
  568. 568
  569. 569 let results : Array[CBIRRetrievalResult] = []
  570. 570 let result_count = if num_results < index.num_images {
  571. 571● num_results
  572. 572 } else {
  573. 573● index.num_images
  574. 574 }
  575. 575 for i in 0..<result_count {
  576. 576● let idx = indices[i]
  577. 577 let feat = index.features[idx]
  578. 578● results.push(CBIRRetrievalResult::{
  579. 579 image_id: feat.image_id,
  580. 580 image_path: feat.image_path,
  581. 581 similarity: similarities[idx],
  582. 582 distance: 1.0 - similarities[idx],
  583. 583 rank: i + 1,
  584. 584 width: feat.width,
  585. 585 height: feat.height,
  586. 586 })
  587. 587 }
  588. 588
  589. 589 CBIRRetrievalResultList::{
  590. 590 results,
  591. 591 num_results: result_count,
  592. 592 query_image_id: query_feature.image_id,
  593. 593 retrieval_time_ms: 0.0,
  594. 594 total_images_in_index: index.num_images,
  595. 595 }
  596. 596}
  597. 597
  598. 598///|
  599. 599/// 提取组合特征(颜色+纹理+形状)
  600. 600pub fn extract_combined_features(img : @types.Image) -> CBIRImageFeature {
  601. 601● let color_config = create_default_color_histogram_config()
  602. 602● let color_feat = extract_color_histogram(img, color_config)
  603. 603● let lbp_config = create_default_lbp_config()
  604. 604● let lbp_feat = extract_lbp_feature(img, lbp_config)
  605. 605
  606. 606 // 组合特征
  607. 607 let combined : Array[Double] = []
  608. 608 for v in color_feat.feature_vector {
  609. 609● combined.push(v)
  610. 610 }
  611. 611 for v in lbp_feat.feature_vector {
  612. 612● combined.push(v)
  613. 613 }
  614. 614
  615. 615 CBIRImageFeature::{
  616. 616 feature_type: FeatureType::Combined,
  617. 617 feature_vector: combined,
  618. 618● feature_length: combined.length(),
  619. 619 image_id: "",
  620. 620 image_path: "",
  621. 621 width: img.width,
  622. 622 height: img.height,
  623. 623 extraction_time_ms: 0.0,
  624. 624 }
  625. 625}
  626. 626
  627. 627///|
  628. 628/// 生成检索报告
  629. 629pub fn retrieval_report_to_string(
  630. 630 results : CBIRRetrievalResultList,
  631. 631 query_feature : CBIRImageFeature,
  632. 632) -> String {
  633. 633● let mut report = "## 图像检索报告\n\n"
  634. 634 report = report + "### 查询信息\n\n"
  635. 635 report = report + "| 指标 | 值 |\n|------|------|\n"
  636. 636 report = report + "| 查询图像ID | " + query_feature.image_id + " |\n"
  637. 637 let feat_type_str = if query_feature.feature_type ==
  638. 638 FeatureType::ColorHistogram {
  639. 639● "颜色直方图"
  640. 640● } else if query_feature.feature_type == FeatureType::Texture {
  641. 641● "纹理"
  642. 642● } else if query_feature.feature_type == FeatureType::Shape {
  643. 643● "形状"
  644. 644● } else if query_feature.feature_type == FeatureType::SIFT {
  645. 645● "SIFT"
  646. 646● } else if query_feature.feature_type == FeatureType::HOG {
  647. 647● "HOG"
  648. 648● } else if query_feature.feature_type == FeatureType::LBP {
  649. 649● "LBP"
  650. 650● } else if query_feature.feature_type == FeatureType::Combined {
  651. 651● "组合特征"
  652. 652● } else if query_feature.feature_type == FeatureType::Unknown {
  653. 653● "未知"
  654. 654 } else {
  655. 655● "其他"
  656. 656 }
  657. 657 report = report + "| 特征类型 | " + feat_type_str + " |\n"
  658. 658 report = report +
  659. 659 "| 特征维度 | " +
  660. 660● query_feature.feature_length.to_string() +
  661. 661 " |\n"
  662. 662 report = report +
  663. 663 "| 索引图像数 | " +
  664. 664● results.total_images_in_index.to_string() +
  665. 665 " |\n"
  666. 666 report = report +
  667. 667 "| 检索耗时 | " +
  668. 668● results.retrieval_time_ms.to_string() +
  669. 669 "ms |\n\n"
  670. 670
  671. 671 if results.num_results > 0 {
  672. 672● report = report + "### 检索结果\n\n"
  673. 673 report = report + "| 排名 | 图像ID | 相似度 | 距离 | 尺寸 |\n"
  674. 674 report = report + "|------|--------|--------|------|------|\n"
  675. 675 for result in results.results {
  676. 676● report = report +
  677. 677 "| " +
  678. 678● result.rank.to_string() +
  679. 679 " | " +
  680. 680 result.image_id +
  681. 681 " | " +
  682. 682● (result.similarity * 100.0).to_string() +
  683. 683 "% | " +
  684. 684● result.distance.to_string() +
  685. 685 " | " +
  686. 686● result.width.to_string() +
  687. 687 "x" +
  688. 688● result.height.to_string() +
  689. 689 " |\n"
  690. 690 }
  691. 691 } else {
  692. 692● report = report + "### 检索结果\n\n未找到匹配图像。\n"
  693. 693 }
  694. 694
  695. 695 report
  696. 696}