DCT变换(matlab)

本文通过两个示例展示了离散余弦变换(DCT)在图像处理中的应用,包括对实际图片进行变换及压缩,以及对人造图像进行低频和高频变换分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://hi.baidu.com/huguosheng/item/06393d5744ee9f3795eb05f2


example 1(use real image):

A=imread('class_f.png');
imshow(A) %A is unit8(0,255)
C=dct2(A); %进行余弦变换
figure;
B=log(abs(C));
imshow(B)
colormap(jet(64)); %显示为64级灰度
colorbar; %显示颜色条,显示变换后的系数分布
C(abs(C)<10)=0; %将DCT变换后的系数值小于10的元素设为0
%E=idct2(C);
D=idct2(C)./255; %对DCT变换值归一化,进行余弦反变换???
figure;
imshow(D) ;
% imshow(uint8(E)); is the same as D=idct2(C)./255
% imshow(E,[]); is the same as D=idct2(C)./255

FF=abs(C)<10; %Compute the number of elements which are smaller than 10
sum(sum(FF)) %result:56632
GG=abs(C)>10; %Compute the number of elements which are larger than 10
sum(sum(GG)) %result:16025

example 2(use artifical image):
constant Image(low frequency image)
A=ones(5);
B=dct2(A)
A =

     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
     1     1     1     1     1
after DCT transformation
     5     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
C=idct2(B) %reconstruction according to B


random image(high frequency image)
A=randn(5)
B=dct2(A)
A =

   -0.4326    1.1909   -0.1867    0.1139    0.2944
   -1.6656    1.1892    0.7258    1.0668   -1.3362
    0.1253   -0.0376   -0.5883    0.0593    0.7143
    0.2877    0.3273    2.1832   -0.0956    1.6236
   -1.1465    0.1746   -0.1364   -0.8323   -0.6918

ans =

    0.5853   -0.5033   -1.3505   -1.2524   -0.3519
    0.2492    0.1007   -0.4273    0.1201   -1.5079
   -0.8317    0.4357   -0.4183   -0.5794   -0.4022
    1.7697   -0.3482    1.3882   -0.3871    1.4934
   -1.0525    0.1744    1.7976    0.0521   -0.4997
B(abs(B)<0.1)=0;//0.1 is threshold
C=idct2(B);//reconstruction
### MATLAB 实现 DCT 变换的示例代码 离散余弦变换(Discrete Cosine Transform, DCT)是一种常见的信号和图像处理技术,在图像压缩领域有广泛应用。以下是使用 MATLAB 实现 DCT 变换的一个完整示例。 #### 图像预处理 在进行 DCT 变换前,通常需要对图像进行一些预处理操作,例如将彩色图像转换为灰度图像、调整图像大小等[^1]: ```matlab % 读取原始图像并将其转换为灰度图像 originalImage = imread('image.jpg'); % 替换 'image.jpg' 为实际文件名 grayImage = rgb2gray(originalImage); % 将图像调整为适合 DCT 处理的尺寸(如 8x8 块) resizedImage = imresize(grayImage, [256, 256]); % 调整为 256x256 的分辨率 ``` #### 使用 `dctmtx` 和 `blkproc` 函数实现 DCT 变换 MATLAB 提供了内置函数 `dctmtx` 来生成 DCT 矩阵,并可以通过 `blkproc` 或其他方法对图像分块处理[^4]: ```matlab % 定义 DCT 矩阵 T = dctmtx(8); % 创建一个 8x8 的 DCT 矩阵 % 对图像按 8x8 块应用 DCT 变换 dctCoefficients = blkproc(resizedImage, [8 8], @(block_struct) T * double(block_struct.data) * T'); % 显示 DCT 变换后的结果 figure; imshow(log(abs(dctCoefficients)), []), colormap(jet); title('DCT Coefficients'); colorbar; ``` #### 自定义 DCT 变换函数 如果需要手动实现 DCT 变换,可以编写自定义函数[^3]。以下是一个简单的二维 DCT 变换实现: ```matlab function Y = my_dct(X) [M, N] = size(X); C = zeros(M, N); for u = 0:M-1 for v = 0:N-1 sum_val = 0; for x = 0:M-1 for y = 0:N-1 sum_val = sum_val + X(x+1, y+1) * cos((pi/M)*(x+0.5)*u) * cos((pi/N)*(y+0.5)*v); end end Cu = sqrt(1/2)^(u==0); Cv = sqrt(1/2)^(v==0); C(u+1,v+1) = (Cu*Cv*sum_val)/(sqrt(M*N)); end end Y = C; end ``` 调用该函数时,可将图像分割为多个小块并逐块处理: ```matlab % 将图像分割为 8x8 块并应用自定义 DCT 函数 blocks = mat2cell(double(resizedImage), 8*ones(1,size(resizedImage,1)/8), 8*ones(1,size(resizedImage,2)/8)); for i = 1:numel(blocks) blocks{i} = my_dct(blocks{i}); end % 合并处理后的块 dctResult = cell2mat(blocks); % 显示结果 figure; imshow(log(abs(dctResult)), []), colormap(jet); title('Custom DCT Result'); colorbar; ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值