function Project5JPEG(image) Acolor = imagegrey(image); r = Acolor(:,:,1); g = Acolor(:,:,2); b = Acolor(:,:,3); AStart = uint8(cat(3, r, g, b)); imagesc(AStart, [0,255]); y = 0.299*r+0.587*g+0.114*b; u = b-y; v = r-y; Y = dereconstruct(y); V = dereconstruct1(v); U = dereconstruct1(u); B = U+Y; R = V+Y; G = (Y-0.299*R-0.114*B)/0.587; % R = dereconstruct1(r); % G = dereconstruct1(g); % B = dereconstruct1(b); %Special function cat for concatinating back to RGB figure; AFinal = uint8(cat(3, R, G, B)); imagesc(AFinal, [0,255]); end function B = dereconstruct(A) sizeA = size(A); B = []; for i=1:sizeA(1)/8 for j =1:sizeA(2)/8 Ab = A((i-1)*8+1:8*i,(j-1)*8+1:8*j); % Unsupress this to preview 8x8 bit % if i==sizeA(1)/8 && j == sizeA(2)/8 % imagesc(Ab, [0, 255]); colormap(gray); % end Xf = Qdct(Ab); B((i-1)*8+1:8*i,(j-1)*8+1:8*j) = Xf; % if i==sizeA(1)/8 && j == sizeA(2)/8 % imagesc(Xf, [0, 255]); colormap(gray); % end end end end function B = dereconstruct1(A) sizeA = size(A); B = []; for i=1:sizeA(1)/8 for j =1:sizeA(2)/8 Ab = A((i-1)*8+1:8*i,(j-1)*8+1:8*j); % Unsupress this to preview 8x8 bit % if i==sizeA(1)/8 && j == sizeA(2)/8 % imagesc(Ab, [0, 255]); colormap(gray); % end Xf = Qdct1(Ab); B((i-1)*8+1:8*i,(j-1)*8+1:8*j) = Xf; % if i==sizeA(1)/8 && j == sizeA(2)/8 % imagesc(Xf, [0, 255]); colormap(gray); % end end end end function Acolor = imagegrey(image) A = imread(image); A = double(A); r=A(:,:,1); g=A(:,:,2); b=A(:,:,3); Agray=0.2126*r+0.7152*g+0.0722*b; Acolor = []; Acolor(:,:,1) = r; Acolor(:,:,2) = g; Acolor(:,:,3) = b; Acolor(:,:,4) = Agray; %Suppress below this to suppress grey image % imagesc(uint8(Agray)); % colormap(gray); % imagesc(Agray);colormap(gray); end function Xf=Qdct(X) [m,n]=size(X);n=m; [m,n]=size(X);n=m; for i=1:n for j=1:n C(i,j)=cos((i-1)*(2*j-1)*pi/(2*n)); end end C = sqrt(2/n)*C; C(1,:) = C(1,:)/sqrt(2); p = 1; % Q = p*8./hilb(8); Q = p*[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]; Xd = double(X); Xc = Xd - 128; Y = C*Xc*C'; %Y = dct(dct(Xc')'); Yq = round(Y./Q); Ydq = Yq.*Q; Xdq = C'*Ydq*C; %Xdq = idct(idct(Ydq')'); Xe = Xdq+128; Xf=Xe; end function Xf=Qdct1(X) [m,n]=size(X);n=m; for i=1:n for j=1:n C(i,j)=cos((i-1)*(2*j-1)*pi/(2*n)); end end C = sqrt(2/n)*C; C(1,:) = C(1,:)/sqrt(2); p = 1; % Q = p*8./hilb(8); Q = [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]; Xd = double(X); Xc = Xd - 128; Y = C*Xc*C'; %Y = dct(dct(Xc')'); Yq = round(Y./Q); Ydq = Yq.*Q; Xdq = C'*Ydq*C; %Xdq = idct(idct(Ydq')'); Xe = Xdq+128; Xf=Xe; end