Image compression huffman coding

조회 수: 207 (최근 30일)
lavanya vs
lavanya vs 2019년 4월 29일
답변: Saherish Pathan 2022년 1월 10일
%clearing all variableas and screen
clear all;
close all;
clc;
%Reading image
a=imread('jpeg-image-compression-1-638.JPG');
figure,imshow(a)
%converting an image to grayscale
I=rgb2gray(a);
%size of the image
[m,n]=size(I);
Totalcount=m*n;
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255
k=I==i;
count(cnt)=sum(k(:))
%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;
%Symbols for an image
symbols = [0:255];
%Huffman code Dictionary
dict = huffmandict(symbols,pro);
%function which converts array to vector
vec_size = 1;
for p = 1:m
for q = 1:n
newvec(vec_size) = I(p,q);
vec_size = vec_size+1;
end
end
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);
%vector to array conversion
dec_row=sqrt(length(dhsig));
dec_col=dec_row;
%variables using to convert vector 2 array
arr_row = 1;
arr_col = 1;
vec_si = 1;
for x = 1:m
for y = 1:n
back(x,y)=dhsig(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end
%converting image from grayscale to rgb
[deco, map] = gray2ind(back,256);
RGB = ind2rgb(deco,map);
imwrite(RGB,'decoded.JPG');
%end of the huffman coding
This code works fine but the image decoded is grey in colour. I cannot find anything wrong in it please help
  댓글 수: 4
nor saziana ariani sazali
nor saziana ariani sazali 2021년 6월 24일
Hi, walter..can i know step for storing huffman dictionary after encoding using huffman?

댓글을 달려면 로그인하십시오.

답변 (3개)

Saherish Pathan
Saherish Pathan 2022년 1월 10일
clear all;
close all;
clc;
%Reading image
a=imread('jpeg-image-compression-1-638.JPG');
figure,imshow(a)
%converting an image to grayscale
I=rgb2gray(a);
%size of the image
[m,n]=size(I);
Totalcount=m*n;
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255
k=I==i;
count(cnt)=sum(k(:))
%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;
%Symbols for an image
symbols = [0:255];
%Huffman code Dictionary
dict = huffmandict(symbols,pro);
%function which converts array to vector
vec_size = 1;
for p = 1:m
for q = 1:n
newvec(vec_size) = I(p,q);
vec_size = vec_size+1;
end
end
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);
%vector to array conversion
dec_row=sqrt(length(dhsig));
dec_col=dec_row;
%variables using to convert vector 2 array
arr_row = 1;
arr_col = 1;
vec_si = 1;
for x = 1:m
for y = 1:n
back(x,y)=dhsig(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end
%converting image from grayscale to rgb
[deco, map] = gray2ind(back,256);
RGB = ind2rgb(deco,map);
imwrite(RGB,'decoded.JPG');

Abdel Rahman Bekawi
Abdel Rahman Bekawi 2020년 1월 11일
편집: Abdel Rahman Bekawi 2020년 1월 12일
well done, your code is great!
however, the thing that makes this logical error is that your map channels are equal to each other, hence you will get grayed scale image as well, so to overcome this issue, I recommend you work with indexed images.
% try the following
[indexed, colormap] = rgb2ind(rgbimage, number_of_colors);
% do all the processing on this indexed format and then at the end
RGB = ind2rgb(back, colormap);
% by this you should get a compressed colored image
I hope this helps!
  댓글 수: 3
hamza munawar
hamza munawar 2020년 8월 31일
have anyone done the huffman encoding on a 2D image in matlab???
Walter Roberson
Walter Roberson 2020년 8월 31일
Yes, people have done that. Just reshape the image into a vector, and Huffman encode that. If you save or transmit the encoded image, remember to store the dictionary and the image size so that you can reconstruct afterwards.

댓글을 달려면 로그인하십시오.


maha asiri
maha asiri 2021년 11월 22일
did you find the anwer for this code
couse i want it

카테고리

Help CenterFile Exchange에서 Source Coding에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by