Discrete cosine transform (c + + implementation)

Source: Internet
Author: User

The theoretical part is reproduced from this article blog:http://blog.csdn.net/luoweifu/article/details/8214959 the blog gives Java code, I use C + + to implement it.

Theory:

In addition to the Fourier transform, the orthogonal transformations commonly used in image processing have some other useful orthogonal transformations, in which the discrete cosine is one. The discrete cosine transform is represented as DCT (discrete cosine transformation), which is commonly used for image processing and image recognition.

Positive transformation of one-dimensional discrete cosine transform

(1)

(2)

In the formula F (U) is the first cosine transform coefficient, U is the generalized frequency variable, u=1,2,3......n-1; F (x) is the time-domain N-point sequence, x=0,1,2......n-1

Inverse transformation

(3)

Obviously, the formula (1) formula (2) and the formula (3) constitute a one-dimensional discrete cosine transform pair.

Two-dimensional discrete cosine transform

Positive transformation

(4)

Equation (4) is a positive transformation formula. where f (x, y) is the element of two-dimensional vector in space domain, x,y=0,1,2,...... N-1;f (U,V) is an element of the transformation coefficient array. The array represented in the formula is NXN

Inverse transformation

(5)

The symbolic meaning in the formula is the same as the positive transformation. The formula (4) and the formula (5) are the analytic definitions of the discrete cosine transform.

Matrix notation

A more concise way to define this is to use a matrix definition. According to the above formula definition, the coefficient matrix of the discrete cosine transform can be written as follows:



If you make n=4, then the one-dimensional analytic definition can be expanded as follows.


Written in matrix type


If the definition F (U) is a transformation matrix, a is a transformation coefficient matrix, and F (x) is the last domain data matrix, then the matrix definition of one-dimensional discrete cosine transform can be written as follows

[F (U)]=[a][f (x)] (6)

In the same vein, you can get the anti-transform expansion


written in matrix type that

[F (x)]= A T[f (U)] (7)

A two-dimensional discrete cosine transform can also be written as a matrix:

[F (U,v)]=[a][f (x, y)][a]t (8)

[F (x, y)]=[a]t[f (u,v)][a]

[F (x, Y)] is a spatial data array, a is a transformation coefficient pattern, [f (u,v)] is a transformation matrix, [A]t is a transpose of [A].

Discrete cosine transform for two-dimensional image

The definition and formula of the two-dimensional discrete cosine transform (7) shows that the discrete cosine transform of two-dimensional image is performed in the following steps:

1. Obtain the two-dimensional data matrix f (x, Y)of the image;

2. Finding the coefficient matrix of the discrete cosine transform [A];

3. Find the transpose matrix corresponding to the coefficient matrix [a]t;

4. Calculate the discrete cosine transform according to the formula (7)[F (U,V)]=[a][f (x, y)][a]t;

The following is my C + + code implementation < Of course, which is for the image, so the use of OpenCV library function;

C + + code:

/* Function: Get DCT coefficient n: Matrix size quotient: coefficient quotientt: coefficient transpose */void coefficient (const int &n, double **quotient, double **quotientt {Double SQR = 1.0/sqrt (n+0.0); for (int i = 0; i < n; i++) {quotient[0][i] = sqr;quotientt[i][0] = SQR;}  for (int i = 1, i < n; i++) {for (int j = 0; J < N; j + +) {Quotient[i][j] = sqrt (2.0/n) *cos (i* (j+0.5) *pi/n); obtained by formula Quotientt[j][i] = Quotient[i][j];}}}  /* Function: Two matrices multiplied by A and B: source input matrix Result: output matrix */void matrixmultiply (double **a, double **b, int n, double **result) {Double t = 0;for (int i = 0; I < n;   i++) {for (int j = 0; J < N; j + +) {t = 0;for (int k = 0; k < n; k++) T + = a[i][k]*b[k][j]; RESULT[I][J] = t;}}} DCT transform void DCT (mat_<uchar> image, const int &n, double **imatrix) {for (int i = 0; i < n; i++) {for (int j = 0 ; J < N; J + +) {Imatrix[i][j] = (double) image (I,J);}}  Allocate space for coefficients double **quotient = new Double*[n];d ouble **quotientt = new Double*[n];d ouble **tmp = new Double*[n];for (int i = 0; I < n; i++) {Quotient[i] = new Double[n];quotientt[i] = new doUble[n]; Tmp[i] = new Double[n];}  Calculate coefficient matrix coefficient (n, quotient, Quotientt), matrixmultiply (quotient, Imatrix, N, TMP); Results from the formula result matrixmultiply (TMP, QUOTIENTT, N, Imatrix); for (int i = 0; i < n; i++) {delete []tmp[i];d elete []quotient[i];d E lete []quotientt[i];} delete []tmp;delete []quotient;delete []quotientt;}




Discrete cosine transform (c + + implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.