(Matlab program attached) (1) Image compression based on DCT coding: display gray-scale image inverse cosine transform recovery map DCT transform map cosine transform coefficient map

Question 1: Randomly select an entire image (named "x.jpg"), write a Matlab program to display grayscale image, inverse cosine transform recovery map, DCT transform map, cosine transform coefficient map, and compare with the original image.

    clc
	clear
	close all;%清理Matlab窗口
	A=imread('D:\x.jpg');%读取图像x.jpg(引号内是图像在计算机内的存储路径)
	I=rgb2gray(A);%RGB图像转灰度图像
	DCT=dct2(I);%DCT变换
	DCT(abs(DCT)<10)=0;%将小于10DCT余弦变换系数值置为0
	reI=idct2(DCT);%DCT变换
	subplot(2,2,1);%绘图
	imshow(I,[0,255]);
	title('灰度图像');
	subplot(2,2,2);
	imshow(reI,[0,255]);
	title('IDCT灰度图像');
	subplot(2,2,3);
	imshow(DCT,[0,255]);
	title('DCT变换图');
	subplot(2,2,4);
	imshow(log(abs(DCT)),[]);
	title('余弦变换系数');

The programming result is as shown in the figure below. According to the programming result, set the DCT cosine transform coefficient value less than 10 to 0 ("DCT(abs(DCT)<10)=0;"). The difference between the IDCT grayscale image and the original grayscale image Not big, the image restoration effect is better.
Insert picture description here

Question 2: On the basis of question 1, change the transformation matrix coefficients and observe the changes in DCT processing.

    clc
	clear
	close all;%清理Matlab窗口
	A=imread('D:\x.jpg');%读取图像x.jpg(引号内是图像在计算机内的存储路径)
	I=rgb2gray(A);%RGB图像转灰度图像
	DCT=dct2(I);%DCT变换
	DCT500=dct2(I);%DCT变换
	DCT(abs(DCT)<10)=0;%将小于10DCT余弦变换系数值置为0
	DCT500(abs(DCT500)<500)=0;%将小于500DCT余弦变换系数值置为0
	reI=idct2(DCT);%DCT变换
	reI500=idct2(DCT500);%DCT变换
	subplot(2,2,1);%绘图
	imshow(I,[0,255]);
	title('DCT<10 灰度图像');
	subplot(2,2,2);
	imshow(reI,[0,255]);
	title('DCT<10 IDCT灰度图像');
	subplot(2,2,3);
	imshow(reI500,[0,255]);
	title('DCT<500 IDCT灰度图像');

The programming results are shown in the figure below. According to the comparison of the programming results, it can be clearly seen that the IDCT grayscale image is compared with the original after the DCT cosine transform coefficient value less than 500 is set to 0 ("DCT(abs(DCT)<500)=0;") Compared with the gray-scale image, obvious distortion occurs, and the image restoration effect is poor.
Insert picture description here

Guess you like

Origin blog.csdn.net/Echoshit8/article/details/112535561