Advertisement
yaramohamed1

Untitled

May 15th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. function compress_image()
  2. %Read Image
  3. A = imread('test1.jpg');
  4.  
  5. %Get Each Channel
  6. redChannel = A(:,:,1);
  7. greenChannel=A(:,:,2);
  8. blueChannel=A(:,:,3);
  9. heightImage=size(A,1)
  10. widthImage=size(A,2)
  11.  
  12. %Loop through the image
  13. for i=1:size(A,1)
  14. for j=1:size(A,2)
  15. %get the pixel 3 values
  16. red = double(redChannel(i,j));
  17. green = double(greenChannel(i,j));
  18. blue = double(blueChannel(i,j));
  19.  
  20. %|------|--------|
  21. %| A | C |
  22. %| B | X |
  23. %|---------------|
  24. %we want to get predicative value for x ,if we r in the first row we will use the equation that x=A
  25. %if we are in the first column ,we use the equation that x=B
  26. %else we use the equation that x=(A+b)/2;
  27.  
  28. %check if we r in the first pixel,we will use X=X
  29. if (i==1)&&(j==1)
  30. red= redChannel(i,j);
  31. green = greenChannel(i,j);
  32. blue = blueChannel(i,j);
  33. %check if we r in the first row,we will use X=A
  34. elseif i==1
  35. red= redChannel(i,j-1);
  36. green = greenChannel(i,j-1);
  37. blue = blueChannel(i,j-1);
  38. %check if we r in the first row,we will use X=B
  39. elseif j==1
  40. red= redChannel(i-1,j);
  41. green = greenChannel(i-1,j);
  42. blue = blueChannel(i-1,j);
  43. %Else we will use the equation ,we will use X=(A+B)/2;
  44. else
  45. red1= redChannel(i,j-1);
  46. red2= redChannel(i-1,j);
  47. green1= greenChannel(i,j-1);
  48. green2= greenChannel(i-1,j);
  49. blue1= blueChannel(i,j-1);
  50. blue2= blueChannel(i-1,j);
  51. red =(red1+red2)/2;
  52. green =(green1+green2)/2;
  53. blue =(blue1+blue2)/2;
  54. end
  55. redChannel(i,j)= red;
  56. blueChannel(i,j)= blue;
  57. greenChannel(i,j)= green;
  58. end
  59. end
  60.  
  61. %Now we need to get the difference between the original image and predicative image
  62. redChannel2 = A(:,:,1);
  63. greenChannel2 = A(:,:,2);
  64. blueChannel2 = A(:,:,3);
  65. for i=1:size(A,1)
  66. for j=1:size(A,2)
  67. %get difference between each pixel
  68. diff1= redChannel2(i,j)- redChannel(i,j);
  69. diff2= greenChannel2(i,j)- greenChannel(i,j);
  70. diff3= blueChannel2(i,j)- blueChannel(i,j);
  71. %Edit the channels with the difference.
  72. redChannel2(i,j)= diff1;
  73. greenChannel2(i,j)= diff2;
  74. blueChannel2(i,j)= diff3;
  75. end
  76. end
  77. %Edit the original image with difference values to work on HUFFMAN on it.
  78. A(:,:,1) = redChannel2;
  79. A(:,:,2) = greenChannel2;
  80. A(:,:,3) = blueChannel2;
  81. %imwrite(A,'test1.jpg');
  82.  
  83. %-----------------------------------------------------------------------------------------
  84. %read image
  85. %imhuff=imread('test1.jpg');
  86. %imhuff=double(imhuff);
  87. imhuff = A;
  88.  
  89. frequency=zeros(1,256);
  90. %frequency=zeros(1,511);
  91. for i=1:size(imhuff,1)
  92. for j=1:size(imhuff,2)
  93. frequency(imhuff(i,j)+1)=(frequency(imhuff(i,j)+1))+1;
  94. %frequency(imhuff(i,j)+256)=(frequency(imhuff(i,j)+256))+1;
  95. end
  96. end
  97. %get probabilities
  98. probabilities = frequency./sum(frequency);
  99. symbols = 0:255;
  100. %symbols = -255:255;
  101. set(0,'RecursionLimit',520);
  102.  
  103. %-----------------------------------------------------------------------------------------
  104. % Create Huffman tree
  105. for l=1:length(symbols) % create a vector of nodes (leaves), one for each letter
  106. leaves(l).val = symbols(l);
  107. leaves(l).zero= '';
  108. leaves(l).one='';
  109. leaves(l).prob = probabilities(l);
  110. end
  111.  
  112. % combine the two nodes with lowest probability to a new node with the summed prob.
  113. % repeat until only one node is left
  114. while length(leaves)>1
  115. [~,I]=sort(probabilities);
  116. probabilities = [probabilities(I(1))+probabilities(I(2)) probabilities(I(3:end))];
  117. node.zero = leaves(I(1));
  118. node.one = leaves(I(2));
  119. node.prob = probabilities(1);
  120. node.val = '';
  121. leaves = [node leaves(I(3:end))];
  122. end
  123.  
  124. % pass through the tree,
  125. % remove unnecessary information
  126. % and create table recursively (depth first)
  127. table.val={}; table.code={};
  128. [tree,table] = descend(leaves(1),table,'');
  129.  
  130. %-----------------------------------------------------------------------------------------
  131. %Compress (Encode) RGB Channels:
  132. input_R = redChannel2(:);
  133. input_G = greenChannel2(:);
  134. input_B = blueChannel2(:);
  135. compressed_R = '';
  136. for l=1:length(input_R),
  137. compressed_R = strcat(compressed_R,table.code(ismember(cell2mat(table.val),input_R(l))));
  138. end
  139. compressed_R = char(compressed_R);
  140.  
  141. compressed_G = '';
  142. for l=1:length(input_G),
  143. compressed_G = strcat(compressed_G,table.code(ismember(cell2mat(table.val),input_G(l))));
  144. end
  145. compressed_G = char(compressed_G);
  146.  
  147. compressed_B = '';
  148. for l=1:length(input_B),
  149. compressed_B = strcat(compressed_B,table.code(ismember(cell2mat(table.val),input_B(l))));
  150. end
  151. compressed_B = char(compressed_B);
  152.  
  153. %-----------------------------------------------------------------------------------------
  154. %Calculate Compression Ratio
  155. % Red Channel:
  156. [hb,wb] = size(dec2bin(redChannel2));
  157. before = hb*wb;
  158. [ha,wa] = size(compressed_R);
  159. after = ha*wa;
  160. CR_Red = before/after;
  161. % Green Channel:
  162. [hb,wb] = size(dec2bin(greenChannel2));
  163. before = hb*wb;
  164. [ha,wa] = size(compressed_G);
  165. after = ha*wa;
  166. CR_Green = before/after;
  167. % Blue Channel:
  168. [hb,wb] = size(dec2bin(blueChannel2));
  169. before = hb*wb;
  170. [ha,wa] = size(compressed_B);
  171. after = ha*wa;
  172. CR_Blue = before/after;
  173.  
  174. %Display
  175. disp(['The Huffman coding rate (Red): ',num2str(CR_Red)]);
  176. disp(['The Huffman coding rate (Green): ',num2str(CR_Green)]);
  177. disp(['The Huffman coding rate (Blue): ',num2str(CR_Blue)]);
  178.  
  179. decoded_img={};
  180. [decoded_img] = decode_tree(tree,compressed_R);
  181. size(decoded_img)
  182. B = reshape(decoded_img,[heightImage,widthImage]);
  183. %K = mat2gray(B);
  184. %imshow(K)
  185. end
  186.  
  187.  
  188. %-----------------------------------------------------------------------------------------
  189. function [tree, table] = descend(oldtree, oldtable, code)
  190. table = oldtable;
  191. if(~isempty(oldtree.val))
  192. tree.val = oldtree.val;
  193. table.val{end+1} = oldtree.val;
  194. table.code{end+1} = code;
  195. else
  196. [tree0, table] = descend(oldtree.zero, table, strcat(code,'0'));
  197. [tree1, table] = descend(oldtree.one, table, strcat(code,'1'));
  198. tree.zero=tree0;
  199. tree.one= tree1;
  200. end
  201. end
  202.  
  203.  
  204. function [decoded_img]=decode_tree(tree,compressed_R)
  205. tempTree=tree;
  206. n=1;
  207.  
  208. for i=1:length(compressed_R)
  209. if(~isempty(tempTree))
  210. decoded_img(n)=tempTree;
  211. n=n+1;
  212. tempTree=tree;
  213. end
  214.  
  215. if(compressed_R(i)==0)
  216. tempTree=tempTree.zero;
  217. else
  218. tempTree=tempTree.one;
  219. end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement