[英]DCT and IDCT implementation in Java for 2-dimensional matrix
我已經對dct和idct都有實現,但是盡管進行了適當的優化,但隨着矩陣大小的增加,它們變得非常慢。 沒有人知道兩者的更快實現或任何為二維案例提供更快實現的Java庫。 謝謝
public final double[][] initCoefficients(double[][] c)
{
final int N = c.length;
final double value = 1/Math.sqrt(2.0);
for (int i=1; i<N; i++)
{
for (int j=1; j<N; j++)
{
c[i][j]=1;
}
}
for (int i=0; i<N; i++)
{
c[i][0] = value;
c[0][i] = value;
}
c[0][0] = 0.5;
return c;
}
/* Computes the discrete cosine transform
*/
public final double[][] forwardDCT(double[][] input)
{
final int N = input.length;
final double mathPI = Math.PI;
final int halfN = N/2;
final double doubN = 2.0*N;
double[][] c = new double[N][N];
c = initCoefficients(c);
double[][] output = new double[N][N];
for (int u=0; u<N; u++)
{
double temp_u = u*mathPI;
for (int v=0; v<N; v++)
{
double temp_v = v*mathPI;
double sum = 0.0;
for (int x=0; x<N; x++)
{
int temp_x = 2*x+1;
for (int y=0; y<N; y++)
{
sum += input[x][y] * Math.cos((temp_x/doubN)*temp_u) * Math.cos(((2*y+1)/doubN)*temp_v);
}
}
sum *= c[u][v]/ halfN;
output[u][v] = sum;
}
}
return output;
}
/*
* Computes the inverse discrete cosine transform
*/
public final double[][] inverseDCT(double[][] input)
{
final int N = input.length;
final double mathPI = Math.PI;
final int halfN = N/2;
final double doubN = 2.0*N;
double[][] c = new double[N][N];
c = initCoefficients(c);
double[][] output = new double[N][N];
for (int x=0; x<N; x++)
{
int temp_x = 2*x+1;
for (int y=0; y<N; y++)
{
int temp_y = 2*y+1;
double sum = 0.0;
for (int u=0; u<N; u++)
{
double temp_u = u*mathPI;
for (int v=0; v<N; v++)
{
sum += c[u][v] * input[u][v] * Math.cos((temp_x/doubN)*temp_u) * Math.cos((temp_y/doubN)*v*mathPI);
}
}
sum /= halfN;
output[x][y] = sum;
}
}
return output;
}
現在,它是一個O(n 4 )算法,四個嵌套循環都進行n
次迭代。 可分離性將其降低到O(n 3 )(如果您足夠勇敢嘗試快速余弦變換,則可降低到O(n 2 log n))。 實際上,它甚至比使用2D公式還要簡單,因為它就是這樣:
或者(可選)使兩個部分完全相同:
轉置意味着它第二次真正在做列,並且在兩次轉置中彼此撤消。
因此,余弦。 你注意到
因為要計算內部(循環)局部變量的余弦,所以預計算余弦似乎很困難
DCT是否有最大尺寸? 如果使用整數是可以的(通常是圖像處理的情況),那么您可以在其中找到大小為4、8、16和32的一些快速實現: https : //github.com/flanglet/kanzi/tree/master / JAVA / SRC /坎茲/變換
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.