A list,

1 Image Segmentation 2 Convert color space to RGB->YCbCr 3 Discrete cosine transform 4 Data quantization 5 Haverman code

Ii. Source code

function JPEGEncodeDecode
%UNTITLED7 Summary of this function goes here
%   Detailed explanation goes here
 
img=imread('1.jpg');
subplot(121); imshow(img); title('original'); Img_ycbcr = rgb2yCBcr (img); % rgb->yuv [row,col,~]=size(img_ycbcr); % Indicates the number of rows and columns to be fetched3A channel is1Column % extends the image row_EXPAND =ceil(row/16) *16; % of the number of rows16, and extend to16A multiple ofif mod(row,16)~=0% row number is not16Is extended by the last linefor i=row:row_expand
        img_ycbcr(i,:,:)=img_ycbcr(row,:,:);
    end
end
col_expand=ceil(col/16) *16; % columns are roundedif mod(col,16)~=0% column number is not16Is extended by the last columnforj=col:col_expand img_ycbcr(:,j,:)=img_ycbcr(:,col,:); End end % For Y,Cb,Cr components4:2:0Sampling Y = img_ycbcr (:, :,1); % Y component Cb = zeros (row_expand /2,col_expand/2);      
Cr=zeros(row_expand/2,col_expand/2); % Cr componentfor i=1:row_expand/2
    for j=1:2:col_expand/2- 1Cb(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));     
    end
end
for i=1:row_expand/2
    for j=2:2:col_expand/2Cb(I,j)=double(img_ycbcr(i*2- 1,j*22 -.2));     
        Cr(i,j)=double(img_ycbcr(i*2- 1,j*2.3)); End end % code each of the three color components Y_Table=[16  11  10  16  24  40  51  61
    12  12  14  19  26  58  60  55
    14  13  16  24  40  57  69  56
    14  17  22  29  51  87  80  62
    18  22  37  56  68 109 103  77
    24  35  55  64  81 104 113  92
    49  64  78  87 103 121 120 101
    72  92  95  98 112 100 103  99]; 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.99.99]; % color difference quantization table Qua_Factor=0.5; % quantization factor, the minimum is0.01And the maximum is255, it is recommended that the0.5and3% for the three channels respectively DCT and quantization Y_dct_q=Dct_Quantize(Y,Qua_Factor,Y_Table); Cb_dct_q=Dct_Quantize(Cb,Qua_Factor,CbCr_Table); Cr_dct_q=Dct_Quantize(Cr,Qua_Factor,CbCr_Table); % inverse quantization and inverse DCT for the three channels Y_in_q_dct=Inverse_Quantize_Dct(Y_dct_q,Qua_Factor,Y_Table); Cb_in_q_dct=Inverse_Quantize_Dct(Cb_dct_q,Qua_Factor,CbCr_Table); Cr_in_q_dct=Inverse_Quantize_Dct(Cr_dct_q,Qua_Factor,CbCr_Table); YCbCr_in(:,:,1)=Y_in_q_dct;
for i=1:row_expand/2
    for j=1:col_expand/2
        YCbCr_in(2*i- 1.2*j- 1.2)=Cb_in_q_dct(i,j);
        YCbCr_in(2*i- 1.2*j,2)=Cb_in_q_dct(i,j);
        YCbCr_in(2*i,2*j- 1.2)=Cb_in_q_dct(i,j);
        YCbCr_in(2*i,2*j,2)=Cb_in_q_dct(i,j);
       
        YCbCr_in(2*i- 1.2*j- 1.3)=Cr_in_q_dct(i,j);
        YCbCr_in(2*i- 1.2*j,3)=Cr_in_q_dct(i,j);
        YCbCr_in(2*i,2*j- 1.3)=Cr_in_q_dct(i,j);
        YCbCr_in(2*i,2*j,3)=Cr_in_q_dct(i,j);
    end
end
function [ Matrix ] = Inverse_Quantize_Dct( I,Qua_Factor,Qua_Table )
%UNTITLED3 Summary of thisfunction goes here % Detailed explanation goes here Qua_Matrix=Qua_Factor.*Qua_Table; % invert matrix I=blkproc(I,[8 8].'x.*P1',Qua_Matrix); [row,column]=size(I); I=blkproc(I,[8 8].'idct2(x)');   
 
I=uint8(I+128);
for i=1:row
    for j=1:column
        if I(i,j)>255
            I(i,j)=255;
        elseif I(i,j)< 0I(i,j)=0;
        end
    end
end
Copy the code

Third, the operation result

Fourth, note

Version: 2014 a