Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Read Image
- A = imread('test1.jpg');
- %to Double
- A=im2double(A);
- %Get Each Channel
- redChannel = A(:, :, 1);
- greenChannel=A(:,:,2);
- blueChannel=A(:,:,3);
- %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=double(redChannel(i,j));
- green = double(greenChannel(i,j));
- blue = double(blueChannel(i,j));
- %check if we r in the first row,we will use X=A
- elseif i==1
- red=double(redChannel(i,j-1));
- green = double(greenChannel(i,j-1));
- blue = double(blueChannel(i,j-1));
- %check if we r in the first row,we will use X=B
- elseif j==1
- red=double(redChannel(i-1,j));
- green = double(greenChannel(i-1,j));
- blue = double(blueChannel(i-1,j));
- %Else we will use the equation ,we will use X=(A+B)/2;
- else
- red1=double(redChannel(i,j-1));
- red2=double(redChannel(i-1,j));
- green1=double(greenChannel(i,j-1));
- green2=double(greenChannel(i-1,j));
- blue1=double(blueChannel(i,j-1));
- blue2=double(blueChannel(i-1,j));
- red=(red1+red2)/2;
- green=(green1+green2)/2;
- blue=(blue1+blue2)/2;
- end
- redChannel(i,j)=uint8(red);
- blueChannel(i,j)=uint8(blue);
- greenChannel(i,j)=uint8(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=double(redChannel2(i,j))-(double(redChannel(i,j)));
- diff2=double(greenChannel2(i,j))-(double(greenChannel(i,j)));
- diff3=double(blueChannel2(i,j))-(double(blueChannel(i,j)));
- %Edit the channels with the difference.
- redChannel2(i,j)=uint8(diff1);
- greenChannel2(i,j)=uint8(diff2);
- blueChannel2(i,j)=uint8(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);
- frequency=zeros(1,511);
- for i=1:size(imhuff,1)
- for j=1:size(imhuff,2)
- frequency(imhuff(i,j)+256)=(frequency(imhuff(i,j)+256))+1;
- end
- end
- %get probabilities
- probabilities=frequency/sum(frequency);
- %loop through all probabilities and form codeword for each probabilities
- for index = 1:length(probabilities)
- %create empty codeword for each probabilities
- codewords{index} = [];
- %create set from each probabilities containing eachself as start.
- set_contents{index} = index;
- %probabilities for each index
- set_probabilities(index) = probabilities(index);
- end
- %loop till we have only one set that contain all codewords
- while length(set_contents) > 1
- %sort return the sorted array(temp) and the index array(sorted_indicies)
- [temp, sorted_indices] = sort(set_probabilities);
- %zero set contain the set with the least probabilites
- zero_set = set_contents{sorted_indices(1)};
- %zero probability has the probabilities of the least set
- zero_probability = set_probabilities(sorted_indices(1));
- for codeword_index = 1:length(zero_set)
- codewords{zero_set(codeword_index)} = [0,codewords{zero_set(codeword_index)}];
- end
- one_set = set_contents{sorted_indices(2)};
- one_probability = set_probabilities(sorted_indices(2));
- for codeword_index = 1:length(one_set)
- codewords{one_set(codeword_index)} = [codewords{one_set(codeword_index)},1];
- end
- set_contents(sorted_indices(1:2)) = [];
- set_contents{length(set_contents)+1} = [zero_set, one_set];
- set_probabilities(sorted_indices(1:2)) = [];
- set_probabilities(length(set_probabilities)+1) = zero_probability + one_probability;
- end
- for index = 1:length(codewords)
- disp([num2str(index), ' ', num2str(probabilities(index)),' ',num2str(codewords{index}(length(codewords{index}):-1:1))]);
- end
- [h,w] = size(dec2bin(redChannel2));
- before = h*w
- av_length = 0;
- for index = 1:length(codewords)
- av_length = av_length + length(codewords{index});
- end
- av_length
- disp(['The Huffman coding rate is: ',num2str(before/av_length)]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement