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