Advertisement
yaramohamed1

Untitled

Apr 30th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. %Read Image
  2. A = imread('test1.jpg');
  3. %to Double
  4. A=im2double(A);
  5. %Get Each Channel
  6. redChannel = A(:, :, 1);
  7. greenChannel=A(:,:,2);
  8. blueChannel=A(:,:,3);
  9. %Loop through the image
  10. for i=1:size(A,1)
  11. for j=1:size(A,2)
  12. %get the pixel 3 values
  13. red = double(redChannel(i,j));
  14. green = double(greenChannel(i,j));
  15. blue = double(blueChannel(i,j));
  16. %|------|--------|
  17. %| A | C |
  18. %| B | X |
  19. %| | |
  20. %|---------------|
  21. %we want to get predicative value for x ,if we r in the first row we will use the equation that x=A
  22. %if we are in the first column ,we use the equation that x=B
  23. %else we use the equation that x=(A+b)/2;
  24.  
  25. %check if we r in the first pixel,we will use X=X
  26. if (i==1)&&(j==1)
  27. red=double(redChannel(i,j));
  28. green = double(greenChannel(i,j));
  29. blue = double(blueChannel(i,j));
  30. %check if we r in the first row,we will use X=A
  31. elseif i==1
  32. red=double(redChannel(i,j-1));
  33. green = double(greenChannel(i,j-1));
  34. blue = double(blueChannel(i,j-1));
  35. %check if we r in the first row,we will use X=B
  36. elseif j==1
  37. red=double(redChannel(i-1,j));
  38. green = double(greenChannel(i-1,j));
  39. blue = double(blueChannel(i-1,j));
  40. %Else we will use the equation ,we will use X=(A+B)/2;
  41. else
  42. red1=double(redChannel(i,j-1));
  43. red2=double(redChannel(i-1,j));
  44. green1=double(greenChannel(i,j-1));
  45. green2=double(greenChannel(i-1,j));
  46. blue1=double(blueChannel(i,j-1));
  47. blue2=double(blueChannel(i-1,j));
  48. red=(red1+red2)/2;
  49. green=(green1+green2)/2;
  50. blue=(blue1+blue2)/2;
  51. end
  52. redChannel(i,j)=uint8(red);
  53. blueChannel(i,j)=uint8(blue);
  54. greenChannel(i,j)=uint8(green);
  55. end
  56. end
  57. %Now we need to get the difference between the original image and predicative image
  58. redChannel2 = A(:, :, 1);
  59. greenChannel2=A(:,:,2);
  60. blueChannel2=A(:,:,3);
  61. for i=1:size(A,1)
  62. for j=1:size(A,2)
  63. %get difference between each pixel
  64. diff1=double(redChannel2(i,j))-(double(redChannel(i,j)));
  65. diff2=double(greenChannel2(i,j))-(double(greenChannel(i,j)));
  66. diff3=double(blueChannel2(i,j))-(double(blueChannel(i,j)));
  67. %Edit the channels with the difference.
  68. redChannel2(i,j)=uint8(diff1);
  69. greenChannel2(i,j)=uint8(diff2);
  70. blueChannel2(i,j)=uint8(diff3);
  71. end
  72. end
  73. %Edit the original image with difference values to work on HUFFMAN on it.
  74. A(:,:,1)=redChannel2;
  75. A(:,:,2)=greenChannel2;
  76. A(:,:,3)=blueChannel2;
  77. imwrite(A,'test1.jpg');
  78. %-----------------------------------------------------------------------------------------
  79. %read image
  80. imhuff=imread('test1.jpg');
  81. imhuff=double(imhuff);
  82.  
  83. frequency=zeros(1,511);
  84. for i=1:size(imhuff,1)
  85. for j=1:size(imhuff,2)
  86. frequency(imhuff(i,j)+256)=(frequency(imhuff(i,j)+256))+1;
  87. end
  88. end
  89.  
  90. %get probabilities
  91. probabilities=frequency/sum(frequency);
  92.  
  93.  
  94. %loop through all probabilities and form codeword for each probabilities
  95. for index = 1:length(probabilities)
  96. %create empty codeword for each probabilities
  97. codewords{index} = [];
  98. %create set from each probabilities containing eachself as start.
  99. set_contents{index} = index;
  100. %probabilities for each index
  101. set_probabilities(index) = probabilities(index);
  102. end
  103.  
  104. %loop till we have only one set that contain all codewords
  105. while length(set_contents) > 1
  106. %sort return the sorted array(temp) and the index array(sorted_indicies)
  107. [temp, sorted_indices] = sort(set_probabilities);
  108. %zero set contain the set with the least probabilites
  109. zero_set = set_contents{sorted_indices(1)};
  110. %zero probability has the probabilities of the least set
  111. zero_probability = set_probabilities(sorted_indices(1));
  112.  
  113.  
  114. for codeword_index = 1:length(zero_set)
  115. codewords{zero_set(codeword_index)} = [0,codewords{zero_set(codeword_index)}];
  116.  
  117. end
  118.  
  119.  
  120.  
  121.  
  122. one_set = set_contents{sorted_indices(2)};
  123.  
  124. one_probability = set_probabilities(sorted_indices(2));
  125.  
  126.  
  127.  
  128. for codeword_index = 1:length(one_set)
  129.  
  130. codewords{one_set(codeword_index)} = [codewords{one_set(codeword_index)},1];
  131. end
  132.  
  133.  
  134.  
  135. set_contents(sorted_indices(1:2)) = [];
  136.  
  137. set_contents{length(set_contents)+1} = [zero_set, one_set];
  138.  
  139.  
  140. set_probabilities(sorted_indices(1:2)) = [];
  141.  
  142. set_probabilities(length(set_probabilities)+1) = zero_probability + one_probability;
  143.  
  144. end
  145.  
  146.  
  147.  
  148. for index = 1:length(codewords)
  149. disp([num2str(index), ' ', num2str(probabilities(index)),' ',num2str(codewords{index}(length(codewords{index}):-1:1))]);
  150. end
  151. [h,w] = size(dec2bin(redChannel2));
  152. before = h*w
  153. av_length = 0;
  154. for index = 1:length(codewords)
  155. av_length = av_length + length(codewords{index});
  156. end
  157. av_length
  158.  
  159. disp(['The Huffman coding rate is: ',num2str(before/av_length)]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement