C + + implements discrete cosine transform

Source: Internet
Author: User
Tags cos

C + + implements discrete cosine transform

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 Transform void DCT( double * * input, double * * output, int row, int c OL) {cout<<"Test in DCT"<<endl;DoubleALPHA, BETA;intU =0;intv =0;inti =0;intj =0; for(U =0; U < row; u++) { for(v =0; v < col; v++) {if(U = =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); }DoubleTMP =0.0; for(i =0; i < row; i++) { for(j =0; J < Col; J + +) {tmp + = * (Double*) input + row*i + j) *Cos((2*i+1) *u*pi/(2.0* row)) *Cos((2*j+1) *v*pi/(2.0* col)); }            }            *((Double*) output + row*u + V) = ALPHA * BETA * TMP; }    }cout<<"The result of the DCT:"<< Endl; for(intm =0; M < row; m++) { for(intn=0; n < col; n++) {cout&LT;&LT;SETW (8) << * (Double*) output + row*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; for(u= 0;u< row;u++)            { for(v = 0; v < col; v++) {if(u= = 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+ row*u+ V) *Cos((2*i+1) *u*pi/(2.0 * row)) *Cos((2*j+1) *v*pi/(2.0 * col)); }            }* ((double*) output + row*i + j) = tmp;}} cout <<"The result of IDCT:"<< Endl; for(intm= 0;m< row;m++)    { for(intN= 0;N< col;N+ +) {cout &LT;&LT;SETW (8) << * ((double*) output + row*m+N) <<"\ T";    } cout << Endl; }}

Test code

#include <iostream>#include <math.h>#include<cstdio>#include <iomanip>#include<algorithm>using namespace STD;#define PI 3.1415926 int main() {inti =0;intj =0;intU =0;intv =0;Const introws =3;Const intcols =3;DoubleInputdata[rows][cols] = {{ the,101, the},        { the, the,131},        { the,134,159},    };DoubleOutputdata[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

In the test code given in this article, the input array has the same number of rows and columns. When the input array has a different number of functions and columns, the result of the output is incorrect. However, when the function is designed to be a two-dimensional array, the number of rows and columns of the input array is not the same, and it can run normally. I have been thinking for a long time and have not come up with specific reasons. It is now likely that the reason for this is that when the number of rows and columns of the array is not the same, the addressing has been wrong? The specific reason remains to be verified.

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

C + + implements discrete cosine transform

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.