资源描述:
《彩色图像空间转换代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、图像篡改检测Matlab源代码说明(1)彩色图像空间转换代码(RGB空间转换为YUV空间)clc;clearall;closeall;img=imread('3.jpg');%figure;imshow(img);title('原图');img_5=img;img_ycbcr=rgb2ycbcr(img);%rgb->yuvfigure;subplot(121);imshow(img);title('原始图像');subplot(122);imshow(img_ycbcr);title('YUV空间图');图
2、1彩色图像空间转换(2)图像分块(分为16*16子块)[row,col,i]=size(img_ycbcr);%对图像进行扩展row_expand=ceil(row/16)*16;%行数上取整再乘16,及扩展成16的倍数ifmod(row,16)~=0%行数不是16的倍数,用最后一行进行扩展fori=row:row_expandimg_ycbcr(i,:,:)=img_ycbcr(row,:,:);endendcol_expand=ceil(col/16)*16;%列数上取整ifmod(col,16)~=0%
3、列数不是16的倍数,用最后一列进行扩展forj=col:col_expandimg_ycbcr(:,j,:)=img_ycbcr(:,col,:);endend(3)对各个分量(Y,Cr,Cb)进行离散余弦变换量化%对各分量进行4:2:0采样Y=img_ycbcr(:,:,1);%Y分量figure;subplot(231);imshow(Y);title('原Y分量');Cb=img_ycbcr(:,:,2);%Cb分量Cr=img_ycbcr(:,:,3);%Cr分量Cb=zeros(row_expand
4、/2,col_expand/2);Cr=zeros(row_expand/2,col_expand/2);fori=1:row_expand/2forj=1:2:col_expand/2-1%奇数Cb(i,j)=double(img_ycbcr(i*2-1,j*2-1,2));Cr(i,j)=double(img_ycbcr(i*2-1,j*2+1,3));endendfori=1:row_expand/2%偶数forj=2:2:col_expand/2Cb(i,j)=double(img_ycbcr(i*2
5、-1,j*2-2,2));Cr(i,j)=double(img_ycbcr(i*2-1,j*2,3));endendsubplot(232);imshow(uint8(Cb));title('原Cb分量');%Cb分量subplot(233);imshow(uint8(Cr));title('原Cr分量');%Cr分量Y_Table=[1611101624405161121214192658605514131624405769561417222951878062182237566810910377243555
6、648110411392496478871031211201017292959811210010399];%亮度量化表CbCr_Table=[17,18,24,47,99,99,99,99;18,21,26,66,99,99,99,99;24,26,56,99,99,99,99,99;47,66,99,99,99,99,99,99;99,99,99,99,99,99,99,99;99,99,99,99,99,99,99,99;99,99,99,99,99,99,99,99;99,99,99,99,99,99,
7、99,99];%色差量化表Qua_Factor=0.2;%量化因子%对Y分量进行处理Qua_Matrix=Qua_Factor*Y_Table;%Y量化矩阵Y=blkproc(Y,[88],'dct2(x)');%DCT变换Y=blkproc(Y,[88],'round(x./P1)',Qua_Matrix);%量化Y=blkproc(Y,[88],'x.*P1',Qua_Matrix);%反量化Y=blkproc(Y,[88],'idct2(x)');%反DCT变换subplot(234);imshow(u
8、int8(Y));title('Y分量量化后');%对Cb分量进行处理Qua_Matrixcc=0.2*CbCr_Table;%Cb质量因子选取为5,并不采用Qua_Factor的数值Cb=blkproc(Cb,[88],'dct2(x)');%DCT变换Cb=blkproc(Cb,[88],'round(x./P1)',Qua_Matrixcc);%量化Cb=blkproc(Cb,[88],'x.