C + + implements discrete cosine transform (parameters are two-dimensional pointers)

Source: Internet
Author: User

Http://www.cnblogs.com/scut-linmaojiang/p/5013590.html

Write in front

So far has read a considerable part of the grid watermark, and other aspects of the paper, but the progress of the paper has not been more, this month to choose some of the more classic papers, the algorithm will be implemented. In the process of realizing the thesis, it is found that some algorithms in the paper are useful to the frequency domain of airspace. So I thought of realizing the discrete cosine transform. Although the code in this article is similar to many existing code on the Web, there is not much difference in thinking, but this article has a relatively important improvement. In particular, the existing DCT algorithm on the net input is a fixed two-dimensional array. When a two-dimensional array is passed as a function parameter, at least the size of the second dimension needs to be given, or the compiler will give an error. But in graphic image processing, when we want to use a DCT transform to convert a graphic or image to the frequency domain, perhaps I do not know the size of the graph or image prior to calling the DCT function, so this method of communication greatly restricts the use of code. One of the improvements in this paper is that the parameters of the DCT function are no longer two-dimensional arrays, but instead pass through a two-dimensional pointer, and implement function functions by manual addressing.

Theoretical basis of discrete cosine transform

I think we are more familiar with the Fourier transform. In fact, the discrete cosine transform is almost the same as the Fourier transform. The definition of a two-dimensional discrete cosine transform is represented by the following:

The inverse transformation is represented as follows:

Code implementation

According to the above formula, it is easy to write code

Dct-discrete cosine TransformvoidDct(Double * * input,Double * * OUTPUT,int row,int col) {cout<<"Test in DCT" <<endl;Double ALPHA, BETA;int u =0;int v =0;int i =0;Int J =0;for (U =0; U < row; u++) {for (v =0; v < col; v++) {if (U = =0) {ALPHA =sqrt1.0/row); }else {ALPHA =sqrt2.0/row); }if (v = =0) {BETA =sqrt1.0/COL); }else {BETA =sqrt2.0/COL); }Double TMP =0.0;for (i =0; i < row; i++) {for (j =0; J < Col; J + +) {tmp + = * (double*) input + col*i + j) *Cos ((2*i+1) *u*pi/(2.0 * row)) * cos ((2*j+1) *v*PI/(2.0 * col)); }} * ((double*) output + col*u + V) = ALPHA * BETA * TMP;}} cout <<  "The result of the DCT:" << Endl; for (int m = 0; m < row; m++) {for (int n= 0; n < col; n++) {cout <<setw (8) << * ((double*) Output + col*m + N) << "\ t";} cout << Endl;}}           

Note that the function parameter in the code is not a two-dimensional array, but a pointer to a two-dimensional array. The code for the corresponding inverse transformation is as follows:

Inverse dctvoid IDCT (double * *Input, double * * output, int row, int col) {cout<<"Test in IDCT" <<endl; DoubleALPHA, BETA; Intu = 0; int v = 0; int i = 0; int j = 0;for (i = 0; i < row; i++) {for (j = 0; J < Col; J + +) {double tmp = 0.0;Foru = 0;U < row;u++) {for (v = 0; v < col; v++) {IfU = = 0) {ALPHA =sqrt (1.0/row); }else {ALPHA =sqrt (2.0/row); }if (v = = 0) {BETA =sqrt (1.0/col); }else {BETA =sqrt (2.0/col); } tmp + =ALPHA * BETA * * ((double*)input + col*U + V) * cos ((2*i+1) *u*pi/(2.0 * row)) * cos ((2*j+1) *v*pi/(2.0 * col));}} * ((double*) output + col*i + j) = tmp;} } cout << "The result of IDCT:" << Endl; for (int m = 0; m < row; m++) {for (int n= 0; N < col; n++) {cout <<SETW (8) << * ((double*) output + col*m + N) <<"\ t";} cout << Endl; }} 

Test code

#Include <iostream>#Include <math.h>#Include<cstdio>#Include <iomanip>#Include<algorithm>UsingNamespaceStd#Define PI 3.1415926IntMain(){int i =0;Int J =0;int u =0;int v =0;Constint rows =3;const int cols = 3; double Inputdata[rows][cols] = {{89,101,114}, {97,115, 131}, {114,134, 159},}; double Outputdata[rows][cols]; DCT ((double**) Inputdata, (double**) outputdata,rows, cols); IDCT ((double**) Outputdata, (double**) inputdata,rows,cols) ; System ( "pause"); return 0;}         

The results of the program run as follows:

Summary

The code can then be upgraded, no longer using a two-dimensional array as a parameter, but using the Eigen type as a parameter, which makes the code clearer and more convenient in terms of memory management.

Reprint Please specify source: http://www.cnblogs.com/scut-linmaojiang/p/5013590.html

C + + implements discrete cosine transform (parameters are two-dimensional pointers)

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.