Discrete cosine transformation (including source code)

Source: Internet
Author: User

In addition to Fourier transformation, commonly used orthogonal transformations in image processing also have some useful orthogonal transformations. The Discrete Cosine is one of them. Discrete cosine transform is represented as DCT (discrete cosine transformation), which is often used in image processing and image recognition.

One-dimensional discrete cosine transformation positive transformation

(1)

(2)

In formula, f (u) is the cosine transformation coefficient of the U, and U is the Generalized Frequency Variable. U =, 3 ...... n-1; f (x) is the time domain n point sequence, x =, 2 ...... n-1

Inverse Transformation

(3)

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

Two-dimensional discrete cosine transformation

Positive transformation

(4)

Formula (4) is a positive transformation formula. F (x, y) is an element of the Two-dimensional vector of the spatial domain. X, Y =, 2 ,...... n-1; f (u, v) is an element of the conversion coefficient array. The array in formula is n × n.

Inverse Transformation

(5)

The meaning of the symbol is the same as that of the positive transform. Formula (4) and formula (5) are analytical definitions of discrete cosine transformation.

Matrix Representation

A more concise definition method is matrix definition. According to the definition of the above formula, the coefficient matrix of discrete cosine transformation can be written as follows:


If n = 4, the one-dimensional analytical expression can be defined as the following expansion.


Written as a matrix


If f (u) is defined as the transformation matrix, a is the transformation coefficient matrix, and f (x) is the time domain data matrix, the matrix definition of one-dimensional discrete cosine transformation can be written as follows:

[F (u)] = [a] [F (x)] (6)

Likewise, you can getInverse TransformationExpansion

Written as a matrix

[F (x)] = [a] t [f (u)] (7)

Two-dimensional discrete cosine transformationYou can also write a matrix:

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

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

In formula, [F (x, y)] is a space data array, a is a transformation coefficient array, and [f (u, v)] is a transformation matrix, [a] t is the transpose of [.

Perform discrete cosine transformation on two-dimensional images

According to the preceding definition and formula of two-dimensional discrete cosine transformation (7), the following steps are taken to calculate the discrete cosine transformation of two-dimensional images:

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

2. Calculate the coefficient matrix of discrete cosine transformation [a];

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

4. Calculate the discrete cosine transformation based on formula (7) [f (u, v)] = [a] [F (x, y)] [a] t;

Source code:
Package cn.edu. jxau. image; import Java. AWT. image. bufferedimage; /*** image transformation ** @ author luoweifu **/public class transformation {/*** width or height of the image to be transformed by DCT */public static final int n = 256; /*** Fourier transform * @ return */Public int [] FFT () {return NULL ;} /*** discrete cosine transformation ** @ Param pix data matrix of the original image * @ Param n original image (N * n) * @ return converted matrix array */Public int [] DCT (INT [] pix, int N) {double [] [] imatrix = new double [N] [N]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {imatrix [I] [J] = (double) (pix [I * n + J]);} double [] [] quotient = coefficient (n); // evaluate the coefficient matrix double [] [] quotientt = transposingmatrix (quotient, N ); // transpose coefficient matrix double [] [] temp = new double [N] [N]; temp = matrixmultiply (quotient, imatrix, n); imatrix = matrixmultiply (temp, quotientt, n); int newpix [] = new int [N * n]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {newpix [I * n + J] = (INT) imatrix [I] [J] ;}} return newpix ;} /*** matrix transpose * @ Param matrix original matrix * @ Param n matrix (N * n) * @ return transposed matrix */private double [] [] transposingmatrix (double [] [] matrix, int N) {double nmatrix [] [] = new double [N] [N]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {nmatrix [I] [J] = matrix [J] [I] ;}} return nmatrix ;} /*** calculate the coefficient matrix of discrete cosine transformation * @ Param N * n matrix size * @ return coefficient matrix */private double [] [] coefficient (int n) {double [] [] coeff = new double [N] [N]; double SQRT = 1.0/math. SQRT (n); For (INT I = 0; I <n; I ++) {coeff [0] [I] = SQRT;} For (INT I = 1; I <n; I ++) {for (Int J = 0; j <n; j ++) {coeff [I] [J] = math. SQRT (2.0/n) * Math. cos (I * Math. pI * (J + 0.5)/(double) n) ;}} return coeff ;} /*** multiply matrix * @ Param a matrix A * @ Param B matrix B * @ Param n matrix size N * n * @ return result matrix */private double [] [] matrixmultiply (double [] [], double [] [] B, int N) {double nmatrix [] [] = new double [N] [N]; Double T = 0.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];} nmatrix [I] [J] = T ;}} return nmatrix ;}}

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.