-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCT.cpp
More file actions
74 lines (64 loc) · 1.88 KB
/
DCT.cpp
File metadata and controls
74 lines (64 loc) · 1.88 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
#include "DCT.h"
// OpenCV
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
cv::Mat calcP()
/*
Cette fonction renvoie la matrice de coefficients P utile pour le calcul de la DCT.
P ne dépend que de la taille du bloc B, et sera donc constante dans notre application
puisque B est toujours de taille 8x8.
*/
{
int N = 8;
cv::Mat P = cv::Mat(N, N, CV_32FC1);
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
if (j == 0)
{
P.at<float>(i,j) = (1.0 / sqrt(N)) * cos(((2*i + 1.0) * j * CV_PI) / ((float) 2 * N));
}
else
{
P.at<float>(i,j) = (sqrt(2.0 / N)) * cos(((2*i + 1.0) * j * CV_PI) / ((float) 2 * N));
}
}
}
return P;
};
cv::Mat DCT(cv::Mat B, cv::Mat P)
/*
Cette fonction renvoie la matrice F = P'BP, Discrete Cosine Transform du bloc 8x8 B.
Paramètres : le bloc B à transformer et la matrice P de coefficients
*/
{
cv::Mat F = cv::Mat(8, 8, CV_32FC3);
std::vector<cv::Mat> YIQ;
split(B, YIQ); // On sépare les différents channels pour pouvoir multiplier par P
for (int i=0; i<3; i++)
{
YIQ[i] = P.t() * YIQ[i] * P;
}
merge(YIQ, F); // On fusionne les différents channels pour reformer l'image RGB (ou plutôt YIQ)
return F;
};
cv::Mat inverseDCT(cv::Mat F, cv::Mat P)
/*
Cette fonction renvoie la matrice B = PFP', en faisant la Discrete Cosine Transform inverse du bloc 8x8 B.
Paramètres : le bloc F à transformer et la matrice P de coefficients
*/
{
cv::Mat B = cv::Mat(8, 8, CV_32FC3);
std::vector<cv::Mat> YIQ;
split(F, YIQ); // On sépare les différents channels pour pouvoir multiplier par P
for (int i=0; i<3; i++)
{
YIQ[i] = P * YIQ[i] * P.t();
}
merge(YIQ, B); // On fusionne les différents channels pour reformer l'image RGB (ou plutôt YIQ)
return B;
}