-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processor.cpp
More file actions
364 lines (299 loc) · 12.9 KB
/
Copy pathimage_processor.cpp
File metadata and controls
364 lines (299 loc) · 12.9 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <sstream>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <queue>
#include <functional>
#include <chrono>
#include <exception>
#include <cstring>
#include <cstdio>
#include <cstdlib>
// ==================== 图像处理核心模块 ====================
struct ImagePixel {
unsigned char r, g, b, a;
};
struct ImageData {
int width;
int height;
int channels;
std::vector<ImagePixel> pixels;
std::string format;
std::string source_url;
};
struct FeatureDescriptor {
std::vector<double> color_histogram;
std::vector<double> texture_features;
std::vector<double> edge_features;
std::vector<double> shape_descriptors;
double quality_score;
std::map<std::string, double> metadata;
};
class ImageProcessor {
private:
std::mutex process_lock;
// 图像质量评估
double calculateSharpness(const ImageData& img) {
if (img.width < 3 || img.height < 3) return 0.0;
double sharpness = 0.0;
int count = 0;
for (int y = 1; y < img.height - 1; ++y) {
for (int x = 1; x < img.width - 1; ++x) {
int idx = y * img.width + x;
double gradient_x = std::abs(img.pixels[idx + 1].r - img.pixels[idx - 1].r);
double gradient_y = std::abs(img.pixels[idx + img.width].r - img.pixels[idx - img.width].r);
sharpness += gradient_x + gradient_y;
count++;
}
}
return count > 0 ? sharpness / count : 0.0;
}
// 颜色直方图计算
std::vector<double> calculateColorHistogram(const ImageData& img, int bins = 32) {
std::vector<double> histogram(bins * 3, 0.0);
double bin_size = 256.0 / bins;
for (const auto& pixel : img.pixels) {
int r_bin = std::min(bins - 1, static_cast<int>(pixel.r / bin_size));
int g_bin = std::min(bins - 1, static_cast<int>(pixel.g / bin_size));
int b_bin = std::min(bins - 1, static_cast<int>(pixel.b / bin_size));
histogram[r_bin]++;
histogram[bins + g_bin]++;
histogram[bins * 2 + b_bin]++;
}
// 归一化
double total = img.pixels.size();
for (auto& val : histogram) {
val /= total;
}
return histogram;
}
// 边缘检测(Sobel算子)
std::vector<double> detectEdges(const ImageData& img) {
std::vector<double> edges;
if (img.width < 3 || img.height < 3) return edges;
int sobel_x[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int sobel_y[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
for (int y = 1; y < img.height - 1; ++y) {
for (int x = 1; x < img.width - 1; ++x) {
double gx = 0, gy = 0;
for (int ky = -1; ky <= 1; ++ky) {
for (int kx = -1; kx <= 1; ++kx) {
int idx = (y + ky) * img.width + (x + kx);
double intensity = (img.pixels[idx].r + img.pixels[idx].g + img.pixels[idx].b) / 3.0;
gx += intensity * sobel_x[ky + 1][kx + 1];
gy += intensity * sobel_y[ky + 1][kx + 1];
}
}
edges.push_back(std::sqrt(gx * gx + gy * gy));
}
}
return edges;
}
// 纹理特征提取(灰度共生矩阵简化版)
std::vector<double> extractTextureFeatures(const ImageData& img) {
std::vector<double> features;
// 计算灰度图像
std::vector<double> gray(img.pixels.size());
for (size_t i = 0; i < img.pixels.size(); ++i) {
gray[i] = (img.pixels[i].r + img.pixels[i].g + img.pixels[i].b) / 3.0;
}
// 对比度
double contrast = 0.0;
int pairs = 0;
for (size_t i = 0; i + 1 < gray.size(); ++i) {
contrast += std::pow(gray[i] - gray[i + 1], 2);
pairs++;
}
features.push_back(pairs > 0 ? contrast / pairs : 0.0);
// 能量
double energy = 0.0;
for (double val : gray) {
energy += val * val;
}
features.push_back(energy / gray.size());
// 熵
std::map<int, int> freq;
for (double val : gray) {
freq[static_cast<int>(val)]++;
}
double entropy = 0.0;
for (const auto& pair : freq) {
double p = pair.second / static_cast<double>(gray.size());
if (p > 0) entropy -= p * std::log2(p);
}
features.push_back(entropy);
return features;
}
public:
FeatureDescriptor extractFeatures(const ImageData& img) {
std::lock_guard<std::mutex> lock(process_lock);
FeatureDescriptor features;
features.color_histogram = calculateColorHistogram(img);
features.texture_features = extractTextureFeatures(img);
features.edge_features = detectEdges(img);
features.quality_score = calculateSharpness(img);
features.metadata["width"] = img.width;
features.metadata["height"] = img.height;
features.metadata["total_pixels"] = img.pixels.size();
features.metadata["avg_brightness"] = calculateAverageBrightness(img);
return features;
}
double calculateSimilarity(const FeatureDescriptor& f1, const FeatureDescriptor& f2) {
// 计算特征向量的余弦相似度
double dot_product = 0.0, norm1 = 0.0, norm2 = 0.0;
// 颜色直方图相似度
for (size_t i = 0; i < f1.color_histogram.size() && i < f2.color_histogram.size(); ++i) {
dot_product += f1.color_histogram[i] * f2.color_histogram[i];
norm1 += f1.color_histogram[i] * f1.color_histogram[i];
norm2 += f2.color_histogram[i] * f2.color_histogram[i];
}
return (norm1 > 0 && norm2 > 0) ? dot_product / (std::sqrt(norm1) * std::sqrt(norm2)) : 0.0;
}
ImageData resizeImage(const ImageData& src, int new_width, int new_height) {
ImageData result;
result.width = new_width;
result.height = new_height;
result.channels = src.channels;
result.format = src.format;
double x_ratio = src.width / static_cast<double>(new_width);
double y_ratio = src.height / static_cast<double>(new_height);
for (int y = 0; y < new_height; ++y) {
for (int x = 0; x < new_width; ++x) {
int src_x = static_cast<int>(x * x_ratio);
int src_y = static_cast<int>(y * y_ratio);
src_x = std::min(src_x, src.width - 1);
src_y = std::min(src_y, src.height - 1);
result.pixels.push_back(src.pixels[src_y * src.width + src_x]);
}
}
return result;
}
private:
double calculateAverageBrightness(const ImageData& img) {
double sum = 0.0;
for (const auto& pixel : img.pixels) {
sum += (pixel.r + pixel.g + pixel.b) / 3.0;
}
return img.pixels.empty() ? 0.0 : sum / img.pixels.size();
}
};
// ==================== 文物图像识别引擎 ====================
struct RecognitionResult {
std::string artifact_type;
double confidence;
std::string dynasty;
std::string material;
std::map<std::string, double> type_probabilities;
std::vector<std::string> characteristics;
std::string authentication_advice;
};
class ArtifactRecognitionEngine {
private:
std::map<std::string, std::vector<FeatureDescriptor>> known_artifacts;
std::mutex recognition_lock;
std::string classifyByFeatures(const FeatureDescriptor& features) {
// 基于特征的分类逻辑(简化版)
double texture_energy = features.texture_features.size() > 1 ? features.texture_features[1] : 0.0;
double edge_density = features.edge_features.empty() ? 0.0 :
std::accumulate(features.edge_features.begin(), features.edge_features.end(), 0.0) / features.edge_features.size();
if (texture_energy > 5000 && edge_density > 100) {
return "古钱币";
} else if (texture_energy > 3000 && edge_density > 80) {
return "瓷器";
} else if (edge_density > 120) {
return "青铜器";
} else {
return "其他文物";
}
}
std::string identifyDynasty(const FeatureDescriptor& features) {
double quality = features.quality_score;
if (quality > 200) return "清代";
else if (quality > 150) return "明代";
else if (quality > 100) return "宋代";
else if (quality > 50) return "唐代";
else return "古代";
}
public:
RecognitionResult recognize(const ImageData& img) {
std::lock_guard<std::mutex> lock(recognition_lock);
RecognitionResult result;
ImageProcessor processor;
FeatureDescriptor features = processor.extractFeatures(img);
result.artifact_type = classifyByFeatures(features);
result.dynasty = identifyDynasty(features);
result.confidence = std::min(0.95, features.quality_score / 300.0);
result.material = result.artifact_type == "古钱币" ? "铜" :
result.artifact_type == "瓷器" ? "瓷" : "金属";
result.type_probabilities[result.artifact_type] = result.confidence;
result.type_probabilities["其他"] = 1.0 - result.confidence;
result.characteristics.push_back("纹理复杂度: " + std::to_string(features.texture_features.size()));
result.characteristics.push_back("边缘密度: " + std::to_string(features.edge_features.size()));
result.characteristics.push_back("质量评分: " + std::to_string(features.quality_score));
if (result.confidence > 0.7) {
result.authentication_advice = "高置信度识别,建议进一步专家鉴定";
} else {
result.authentication_advice = "低置信度,需要人工审核";
}
return result;
}
bool learnArtifact(const std::string& type, const FeatureDescriptor& features) {
std::lock_guard<std::mutex> lock(recognition_lock);
known_artifacts[type].push_back(features);
return true;
}
};
// ==================== 测试主函数 ====================
int main() {
std::cout << "========================================" << std::endl;
std::cout << " 文物图像处理与识别模块" << std::endl;
std::cout << " Artifact Image Processing & Recognition" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << std::endl;
// 创建测试图像数据(模拟)
ImageData test_image;
test_image.width = 100;
test_image.height = 100;
test_image.channels = 3;
test_image.format = "RGB";
for (int i = 0; i < 100 * 100; ++i) {
ImagePixel pixel;
pixel.r = static_cast<unsigned char>(rand() % 256);
pixel.g = static_cast<unsigned char>(rand() % 256);
pixel.b = static_cast<unsigned char>(rand() % 256);
pixel.a = 255;
test_image.pixels.push_back(pixel);
}
std::cout << "[INFO] 加载测试图像: " << test_image.width << "x" << test_image.height << std::endl;
// 特征提取
ImageProcessor processor;
auto features = processor.extractFeatures(test_image);
std::cout << std::endl;
std::cout << "=== 特征提取结果 ===" << std::endl;
std::cout << "颜色直方图维度: " << features.color_histogram.size() << std::endl;
std::cout << "纹理特征数量: " << features.texture_features.size() << std::endl;
std::cout << "边缘特征数量: " << features.edge_features.size() << std::endl;
std::cout << "图像质量评分: " << features.quality_score << std::endl;
// 文物识别
ArtifactRecognitionEngine recognizer;
auto recognition = recognizer.recognize(test_image);
std::cout << std::endl;
std::cout << "=== 识别结果 ===" << std::endl;
std::cout << "文物类型: " << recognition.artifact_type << std::endl;
std::cout << "置信度: " << (recognition.confidence * 100) << "%" << std::endl;
std::cout << "推测朝代: " << recognition.dynasty << std::endl;
std::cout << "材质: " << recognition.material << std::endl;
std::cout << std::endl;
std::cout << "鉴定建议: " << recognition.authentication_advice << std::endl;
std::cout << std::endl;
std::cout << "=== C++图像处理模块运行成功 ===" << std::endl;
return 0;
}