Advertisement
yaramohamed1

Untitled

May 15th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1. function compress_image()
  2. %Read Image
  3. A = imread('test1.jpg');
  4. % A = imresize(A,[10 10]);
  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_r={};
  180. [decoded_img_r] = decode_tree(leaves(1),compressed_R);
  181. size(decoded_img_r)
  182. BR = reshape(decoded_img_r,[heightImage,widthImage]);
  183.  
  184. decoded_img_g={};
  185. [decoded_img_g] = decode_tree(leaves(1),compressed_G);
  186. size(decoded_img_g)
  187. BG = reshape(decoded_img_g,[heightImage,widthImage]);
  188.  
  189.  
  190. decoded_img_b={};
  191. [decoded_img_b] = decode_tree(leaves(1),compressed_B);
  192. size(decoded_img_b)
  193. BB = reshape(decoded_img_b,[heightImage,widthImage]);
  194.  
  195. redChannels=BR;
  196.  
  197. blueChannels= BB;
  198. greenChannels= BG;
  199.  
  200.  
  201. for i=1:heightImage
  202. for j=1:widthImage
  203. %get the pixel 3 values
  204. red = double(BR(i,j));
  205. green = double(BG(i,j));
  206. blue = double(BB(i,j));
  207.  
  208. %|------|--------|
  209. %| A | C |
  210. %| B | X |
  211. %|---------------|
  212. %we want to get predicative value for x ,if we r in the first row we will use the equation that x=A
  213. %if we are in the first column ,we use the equation that x=B
  214. %else we use the equation that x=(A+b)/2;
  215.  
  216. %check if we r in the first pixel,we will use X=X
  217. if (i==1)&&(j==1)
  218. red= BR(i,j);
  219. green = BG(i,j);
  220. blue = BB(i,j);
  221. %check if we r in the first row,we will use X=A
  222. elseif i==1
  223. red= BR(i,j-1);
  224. green = BG(i,j-1);
  225. blue = BB(i,j-1);
  226. %check if we r in the first row,we will use X=B
  227. elseif j==1
  228. red= BR(i-1,j);
  229. green = BG(i-1,j);
  230. blue = BB(i-1,j);
  231. %Else we will use the equation ,we will use X=(A+B)/2;
  232. else
  233. red1= BR(i,j-1);
  234. red2= BR(i-1,j);
  235. green1= BG(i,j-1);
  236. green2= BG(i-1,j);
  237. blue1= BB(i,j-1);
  238. blue2= BB(i-1,j);
  239. red =(red1+red2)/2;
  240. green =(green1+green2)/2;
  241. blue =(blue1+blue2)/2;
  242. end
  243. redChannels(i,j)= red;
  244. blueChannels(i,j)= blue;
  245. greenChannels(i,j)= green;
  246. end
  247. end
  248.  
  249.  
  250. %Now we need to get the difference between the original image and predicative image
  251. for i=1:heightImage
  252. for j=1:widthImage
  253. %get difference between each pixel
  254. sum1= BR(i,j)+ redChannels(i,j);
  255. sum2= BG(i,j)+greenChannels(i,j);
  256. sum3= BB(i,j)+blueChannels(i,j);
  257. %Edit the channels with the difference.
  258. BR(i,j)= sum1;
  259. BG(i,j)= sum2;
  260. BB(i,j)= sum3;
  261. end
  262. end
  263. my_image = cat(3,BR,BG,BB);
  264. imshow(my_image)
  265. end
  266.  
  267.  
  268. %-----------------------------------------------------------------------------------------
  269. function [tree, table] = descend(oldtree, oldtable, code)
  270. table = oldtable;
  271. if(~isempty(oldtree.val))
  272. tree.val = oldtree.val;
  273. table.val{end+1} = oldtree.val;
  274. table.code{end+1} = code;
  275. else
  276. [tree0, table] = descend(oldtree.zero, table, strcat(code,'0'));
  277. [tree1, table] = descend(oldtree.one, table, strcat(code,'1'));
  278. tree.zero=tree0;
  279. tree.one= tree1;
  280. end
  281. end
  282.  
  283.  
  284. function [decoded_img]=decode_tree(tree,compressed_R)
  285. tempTree=tree;
  286. n=1;
  287. for i=1:length(compressed_R)
  288. if(~isempty(tempTree.val))
  289. decoded_img(n)=tempTree.val;
  290. n=n+1;
  291. tempTree=tree;
  292. end
  293. s1='0';
  294. s2=compressed_R(i);
  295. tf = strcmp(s1,s2);
  296. if(tf==1)
  297. tempTree=tempTree.zero;
  298. else
  299. tempTree=tempTree.one;
  300. end
  301.  
  302. end
  303. decoded_img(n)=tempTree.val;
  304. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement