Image compression huffman coding

%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

5 comentarios

Bhaumik chaudhari
Bhaumik chaudhari el 22 de Sept. de 2020
hey , can you send me the code that is working for image processing huffman coding
email id:bhaumikchaudhari225@gmail.com
Walter Roberson
Walter Roberson el 23 de Sept. de 2020
  1. Read the image.
  2. reshape the image to be a vector.
  3. Use histcounts or histc to count the number of occurances of each of the bytes
  4. throw away any entries that have a count of 0 (but keep a list of what the original value is for each)
  5. Create the huffman dictionary.
  6. Use the huffman encoding function.
  7. Store the image size and the huffman dictionary and the huffman encoding vector efficiently .Most people miss this step.
nor saziana ariani sazali
nor saziana ariani sazali el 24 de Jun. de 2021
Hi, walter..can i know step for storing huffman dictionary after encoding using huffman?
maha asiri
maha asiri el 22 de Nov. de 2021
Movida: Walter Roberson el 10 de Ag. de 2024
did you find the anwer for this code
couse i want it

Iniciar sesión para comentar.

Respuestas (3)

Saherish Pathan
Saherish Pathan el 10 de En. de 2022
Editada: Walter Roberson el 10 de Ag. de 2024
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 el 11 de En. de 2020
Editada: Abdel Rahman Bekawi el 12 de En. de 2020
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 comentarios

Walter Roberson
Walter Roberson el 12 de En. de 2020
Note that if you are calculating compression efficiency or are expecting to be able to save the compressed image and restore it at a later time, then you should also be storing the rgb table and the Huffman dictionary
hamza munawar
hamza munawar el 31 de Ag. de 2020
have anyone done the huffman encoding on a 2D image in matlab???
Walter Roberson
Walter Roberson el 31 de Ag. de 2020
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.

Iniciar sesión para comentar.

Idin Motedayen-Aval
Idin Motedayen-Aval el 3 de Jun. de 2024
Editada: Idin Motedayen-Aval el 3 de Jun. de 2024
This page is still getting a fair number of views, so I wanted to summarize the discussion.
  • As it has been pointed out, the problem in the original code is the line "I = rgb2gray(a);". This line converts the image to grayscale, so everything is grayscale from that point on. The color information is lost and cannot be recovered.
  • The right approach is to use indexed images as pointed out by @abdel rahman.
  • If you want to store/transmit the compressed image, as @Walter Roberson pointed out a few times, "you should also be storing the rgb table and the Huffman dictionary."
  • The OP's code is a mere academic exercise in Huffman coding an image, and then decoding it to recover the original image (since Huffman coding is lossless, the recovered image will be indentical to the original).
  • I cleaned up the code a bit to show how it might run in MATLAB (it can be cleaned up much more; I preserved much of the original code for traceability)
%clearing all variableas and screen
clear all;
close all;
clc;
number_of_colors = 256;
%Reading image
a=imread('peppers.png');
figure(1),imshow(a)
%converting an image to grayscale
% I=rgb2gray(a);
% Use indexed image instead of grayscale
[I, myCmap] = rgb2ind(a, number_of_colors);
%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.
pro = zeros(256,1);
for i=0:255
k=(I==i);
count=sum(k(:));
%pro array is having the probabilities
pro(cnt)=count/Totalcount;
% sigma=sigma+pro(cnt); <-- Not needed here
% cumpro(cnt)=sigma; <-- Not needed here
cnt=cnt+1;
end
% Probablities can also be found using histcounts
pro1 = histcounts(I,0:256,'Normalization','probability');
cumpro = cumsum(pro); % if the cumulative sum is needed
sigma = sum(pro); % if the sum is needed; should always be 1.0
%Symbols for an image
symbols = 0:255;
%Huffman code Dictionary
dict = huffmandict(symbols,pro);
%function which converts array to vector
newvec = reshape(I,[numel(I),1]);
%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
back = reshape(dhsig,[m n]);
%converting image from grayscale to rgb
% [deco, map] = gray2ind(back,256);
% RGB = ind2rgb(deco,map);
RGB = ind2rgb(back,myCmap);
imwrite(RGB,'decoded.JPG');
figure(2),imshow(RGB)
%end of the huffman coding

1 comentario

[I, myCmap] = rgb2ind(a, number_of_colors);
Note that with this approach of converting to indexed image, the restored image will not generally be identical to the original image. To have the restored image be identical to the original image, reshape the image from being m x n x 3 to being m x (n*3) and then encode that effectively-grayscale result.

Iniciar sesión para comentar.

Categorías

Más información sobre Denoising and Compression en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Abr. de 2019

Comentada:

el 10 de Ag. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by