function compress_image() %Read Image A = imread('test1.jpg'); % A = imresize(A,[10 10]); %Get Each Channel redChannel = A(:,:,1); greenChannel=A(:,:,2); blueChannel=A(:,:,3); heightImage=size(A,1) widthImage=size(A,2) %Loop through the image for i=1:size(A,1) for j=1:size(A,2) %get the pixel 3 values red = double(redChannel(i,j)); green = double(greenChannel(i,j)); blue = double(blueChannel(i,j)); %|------|--------| %| A | C | %| B | X | %|---------------| %we want to get predicative value for x ,if we r in the first row we will use the equation that x=A %if we are in the first column ,we use the equation that x=B %else we use the equation that x=(A+b)/2; %check if we r in the first pixel,we will use X=X if (i==1)&&(j==1) red= redChannel(i,j); green = greenChannel(i,j); blue = blueChannel(i,j); %check if we r in the first row,we will use X=A elseif i==1 red= redChannel(i,j-1); green = greenChannel(i,j-1); blue = blueChannel(i,j-1); %check if we r in the first row,we will use X=B elseif j==1 red= redChannel(i-1,j); green = greenChannel(i-1,j); blue = blueChannel(i-1,j); %Else we will use the equation ,we will use X=(A+B)/2; else red1= redChannel(i,j-1); red2= redChannel(i-1,j); green1= greenChannel(i,j-1); green2= greenChannel(i-1,j); blue1= blueChannel(i,j-1); blue2= blueChannel(i-1,j); red =(red1+red2)/2; green =(green1+green2)/2; blue =(blue1+blue2)/2; end redChannel(i,j)= red; blueChannel(i,j)= blue; greenChannel(i,j)= green; end end %Now we need to get the difference between the original image and predicative image redChannel2 = A(:,:,1); greenChannel2 = A(:,:,2); blueChannel2 = A(:,:,3); for i=1:size(A,1) for j=1:size(A,2) %get difference between each pixel diff1= redChannel2(i,j)- redChannel(i,j); diff2= greenChannel2(i,j)- greenChannel(i,j); diff3= blueChannel2(i,j)- blueChannel(i,j); %Edit the channels with the difference. redChannel2(i,j)= diff1; greenChannel2(i,j)= diff2; blueChannel2(i,j)= diff3; end end %Edit the original image with difference values to work on HUFFMAN on it. A(:,:,1) = redChannel2; A(:,:,2) = greenChannel2; A(:,:,3) = blueChannel2; %imwrite(A,'test1.jpg'); %----------------------------------------------------------------------------------------- %read image %imhuff=imread('test1.jpg'); %imhuff=double(imhuff); imhuff = A; frequency=zeros(1,256); %frequency=zeros(1,511); for i=1:size(imhuff,1) for j=1:size(imhuff,2) frequency(imhuff(i,j)+1)=(frequency(imhuff(i,j)+1))+1; %frequency(imhuff(i,j)+256)=(frequency(imhuff(i,j)+256))+1; end end %get probabilities probabilities = frequency./sum(frequency); symbols = 0:255; %symbols = -255:255; set(0,'RecursionLimit',520); %----------------------------------------------------------------------------------------- % Create Huffman tree for l=1:length(symbols) % create a vector of nodes (leaves), one for each letter leaves(l).val = symbols(l); leaves(l).zero= ''; leaves(l).one=''; leaves(l).prob = probabilities(l); end % combine the two nodes with lowest probability to a new node with the summed prob. % repeat until only one node is left while length(leaves)>1 [~,I]=sort(probabilities); probabilities = [probabilities(I(1))+probabilities(I(2)) probabilities(I(3:end))]; node.zero = leaves(I(1)); node.one = leaves(I(2)); node.prob = probabilities(1); node.val = ''; leaves = [node leaves(I(3:end))]; end % pass through the tree, % remove unnecessary information % and create table recursively (depth first) table.val={}; table.code={}; [tree,table] = descend(leaves(1),table,''); %----------------------------------------------------------------------------------------- %Compress (Encode) RGB Channels: input_R = redChannel2(:); input_G = greenChannel2(:); input_B = blueChannel2(:); compressed_R = ''; for l=1:length(input_R), compressed_R = strcat(compressed_R,table.code(ismember(cell2mat(table.val),input_R(l)))); end compressed_R = char(compressed_R); compressed_G = ''; for l=1:length(input_G), compressed_G = strcat(compressed_G,table.code(ismember(cell2mat(table.val),input_G(l)))); end compressed_G = char(compressed_G); compressed_B = ''; for l=1:length(input_B), compressed_B = strcat(compressed_B,table.code(ismember(cell2mat(table.val),input_B(l)))); end compressed_B = char(compressed_B); %----------------------------------------------------------------------------------------- %Calculate Compression Ratio % Red Channel: [hb,wb] = size(dec2bin(redChannel2)); before = hb*wb; [ha,wa] = size(compressed_R); after = ha*wa; CR_Red = before/after; % Green Channel: [hb,wb] = size(dec2bin(greenChannel2)); before = hb*wb; [ha,wa] = size(compressed_G); after = ha*wa; CR_Green = before/after; % Blue Channel: [hb,wb] = size(dec2bin(blueChannel2)); before = hb*wb; [ha,wa] = size(compressed_B); after = ha*wa; CR_Blue = before/after; %Display disp(['The Huffman coding rate (Red): ',num2str(CR_Red)]); disp(['The Huffman coding rate (Green): ',num2str(CR_Green)]); disp(['The Huffman coding rate (Blue): ',num2str(CR_Blue)]); decoded_img_r={}; [decoded_img_r] = decode_tree(leaves(1),compressed_R); size(decoded_img_r) BR = reshape(decoded_img_r,[heightImage,widthImage]); decoded_img_g={}; [decoded_img_g] = decode_tree(leaves(1),compressed_G); size(decoded_img_g) BG = reshape(decoded_img_g,[heightImage,widthImage]); decoded_img_b={}; [decoded_img_b] = decode_tree(leaves(1),compressed_B); size(decoded_img_b) BB = reshape(decoded_img_b,[heightImage,widthImage]); [m,n]=size(decoded_img_r); my_image= zeros(m,n); my_image(:,:,1)=decoded_img_r; my_image(:,:,2)=decoded_img_g; my_image(:,:,3)=decoded_img_b; imshow(my_image) end %----------------------------------------------------------------------------------------- function [tree, table] = descend(oldtree, oldtable, code) table = oldtable; if(~isempty(oldtree.val)) tree.val = oldtree.val; table.val{end+1} = oldtree.val; table.code{end+1} = code; else [tree0, table] = descend(oldtree.zero, table, strcat(code,'0')); [tree1, table] = descend(oldtree.one, table, strcat(code,'1')); tree.zero=tree0; tree.one= tree1; end end function [decoded_img]=decode_tree(tree,compressed_R) tempTree=tree; n=1; for i=1:length(compressed_R) if(~isempty(tempTree.val)) decoded_img(n)=tempTree.val; n=n+1; tempTree=tree; end s1='0'; s2=compressed_R(i); tf = strcmp(s1,s2); if(tf==1) tempTree=tempTree.zero; else tempTree=tempTree.one; end end decoded_img(n)=tempTree.val; end