File Exchange

image thumbnail


version (60 KB) by Ahmed Ayman
LZW image encoder/decoder


Updated 13 May 2017

GitHub view license on GitHub

This is a LZW binary (Black and White) image compression project using matlab
for an example, please run the LZW_test

Cite As

Ahmed Ayman (2020). ahmedmayman/LZW_image_codec (, GitHub. Retrieved .

Comments and Ratings (24)

where is the code

Ahmed Ayman

LZW algorithm is indeed a lossless algorithm, but my implementation is for binary (or binarized) images.
So, it's an expected result, but I unfortunately didn't clarify in the description.

Sorry for the inconvenience, I edited the description.
I think I'll start working on an RGB version soon, if you need it watch for updates.


I have a question. I used my own photo, and after encoding it and decoding it, my resulting picture (Decoded) was not the same as the original one. More precisely, it was a black-white copy of original. You can see comparison here:

Seems like this Decoded image is binarized original picture. Is this expected result? Is there something that can be done in order to Decoded image be exactly like original image? I am asking this because, as I know, LZW algorithm is lossless algorithm. Thanks in advance!


Ahmed Ayman

Please reconsider your rating

Ahmed Ayman

Please download the latest version
I added the final dictionary as output in LZW_img_enc() and LZW_img_dec()
example :
[Encoded,final_dictionary] = LZW_img_enc(Binary');
[Decoded,final_dictionary] = LZW_img_dec(Encoded);
you will notice that I omitted vec2mat() in the decoding line, because you only need it to transform the decoded vector into matrix form to display the image. So you might add
Decoded = vec2mat(Decoded,numel(Binary(1,:)));

As for the 2nd Question, After the LZW_test.m finish, you will find the Encoded vector saved in your workspace
plus, you can use the functions LZW_img_enc() and LZW_img_dec() in a separate code of your own and manipulate their output as you want
the LZW_test.m is just a demo, you can edit it, use the codec functions in command line, or implement them in your own code

Final qustion please if i want to display the dictionary after finishing the encoding and decoding proccess what should i do ? and what to do if i want to display also a text file or vector than contains the endoced message after the encode proccess only what should i do?
thanks for advance and sorry for too many qusions

Ahmed Ayman

Please note that the output from
Binary = Input > graythresh(Input);
is not as efficient as the output using imbinarize(), so you will most probably find that the final decoded image is not identical to your input image
check the input for the LZW_img_enc() using the command
you'll see an identical image to the finally decoded one
so the difference between input and decoded will be due to the method you used to binarize your image and not the codec functions

I hope I helped
feel free to revisit your previous rating

Ahmed Ayman

the output of the LZW_test is your input image vs. decoded image after (binarization, encoding, and decoding)
it may take long depending on the size of your image

I think your issues and comments would be more useful for everyone
but if you wish you can raise issues on the github repo too

ok i fixed the error and now its running but its taking too long what should be the output i expect? two images right decodedand encoded one? please man if you can give me ur mail or anything to contact you on would be gratefull

Ahmed Ayman

graythresh() outputs a global threshold for imbinarize() to use as input
meaning, if you used graythresh() you still need imbinarize() to binarize your image

Attempted to access enc_img(1); index out of bounds because numel(enc_img)=0. this is what i got after using another function rather than imbnraize

Ahmed Ayman

in this question there's an alternative for imbinarize
I didn't try it but check it out

i fixed the error by using graythresh() instead of imbinrization but this error appears ??? Attempted to access enc_img(1); index out of bounds because numel(enc_img)=0.

Error in ==> LZW_img_dec at 32
current_code = bi2de(enc_img(1));

Error in ==> LZW_test at 23
Decoded = logical(vec2mat(LZW_img_dec(Encoded),numel(Binary(1,:))));

ahhh i have the image processing toolbox installed but i am using R2010 can u please tell me a solution without having to download the new version ?!

Ahmed Ayman

is this your issue ?
"Undefined function 'imbinarize' for input arguments of type 'uint8'."
if it is I hope this answer help you
note :
1- imbinarize is a new function introduced in R2016a -which I defined as the matlab release I used in development-
2- you should have Image Processing toolbox installed
for that and other alternative solutions check the answer above

for any further issues feel free to ask

please ya3m ayman zmilek bydi3 :D:D

the code doesnt work when i run the lzw_testit gives me error in the binraztaion it cant accept unit8 please if u can provide me ur number i need ur help so much

Ahmed Ayman

please be specific about the issues

please remove the code


added "binary (Black and White)" to the description

added final dictionary output to the functions LZW_img_enc() and LZW_img_dec()

updated Description and Required Products

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux