DCT, IDCT变换--C语言实现
博客专区 > tigerBin 的博客 > 博客详情
DCT, IDCT变换--C语言实现
tigerBin 发表于6个月前
DCT, IDCT变换--C语言实现
  • 发表于 6个月前
  • 阅读 34
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

变换矩阵,DCT = IDCT'

static const double MtxDCT[8][8] = 
{
    {0.3536,    0.3536,    0.3536,    0.3536,    0.3536,    0.3536,    0.3536,    0.3536},
    {0.4904,    0.4157,    0.2778,    0.0975,   -0.0975,   -0.2778,   -0.4157,   -0.4904},
    {0.4619,    0.1913,   -0.1913,   -0.4619,   -0.4619,   -0.1913,    0.1913,    0.4619},
    {0.4157,   -0.0975,   -0.4904,   -0.2778,    0.2778,    0.4904,    0.0975,   -0.4157},
    {0.3536,   -0.3536,   -0.3536,    0.3536,    0.3536,   -0.3536,   -0.3536,    0.3536},
    {0.2778,   -0.4904,    0.0975,    0.4157,   -0.4157,   -0.0975,    0.4904,   -0.2778},
    {0.1913,   -0.4619,    0.4619,   -0.1913,   -0.1913,    0.4619,   -0.4619,    0.1913},
    {0.0975,   -0.2778,    0.4157,   -0.4904,    0.4904,   -0.4157,    0.2778,   -0.0975}
};


static const double MtxIDCT[8][8] = 
{
    {0.3536,    0.4904,    0.4619,    0.4157,    0.3536,    0.2778,    0.1913,    0.0975},
    {0.3536,    0.4157,    0.1913,   -0.0975,   -0.3536,   -0.4904,   -0.4619,   -0.2778},
    {0.3536,    0.2778,   -0.1913,   -0.4904,   -0.3536,    0.0975,    0.4619,    0.4157},
    {0.3536,    0.0975,   -0.4619,   -0.2778,    0.3536,    0.4157,   -0.1913,   -0.4904},
    {0.3536,   -0.0975,   -0.4619,    0.2778,    0.3536,   -0.4157,   -0.1913,    0.4904},
    {0.3536,   -0.2778,   -0.1913,    0.4904,   -0.3536,   -0.0975,    0.4619,   -0.4157},
    {0.3536,   -0.4157,    0.1913,    0.0975,   -0.3536,    0.4904,   -0.4619,    0.2778},
    {0.3536,   -0.4904,    0.4619,   -0.4157,    0.3536,   -0.2778,    0.1913,   -0.0975}
};

变换方程: F(u,v) = G * f * G'

逆变换方程: f(x,y) = G' * F * G

其中 G = DCT,  G' = IDCT ,  G 与 G' 互为转置

计算:

class MCU
{
public:
	int *operator[] (int row)
	{
		return data[row];
	}


	int data[8][8];
};


MCU MtxMulI2D(MCU &left, const double right[8][8])
{
	MCU dctBuf;
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			double tempVal = 0.0;
			for (int k = 0; k < 8; k++)
			{
				tempVal += left[i][k] * right[k][j];
			}
			dctBuf[i][j] = round(tempVal);
		}
	}
	return dctBuf;
}

MCU MtxMulD2I(const double left[8][8], MCU &right)
{
	MCU dctBuf;
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			double tempVal = 0.0;
			for (int k = 0; k < 8; k++)
			{
				tempVal += left[i][k] * right[k][j];
			}
			dctBuf[i][j] = round(tempVal);
		}
	}
	return dctBuf;
}

void DCT(MCU &block)
{
	block = MtxMulD2I(MtxDCT, block);
	block = MtxMulI2D(block, MtxIDCT);
}


void IDCT(MCU &block)
{
	block = MtxMulD2I(MtxIDCT, block);
	block = MtxMulI2D(block, MtxDCT);
}

测试:


int data2[8][8] = 
{
 {144,146,149,152,154,156,156,156},
 {148,150,152,154,156,156,156,156},
 {155,156,157,158,158,157,156,155},
 {160,161,161,162,161,159,157,155},
 {163,163,164,163,162,160,158,156},
 {163,163,164,164,162,160,158,157},
 {160,161,162,162,162,161,159,158},
 {158,159,161,161,162,161,159,158}
};

int data1[8][8] = 
{
 {139, 144, 149, 153, 155, 155, 155, 155}, 
 {144, 151, 153, 156, 159, 156, 156, 156}, 
 {150, 155, 160, 163, 158, 156, 156, 156}, 
 {159, 161, 162, 160, 160, 159, 159, 159}, 
 {159, 160, 161, 162, 162, 155, 155, 155}, 
 {161, 161, 161, 161, 160, 157, 157, 157}, 
 {162, 162, 161, 163, 162, 157, 157, 157}, 
 {162, 162, 161, 161, 163, 158, 158, 158} 
};


void Show(MCU &block)
{
	for (int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
			printf("%4d ", block[i][j]);
		}
		printf("\n");
	}
}




int main(int argc, char *argv[])
{
	MCU block;
	for (int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
			block[i][j] = data1[i][j];
		}
	}

	DCT(block);   // 离散余弦变换
	IDCT(block);  // 逆离散余弦变换
	Show(block);
	return 0;
}

最后计算结果与原始数据相差不大

标签: IDCT DCT
共有 人打赏支持
粉丝 0
博文 48
码字总数 14652
×
tigerBin
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: