在传统的图像,视频的后处理阶段,一般会涉及到图像大小的缩放问题。这样的操作是为了适配不同屏幕分辨率的大小。例如,对于高档相机拍摄的照片,一般都很大,而要在普通显示器上显示,则要在解码图像数据之后再做缩小操作才能显示到屏幕上。对于手机屏幕,更是如此。那么,能否在图像解码的过程中实现图片的缩小?答案是肯定的。网上开源的jpeg项目早就实现了频域下采样技术。美图秀秀,QQlive等多款桌面图片应用软件也采用了这项技术,并宣称对大图的加载速度快了好几倍。不过,QQlive居然直接使用了这个库!
通过在视频解码中使用频域下采样技术,我们在400M主频的手机上实现了720p的流畅解码,在400M主频的CPU上实现了1080p的视频解码,但不是很流畅。
频域下采样技术并不是多么高深的算法,下面的matlab代码十分简洁的描述了图像的频域下采样过程。
1. DCT域下采样算法
% dct频率域下采样算法 % 根据Dugad方法实现 %程序作者: [email protected] %2010-10-12 I=imread('456.png'); I=rgb2gray(I); figure(1); imshow(I); [width,height] = size(I); block_size_x = 8; block_size_y = 8; x_block_num = width/block_size_x; y_block_num = height/block_size_y; z_dst = zeros(width/2,height/2); pfun1 = @dct2; I_freq = blkproc(I,[8 8],pfun1); for i = 1: x_block_num for j = 1 : y_block_num P = I_freq((i-1)*8+1:(i-1)*8+4,(j-1)*8+1:(j-1)*8+4); P = P/2.0; z_dst( (i-1)*4 +1 : (i-1)*4 +4, (j-1)*4 +1 : (j-1)*4 +4) = P; end end pfun2 = @idct2; J = blkproc(z_dst,[4 4],pfun2); J = uint8(round(J)); figure(2); imshow(J);
2. 另外一种DCT频域下采样算法
% dct频率域下采样算法,根据下面论文提到的算法实现 % 一种高效的DCT域图像下采样方法 中国图像图形学报 2005年4月 %程序作者: [email protected] ,2010-10
clc; clear; I=imread('456.png'); I=rgb2gray(I); figure(1); imshow(I); [width,height] = size(I); block_size_x = 8*2; block_size_y = 8*2; x_block_num = width/block_size_x; y_block_num = height/block_size_y; C16 = dctmtx(16); C16LL = C16(1:8,1:8); C16LH = C16(1:8,9:16); C16HL = C16(9:16,1:8); C16HH = C16(9:16,9:16); C8 = dctmtx(8); P = C16LL*C8'; P = P/sqrt(2); Q = C16LH*C8'; Q = Q/sqrt(2); E = (P + Q)/2; F = (P - Q)/2; z_dst = zeros(width/2,height/2); pfun1 = @dct2; I_freq = blkproc(I,[8 8],pfun1); for i = 1: x_block_num for j = 1 : y_block_num X1 = I_freq((i-1)*16+1:(i-1)*16+8,(j-1)*16+1:(j-1)*16+8); X2 = I_freq((i-1)*16+1:(i-1)*16+8,(j-1)*16+9:(j-1)*16+16); X3 = I_freq((i-1)*16+9:(i-1)*16+16,(j-1)*16+1:(j-1)*16+8); X4 = I_freq((i-1)*16+9:(i-1)*16+16,(j-1)*16+9:(j-1)*16+16); A = E*(X1+X3)+F*(X1-X3); B = E*(X2+X4)+F*(X2-X4); XLL = (A + B) * E' + (A - B) * F'; z_dst( (i-1)*8 +1 : (i-1)*8 +8, (j-1)*8 +1 : (j-1)*8 +8) = XLL; end end pfun2 = @idct2; J = blkproc(z_dst,[8 8],pfun2); J = uint8(round(J)); figure(2); imshow(J);