OpenCV离散余弦变换原理与源码
程序员文章站
2022-07-14 22:18:54
...
环境配置:VS2012 OpenCV2.4.9
代码:
#include "highgui.h"
#include "math.h"
#include "cv.h"
#include "stdlib.h"
using namespace cv;
void DCT(Mat &src, Mat &dst)
{
double pi = 3.141592657;
Mat C_Mat(src.rows,src.cols, CV_64FC1);
Mat CT_Mat(src.rows,src.cols, CV_64FC1);
for(int j=0;j<C_Mat.rows;j++)
C_Mat.at<double>(0,j) = sqrt(2.0/(C_Mat.rows))*sqrt(1.0/2);
for(int i=1;i<C_Mat.rows;i++)
for(int j=0;j<C_Mat.cols;j++)
C_Mat.at<double>(i,j) = sqrt(2.0/(C_Mat.rows))*cos(pi*(i-1)*(2*j-1)/2/(C_Mat.rows));
CT_Mat = C_Mat.t();
dst = C_Mat*src*CT_Mat;
}
void main()
{
Mat img = imread("D:/Pic/4.jpg",0);
//imshow("img",img);
Mat src(img.rows,img.cols,CV_64FC1);
img.convertTo(src,CV_64FC1);
//imshow("src",src);
Mat dst(src.rows,src.cols,CV_64FC1);
DCT(src,dst);
imshow("dst",dst);
cvWaitKey(0);
}