-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_38.cpp
198 lines (157 loc) · 4.37 KB
/
answer_38.cpp
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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
#include <complex>
const int height = 128, width = 128, channel = 3;
// DCT hyper-parameter
int T = 8;
int K = 4;
// DCT coefficient
struct dct_str {
double coef[height][width][channel];
};
// Discrete Cosine transformation
dct_str dct(cv::Mat img, dct_str dct_s){
double I;
double F;
double Cu, Cv;
for(int ys = 0; ys < height; ys += T){
for(int xs = 0; xs < width; xs += T){
for(int c = 0; c < channel; c++){
for(int v = 0; v < T; v ++){
for(int u = 0; u < T; u ++){
F = 0;
if (u == 0){
Cu = 1. / sqrt(2);
} else{
Cu = 1;
}
if (v == 0){
Cv = 1. / sqrt(2);
}else {
Cv = 1;
}
for (int y = 0; y < T; y++){
for(int x = 0; x < T; x++){
I = (double)img.at<cv::Vec3b>(ys + y, xs + x)[c];
F += 2. / T * Cu * Cv * I * cos((2. * x + 1) * u * M_PI / 2. / T) * cos((2. * y + 1) * v * M_PI / 2. / T);
}
}
dct_s.coef[ys + v][xs + u][c] = F;
}
}
}
}
}
return dct_s;
}
// Inverse Discrete Cosine transformation
cv::Mat idct(cv::Mat out, dct_str dct_s){
double f;
double Cu, Cv;
for(int ys = 0; ys < height; ys += T){
for(int xs = 0; xs < width; xs += T){
for(int c = 0; c < channel; c++){
for(int y = 0; y < T; y++){
for(int x = 0; x < T; x++){
f = 0;
for (int v = 0; v < K; v++){
for (int u = 0; u < K; u++){
if (u == 0){
Cu = 1. / sqrt(2);
} else {
Cu = 1;
}
if (v == 0){
Cv = 1. / sqrt(2);
} else {
Cv = 1;
}
f += 2. / T * Cu * Cv * dct_s.coef[ys + v][xs + u][c] * cos((2. * x + 1) * u * M_PI / 2. / T) * cos((2. * y + 1) * v * M_PI / 2. / T);
}
}
f = fmin(fmax(f, 0), 255);
out.at<cv::Vec3b>(ys + y, xs + x)[c] = (uchar)f;
}
}
}
}
}
return out;
}
// Quantization
dct_str quantization(dct_str dct_s){
double Q[T][T] = {{16, 11, 10, 16, 24, 40, 51, 61},
{12, 12, 14, 19, 26, 58, 60, 55},
{12, 12, 14, 19, 26, 58, 60, 55},
{14, 17, 22, 29, 51, 87, 80, 62},
{18, 22, 37, 56, 68, 109, 103, 77},
{24, 35, 55, 64, 81, 104, 113, 92},
{49, 64, 78, 87, 103, 121, 120, 101},
{72, 92, 95, 98, 112, 100, 103, 99}
};
for (int ys = 0; ys < height; ys += T){
for (int xs = 0; xs < width; xs += T){
for(int c = 0; c < channel; c++){
for(int y = 0; y < T; y++){
for(int x = 0; x < T; x++){
dct_s.coef[ys + y][xs + x][c] = round(dct_s.coef[ys + y][xs + x][c] / Q[y][x]) * Q[y][x];
}
}
}
}
}
return dct_s;
}
// Compute MSE
double MSE(cv::Mat img1, cv::Mat img2){
double mse = 0;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
for(int c = 0; c < channel; c++){
mse += pow(((double)img1.at<cv::Vec3b>(y, x)[c] - (double)img2.at<cv::Vec3b>(y, x)[c]), 2);
}
}
}
mse /= (height * width);
return mse;
}
// Compute PSNR
double PSNR(double mse, double v_max){
return 10 * log10(v_max * v_max / mse);
}
// Compute bitrate
double BITRATE(){
return T * K * K / T * T;
}
// Main
int main(int argc, const char* argv[]){
double mse;
double psnr;
double bitrate;
// read original image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// DCT coefficient
dct_str dct_s;
// output image
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
// DCT
dct_s = dct(img, dct_s);
// Quantization
dct_s = quantization(dct_s);
// IDCT
out = idct(out, dct_s);
// MSE, PSNR
mse = MSE(img, out);
psnr = PSNR(mse, 255);
bitrate = BITRATE();
std::cout << "MSE: " << mse << std::endl;
std::cout << "PSNR: " << psnr << std::endl;
std::cout << "bitrate: " << bitrate << std::endl;
cv::imwrite("out.jpg", out);
//cv::imshow("answer", out);
//cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}