function changetogray(p) %This takes an 8x8 portion of the input picture %and converts the image to gray-scale %utilizing the loss of quality variable p. %Constructs the image into a matrix. x = imread('C:\Users\cnelso17\Desktop\project5.jpg'); x = double(x); %Define in double percision. x = x(200:207,200:207,:); %8x8 block. r = x(:,:,1); %Defining Red. g = x(:,:,2); %Defining Green. b = x(:,:,3); %Defining Blue. %Converts to gray-scale. xgray = .2126*r + .7152*g + .0722*b; xgray = uint8(xgray); %Proper units. imagesc(xgray); colormap(gray);figure(2); %Displays image. %Choosing the quantize matrix. Uncomment to select. %Hildberg 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 = Quantize(xgray,Q); d = deQuantize(q,C,Q); 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 8x8. function img = Quantize(xb,Q) xb = double(xb); Xc = xb - 128; Y = dct(dct(Xc')'); Yq = round(Y./Q) img = Yq; %DeQuantizes the 8x8. function img = deQuantize(Yq,C,Q) Ydq = Yq.*Q; Xdq = C'*Ydq*C; Xe = Xdq + 128; Xf = uint8(Xe); img = Xf;