20091ee114_1_CA7_Sol

20091ee114_1_CA7_Sol - UCLA, Electrical Engineering...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 13 UCLA, Electrical Engineering Department Winter Quarter, 2009 EE 114, Part 2: Image Processing Solution to Computer Assignment #7 1. m_files Here are several m_files that are used: writeGPJ.m function result = WriteGPJ(filename, image, quant) % compress an image into GPJ file format result = -1; fid = fopen(filename, 'w'); if fid == -1 disp('Error: Can not open file.'); return; end %initialization [y_dim, x_dim] = size(image); Q = GenerateQ(quant); %write file header % write file id tag fprintf(fid, 'GPJFile'); fwrite(fid, 0, 'uchar'); % write file option code (always 0 for this version) fprintf(fid, '%3d', 0); fwrite(fid, 0, 'uchar'); % write quant fprintf(fid, '%3d', quant); fwrite(fid, 0, 'uchar'); % write x dim fprintf(fid, '%7d', x_dim); fwrite(fid, 0, 'uchar'); % write y dim fprintf(fid, '%7d', y_dim); fwrite(fid, 0, 'uchar'); for y = 1:8:y_dim for x = 1:8:x_dim % crop out the block block = double(image(y:y+7, x:x+7)) - 128; % DCT transformation coeff = dct2(block); % Quantization qcoeff = round(coeff ./ Q); % zig-zag scan seq = Zig(qcoeff); % code the coefficients
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EncodeCoeff(fid, seq); end end fclose(fid); result = 0; return; readGPJ.m function image = ReadGPJ(filename) % decompress an image from GPJ file format image = -1; fid = fopen(filename, 'r'); if fid == -1 disp('Error: Can not open file.'); return; end 6 % read the file header % check the file id tag and version number field = fscanf(fid, '%s', 1); idTag = sscanf(field, '%s\0', 1); field = fscanf(fid, '%s', 1); version = sscanf(field, '%d\0', 1); if ((idTag ~= 'GPJFile') | (version ~= 0)) disp('Error: GPJ file corrupted or wrong version.'); return; end % read the parameters field = fscanf(fid, '%s', 1); quant = sscanf(field, '%d\0'); field = fscanf(fid, '%s', 1); x_dim = sscanf(field, '%d\0'); field = fscanf(fid, '%s', 1); y_dim = sscanf(field, '%d\0'); fseek(fid, 32, 'bof'); % initialization image = zeros(y_dim, x_dim); Q = GenerateQ(quant); for y = 1:8:y_dim for x = 1:8:x_dim % decode the coefficients seq = DecodeCoeff(fid); % inverse zig-zag scan qcoeff = Zag(seq); % De-quantization coeff = qcoeff .* Q; % DCT transformation block = idct2(coeff); % paint the block image(y:y+7, x:x+7) = block + 128; end end fclose(fid);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/10/2010 for the course EE EE114 taught by Professor Vanderschaar during the Spring '09 term at UCLA.

Page1 / 13

20091ee114_1_CA7_Sol - UCLA, Electrical Engineering...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online