Você está na página 1de 6

JPEG Image Compression Implemented in Matlab

By Michael Christensen

Abstract: In this project I attempted to implement basic JPEG compression using only basic Matlab functions. This included going from a basic grayscale bitmap image all the ay to a fully encoded file readable by standard image readers. I ill sho that I ha!e implemented the majority of the project" including much of the final binary coding. #lthough I ne!er obtained a fully completed image from my functions" I came !ery close. Results: In this section I ill present the steps of obtaining a finali$ed JPEG file and ho I implemented each step in matlab. I ill comment on possible impro!ements and mista%es made in each case. The code used is attached at the end of this report. &tep'( Con!erting the base image to )*) matrices" +CT transform" ,uanti$ing These steps ere relati!ely easy" especially in matlab" hich is specifically set up to or% ith matrices. The -.+ discrete cosine transform is done simply ith the dct-/0 command. #fter splitting the matri* into )*) matrices and performing the +CT" a simple piece ise di!ision by the ,uanti$ation matri* obtains the ,uanti$ed matrices needed for the ne*t step. &tep -( 1ig.1ag Encoding of 2uanti$ed Matrices I found no matlab implementation for this function" so I rote one myself. I too% ad!antage of the fact that each diagonal ro has addresses that add up to the same number. +epending on hether the number is e!en or odd determined the direction of the iteration through the ro . The code I rote is able to $ig.$ag through any matri* of e,ual height and idth. This could be useful if one anted to e*periment ith de!iding images into matrices larger than )*).

&tep 3( Con!ersion of ,uanti$ed !ectors into the JPEG defined bitstream 4or this step" I started ith an old implementation of the default #C code ritten by 5u 6en 6u. #fter updating the code to or% ith Matlab 7 I modified the code to encode the first number in the incoming !ector ith the default +C code" the table for hich I added to the file. The function returns a completed bitstream to correspond to the input of the ,uanti$ed !ector. I am sorry to say I cannot guarantee that the code contained in !ecenc.m is at all reliable. I tested single !ectors ith standard e*amples and obtained the correct result. 6o e!er" ha!ing ne!er obtained a final result" I cannot guarantee that this function complies ith the JPEG standard. &tep 8( Construction of the JPEG 4ile header" 9riting the 4ile Being relati!ely ine*perienced ith coding in general" this step presented me ith the most trouble o!erall. It too% se!eral hours to determine ho to encode a binary !ector into a file. It too% e!en longer to reali$e that each byte encoded into the file as being represented ith the least significant bits on the left side. #fter o!ercoming that obstacle" I as faced ith the tas% of constructing a file header for my bit stream. The JPEG standard only goes as far as con!ersion to the binary bit stream. 9hile that process is ell defined in scientific papers" the construction of a JPEG file header is not. In the matlab file head.m" I tried to e*press hat I learned about the process in the most e*pressi!e ay possible. It seems that the JPEG file is bro%en into many bloc%s. Each bloc% begins ith t o bytes" the first being 44 in he*adecimal and the second being :;;< here different :;;<s denoting different bloc%s. The second part of each bloc% is the length" in bytes" of the bloc% including the t o length bytes. The rest of the bloc% contains the data as defined by the bloc% type. #s of this riting" I am in the middle of coding head.m" but ill be unable to finish due to time constraints. I am confident" though" that I ha!e a !ery good understanding of ho the rest of the header construction ould proceed.

Conclusion: 9hile not completing the goal I set out to achie!e" I ha!e demonstrated that con!ersion from a grayscale image to the JPEG encoded binary bit stream is a fairly simple and straightfor ard process. It comes as no surprise to me that the file I=> as the most challenging part of the process. #s for here to go from here" I hope to complete the project in my free time and publish the matlab files on the internet. I belie!e that hile there are far more po erful and efficient implementations of the JPEG algorithm out there" other students li%e myself ould benefit from a simple and straightfor ard implementation that emphasi$es step.by step e*planations of hat is going on and hy.

function b = jpeg(file); % % % % % % % % % %

Usage : Input an image file and recieve the jpeg implementation of the file. b: file: output binary stream input file by ichael !hristensen "ecember #$% &''(

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% % % )tep #: *ead in the file% obtain parameters % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% + = imread(file); %reads file into a matri, - = imfinfo(file); %reads file info %convert to .!b!r %if -./ormat==0bmp0 % +=rgb&ycbcr(+) 1idth=-.2idth; height=-.3eight; %detirmine number of 4,4 matrices% use ceil to round up 2=ceil(1idth54); 3=ceil(height54); %create a matri, of 6eros and add the image to it to fill out the 4,4 %matrices (matri, 1ill stay the same si6e if height and 1idth are %divisible by 4 I=6eros(374%274%0uint40); I(#:height%#:1idth)=+(#:height%#:1idth); %divide numbers into 2,3 4,4 matrices 8=6eros(3%2%4%4); for 9=#:3 for :=#:2 for j=#:4 for ;=#:4 8(9%:%j%;)=I((9<#)74=j%(:<#)74=;); end end end end

%define ?=@... #A #& #$ #$ #4 &$ $B D&

luminance >uanti6ation matri, ## #& #C #D && C( A$ B& #' #$ #A && CD (( D4 B( #A #B &$ &B (A A$ 4D B4 &$ &A $' (# A4 4# #'C ##& $' (4 (D 4D #'B #'$ #&# #'' (# A' AB 4' #'C ##C #&' #'C A# (( (A A& DD B& #'# BBE;

b=@E; vprev=@E; vcurrent=@E; for 9=#:3 for :=#:2 temp=6eros(4%4%0uint40);%create temporary matri, temp(:%:)=8(9%:%:%:);%add values from current 4,4 sector temp=double(temp);%convert numbers to double format(floating point) temp=temp<#&4;%shift mean to 6ero temp=dct&(temp);%perform &<" cosine transfer function temp=temp.5?;%devide by >uanti6ation matri, temp=round(temp);%round off the >uanti6ed matri, vcurrent=6ig6ag(temp);%convert >uanti6ed matri, to #<" vector vcurrent=shorten(vcurrent);%remove e,tra 6eros from vector if 9==# FF :==# b=@b vecenc(vcurrent)E; else vcurrent(#)=vcurrent(#) < vprev(#);%ta;e difference of first value b=@b vecenc(vcurrent)E; end vprev=vcurrent; end end b=@head(?) b # # # # # # # # denote GH/ Gnd of /ile # ' ' # # ' # #E;%last t1o bytes:ff dB

Você também pode gostar