DCT离散余弦变换编程

离散傅里叶变换和离散余弦变换公式如下:

--------------------------------------------------------------------------------------------------------------------------------------------

DCT离散余弦变换编程_第1张图片

--------------------------------------------------------------------------------------------------------------------------------------------

DCT离散余弦变换编程_第2张图片

----------------------------------编程实现-----------------------------------------------------------------------------------------------

可以通过以下3种方式实现:


1. 按照公式定义来编程计算

2. 采用蝶形运算来编程实现

3. 调用现成的fftw算法来实现


对于第1,2两种方法,有很多资料介绍,对于需要快速开发的程序,则可直接调用fftw函数库实现,据说该库函数计算傅里叶变换是全地球最快的。

FFTW中文参考: http://blog.csdn.net/congwulong/article/details/7576012

下面代码是调用fftw的一个示例:

#include 
#include 
#include 
#include 
#include "fftw3.h"

#pragma comment(lib,"libfftw3-3.lib")
//#pragma comment(lib,"libfftw3l-3.lib")
//#pragma comment(lib,"libfftw3f-3.lib")

int main() //DCT测试
{
	int M=3;
	int N=3;

//	int M=4;
//	int N=4;

	double *in;
	double *out;

	double In[3][3]={{0,2,4},{6,1,3},{5,7,4}};
//	float In[4][4] = { {20,2,4,7},  {6,1,3,8},  {5,7,4,0},  {11,2,12,17} };
	fftw_plan p;

	in = (double*)malloc(sizeof(double)*M*N);
	out = (double*)malloc(sizeof(double)*M*N);

	//-------------- DCT: in ---------------------
	double sum=0;
	printf("in[][]:\n");
	for(int i=0; i

------------------下面是调用OpenCV的示例----------------------------

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include 

int main() //DCT测试
{
	int M = 4;
	int N = 4;
	float data0[] = { 20,2,4,7,  6,1,3,8,  5,7,4,0,  11,2,12,17 };
	float data1[] = { 20,2,4,7,  6,1,3,8,  5,7,4,0,  11,2,12,17 };
	float data2[] = { 20,2,4,7,  6,1,3,8,  5,7,4,0,  11,2,12,17 };

	CvMat a, b, c;
	a = cvMat(M,N,CV_32FC1,data0);
	b = cvMat(M,N,CV_32FC1,data1);
	c = cvMat(M,N,CV_32FC1,data2);
	
	CvMat *d = cvCreateMat(M, N, CV_32FC1);
	for(int i=0; i}
/*
可以看出OpenCV的DCT变换是按照离散余弦变换标准定义计算的:
--------------- a --------------------
a[0,0]=20.00; a[0,1]=2.00; a[0,2]=4.00; a[0,3]=7.00;
a[1,0]=6.00; a[1,1]=1.00; a[1,2]=3.00; a[1,3]=8.00;
a[2,0]=5.00; a[2,1]=7.00; a[2,2]=4.00; a[2,3]=0.00;
a[3,0]=11.00; a[3,1]=2.00; a[3,2]=12.00; a[3,3]=17.00;
--------------- b --------------------
b[0,0]=27.25; b[0,1]=1.78; b[0,2]=9.75; b[0,3]=4.95;
b[1,0]=-2.67; b[1,1]=7.92; b[1,2]=4.45; b[1,3]=0.32;
b[2,0]=10.25; b[2,1]=-0.45; b[2,2]=7.75; b[2,3]=4.79;
b[3,0]=-1.87; b[3,1]=7.82; b[3,2]=-4.28; b[3,3]=-0.92;
--------------- c --------------------
c[0,0]=20.00; c[0,1]=2.00; c[0,2]=4.00; c[0,3]=7.00;
c[1,0]=6.00; c[1,1]=1.00; c[1,2]=3.00; c[1,3]=8.00;
c[2,0]=5.00; c[2,1]=7.00; c[2,2]=4.00; c[2,3]=-0.00;
c[3,0]=11.00; c[3,1]=2.00; c[3,2]=12.00; c[3,3]=17.00;
请按任意键继续. . .
*/

 
  

 
 

你可能感兴趣的:(DCT离散余弦变换编程)