-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
71 lines (63 loc) · 2 KB
/
utils.cpp
File metadata and controls
71 lines (63 loc) · 2 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
#include "utils.h"
#include <map>
#include <numeric>
// 计算DCT(使用OpenCV)
cv::Mat calculateDCT(const cv::Mat& input) {
cv::Mat floatInput;
// 确保输入是浮点类型
if (input.type() != CV_32F && input.type() != CV_64F) {
input.convertTo(floatInput, CV_32F);
} else {
floatInput = input.clone();
}
cv::Mat dctResult;
cv::dct(floatInput, dctResult);
return dctResult;
}
// 计算IDCT(使用OpenCV)
cv::Mat calculateIDCT(const cv::Mat& input) {
cv::Mat idctResult;
cv::idct(input, idctResult);
return idctResult;
}
// 计算信息熵 (式 3 的核心部分)
double calculateEntropy(const cv::Mat& region) {
std::map<uchar, int> hist;
int totalPixels = region.rows * region.cols;
if (totalPixels == 0) return 0.0;
for (int i = 0; i < region.rows; ++i) {
for (int j = 0; j < region.cols; ++j) {
hist[region.at<uchar>(i, j)]++;
}
}
double entropy = 0.0;
for (auto const& [pixelValue, count] : hist) {
if (count > 0) {
double probability = static_cast<double>(count) / totalPixels;
entropy -= probability * std::log2(probability); // 使用 log base 2
}
}
return entropy; // 这就是 H_uv
}
// 计算高斯权重 (式 17) - 用于边缘块像素修改量分配
cv::Mat calculateGaussianWeights(int rows, int cols, double sigma) {
cv::Mat weights = cv::Mat::zeros(rows, cols, CV_64F);
double sumWeights = 0.0;
int centerX = cols / 2;
int centerY = rows / 2;
double sigmaSq = sigma * sigma;
double factor = 1.0 / (2.0 * CV_PI * sigmaSq);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double distSq = std::pow(i - centerY, 2) + std::pow(j - centerX, 2);
double weight = factor * std::exp(-distSq / (2.0 * sigmaSq));
weights.at<double>(i, j) = weight;
sumWeights += weight;
}
}
// 归一化权重,使其和为 1
if (sumWeights > 1e-9) { // 避免除以零
weights /= sumWeights;
}
return weights;
}