﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-x-zone-文章分类-变换算法</title><link>http://www.cppblog.com/cc1983/category/5477.html</link><description>all about c++</description><language>zh-cn</language><lastBuildDate>Sun, 25 May 2008 06:09:11 GMT</lastBuildDate><pubDate>Sun, 25 May 2008 06:09:11 GMT</pubDate><ttl>60</ttl><item><title>DCT</title><link>http://www.cppblog.com/cc1983/articles/DCT.html</link><dc:creator>刘旭</dc:creator><author>刘旭</author><pubDate>Tue, 30 Oct 2007 15:55:00 GMT</pubDate><guid>http://www.cppblog.com/cc1983/articles/DCT.html</guid><wfw:comment>http://www.cppblog.com/cc1983/comments/35554.html</wfw:comment><comments>http://www.cppblog.com/cc1983/articles/DCT.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cc1983/comments/commentRss/35554.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cc1983/services/trackbacks/35554.html</trackback:ping><description><![CDATA[<p>DCT是仅次与K-L变换的信息集中能力优秀的算法，K-L算法需要对每个子图象求变换矩阵,所以DCT是替代它的实用算法。<br><br>1. 8*8 2D DCT 直接变换<br><br>&nbsp;&nbsp; for (u = 0; u &lt; 8; u++)<br>&nbsp;&nbsp; for (v = 0; v &lt; 8; v++)<br>&nbsp; &nbsp;for (x = 0; x &lt; 8; x++)<br>&nbsp;&nbsp; for (y = 0; y &lt; 8; y++)<br>&nbsp;&nbsp; MAT(F,u,v)) += MAT(f,x,y)*c*cos((2*x+1)*u*pi/(2*nrow))*cos((2*y+1)*v*pi/(2*ncol));<br><br>x,y是像素坐标， x,y = 0,1,2,...,7;<br>u,v是像素坐标对应的DCT坐标;<br><br>MAT(m,i,j)表示8*8点矩阵m中第i行第j列的元素,一维算法计算量大,不实用,算法忽略。<br>C(0) = 1/sqrt(2), C(i) = 1&nbsp;&nbsp;&nbsp;&nbsp; i = 1,2,...,7。&nbsp;<br>c = 1/4*C(u)*C(v)<br><br>2. 8*8 2D DCT 分解为一维形式<br><br>&nbsp;&nbsp;for (v = 0; v &lt; 8; v++)<br>&nbsp; for (u = 0; u &lt; 8; u++)<br>&nbsp; for (x = 0; x &lt; 8; x++)<br>&nbsp; MAT(tmpF,u,v) += MAT(f,x,v)*coff*cos((2*x+1)*pi*u/(2*nrow)):<br><br>&nbsp; for (u = 0; u &lt; 8; u++)<br>&nbsp; for (v = 0; v &lt; 8; v++)<br>&nbsp; for (y = 0; y &lt; 8; y++)<br>&nbsp; MAT(F,u,v) += coff*MAT(tmpF,u,y)*cos((2*y+1)*pi*v/(2*ncol)):<br><br>coff[0] = 1/sqrt(8);<br>coff[i] = sqrt(2)/sqrt(8); i = 1,2,...,7;<br>算法实现:<br>#define PI 3.14159265358979323846<br>void DCT(double *f,&nbsp; double *F, &nbsp;int r)<br>{<br>&nbsp;&nbsp;&nbsp; // 离散余弦变换点数<br>&nbsp;&nbsp;&nbsp; int&nbsp;count;<br>&nbsp;&nbsp;&nbsp; count = 1&lt;&lt;r; // r = 3&nbsp;8*8变换 r = 4 16*16变换<br>&nbsp;&nbsp;&nbsp; // 循环变量<br>&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;m,n,x;<br>&nbsp;&nbsp;&nbsp; // 中间变量<br>&nbsp;&nbsp;&nbsp; double *dTemp = new double[count*count];<br>&nbsp;&nbsp;&nbsp; double *coff = new double[count];</p>
<p>&nbsp;&nbsp;&nbsp; // 设定系数coff<br>&nbsp;&nbsp;&nbsp; coff[0]=1/sqrt(count);<br>&nbsp;&nbsp;&nbsp; for (m = 1;m &lt; count;m++)<br>&nbsp;&nbsp;&nbsp; {&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; coff[m]=sqrt(2)/sqrt(count);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;//dTemp赋初值<br>&nbsp;&nbsp;&nbsp; memset(dTemp, 0, sizeof(double) * count * count);<br>&nbsp;&nbsp;&nbsp; memset(F, 0, sizeof(double) * count * count);<br>&nbsp;&nbsp;&nbsp; // 求F[m,n]&nbsp;<br>&nbsp;&nbsp;&nbsp; for (n = 0; n &lt; count; n++)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (m = 0; m &lt; count; m++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (x = 0; x &lt; count; x++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dTemp[m*count+n] += f[x*count+n]*coff[m]*cos((2*x+1)*PI*m/(2*count));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp; for (m=0; m&lt; count; m++)<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp; for (n = 0; n &lt; count; n++)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (x = 0; x &lt; count; x++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; F[m*count+n] += dTemp[m*count+x]*coff[n]*cos((2*x+1)*PI*n/(2*count));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp; }<br>&nbsp;&nbsp;<br>&nbsp;&nbsp; // 释放内存<br>&nbsp;&nbsp; delete dTemp;<br>&nbsp;&nbsp; delete coff;<br>}</p>
<img src ="http://www.cppblog.com/cc1983/aggbug/35554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cc1983/" target="_blank">刘旭</a> 2007-10-30 23:55 <a href="http://www.cppblog.com/cc1983/articles/DCT.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>