function reconstructgray(p) %This takes a full image, Quantizes, then reconstructs %in gray-scale based upon resolution factor p. %Constructs the image into a matrix. x = imread('C:\Users\cnelso17\Desktop\project5.jpg'); %Defines the dimensions of the matrix. [height, width, dim] = size(x); %Finds out how many 8x8 blocks there are. h8=floor(height/8); w8=floor(width/8); h88=h8*8; w88=w8*8; x = double(x); %Converts to double percision. %Restricts the image to dimensions divisible by 8. x = x(1:h88,1:w88,:); r = x(:,:,1); %Defining Red. g = x(:,:,2); %Defining Green. b = x(:,:,3); %Defining Blue. xgray = .2126*r + .7152*g + .0722*b; %Convert to gray-scale. xgray = uint8(xgray); %Proper units. imagesc(xgray);colormap(gray);figure(2); %Displays full image. %Quanzitization Matricies. Uncomment to use. %Hilbert Matrix Q = p*8./hilb(8); %JPEG Standard Matrix: % 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]; C = getC(); q = QuantizeFull(xgray,Q,h8,w8); d = deQuantizeFull(q,C,Q,h8,w8); imagesc(d,[0 255]);colormap(gray); %dct function. function C = getC() for i = 1:8 for j = 1:8 C(i,j) = cos((i-1)*(2*j-1)*pi/(2*8)); end end C = sqrt(2/8)*C; C(1,:) = C(1,:)/sqrt(2); %Quantizes the full image. function img = QuantizeFull(xgray,Q,h8,w8) for i = 1:h8 for j = 1:w8 xb = xgray((i-1)*8+1:i*8,(j-1)*8+1:j*8); xb = double(xb); Xc = xb - 128; Y = dct(dct(Xc')'); Yq = round(Y./Q); img((i-1)*8+1:i*8,(j-1)*8+1:j*8) = Yq; end end %DeQuantizes the full image. function img = deQuantizeFull(yq,C,Q,h8,w8) for i = 1:h8 for j = 1:w8 Yq = yq((i-1)*8+1:i*8,(j-1)*8+1:j*8); Ydq = Yq.*Q; Xdq = C'*Ydq*C; Xe = Xdq + 128; Xf = uint8(Xe); img((i-1)*8+1:i*8,(j-1)*8+1:j*8) = Xf; end end