Jump to content

Image Compression


Slithe

Recommended Posts

For a school project, I need to think up some (hopefully easy) ways to compress (and decompress) images. I have looked at JPEG's algorithm, but it seems like much too much work to implement. This is just an assignment, so I was trying to think of algorithms that would be easier to implement in Matlab.

 

Lately, I have been trying to compress a grayscale image by transforming it into frequency space with a Fourier Transform. Then I would examine each value of the transformed matrix and, if the value was small enough, I would just set it zero. I would then use run-length-encoding to pack all the zeroes. However, when I decoded (i.e. unpacked and transformed back to pixel space) the image, pretty much all the values were zero (i.e. the image was all black); however, most values in frequency space were not zero. What am I doing wrong?

 

Here is my Matlab code. It takes as input a 3-d matrix that stores red green and blue values.

function out = FFTCOMPRESS( in )
x = length(in(1,)/3
y = length(in(:,1))
fr = fft2(in(:,:,1));
fg = fft2(in(:,:,2));
fb = fft2(in(:,:,3));
myout = zeros(1, 5+4*y*x*3);
myout(1) = x;
myout(2) = y;
myout(3) = 6;
for ii=1:y,
   for jj=1:x,
       if fr(ii,jj)<10
           fr(ii,jj)=0;
       end
       if fg(ii,jj)<10
           fg(ii,jj)=0;
       end
       if fb(ii,jj)<10
           fb(ii,jj)=0;
       end
   end
end
n = 6
nz = 0
for jj=1:x,
   for ii=1:y,
       if nz ~= 0,
           if fr(ii,jj)==0
               nz=nz+1;
           else
               myout(n)=0;
               myout(n+1)=nz;
               myout(n+2)=fr(ii,jj);
               nz=0;
               n=n+3;
           end
       else
           if fr(ii,jj)==0
               nz=nz+1;
           else
               myout(n)=fr(ii,jj);
               n=n+1;
           end
       end
   end
end
nz=0;
myout(4)=n;
%Do the same think for fg & fb - I ommited this part.
out=myout(1:(n-1));

 

Also, does anyone have any other good ideas about how I could easily compress images? Furthermore, does anyone know of a good way to compare the quality of a compressed image with its original?

Link to comment
Share on other sites

Cool. I hope you get some replies on this -- it's interesting.

 

Regarding your last question, do you mean mathematically comparing the compressed image with the original? I'm no expert, but I believe the "standard" technique is to "align" the images with a predetermined corresponding point (e.g. the first pixel in the upper left corner), and then measure the difference between the two pixels in each corresponding location (easy, since they're presumably the same size and number of pixels). You can actually store those values in a third file, called a "difference image", which serves as a kind of map of what your changes did (potentially useful for things like device calibration).

 

You might check into wavelet transforms, which supposedly offer advantages over Fourier transforms. There are good articles on Discrete wavelet transform and Complex wavelet transform in the Wikipedia. These approaches are I believe used in rapid image recognition software, such as automated car-driving programs and facial recognition.

 

If you have access to the IEEE or ACM portals there are a bunch of papers in there -- I believe both institutions also have publications dedicated to image processing. Some more search terms you can try: image differencing, Hutchison metric, and watermark removal.

 

There are also some similarities between this and the process of Normal Mapping, which is used in 3d modeling, which may or may not be useful to you.

 

Good luck! :)

Link to comment
Share on other sites

  • 4 years later...

Of course there is a way to compare images, otherwise how would you gauge different compression algorithms. http://www.rimtengg.com/iscet/proceedings/pdfs/image%20proc/101.pdf

 

As far as fairly simple to implement compression techniques:

Easy technique 1: (255,255,255,255)(red,green,blue,opacity) takes 32 bits per pixel to store, drop opacity, now you are down to 24bits, now cut the palette in 1/2, thats 21 bits.

Not quite as easy technique 2: take a note of all the values actually used in the image, cut out colors that are close to each other (say within 1 or 2 or 3 (quality comes in affect here) values (128,129,130,131,132) and assign them the median value, pay attention to some hues more than others as our eyes recognize some tones better than others (we're horrible at green by the way). Now create a map of colors and store them in a table (or just array), now readdress every pixel with respect to its corresponding number in the table. (64 colors (without opacity, though you could implement limited levels of opacity) is addressable in 6 bits/pixel plus table size) Also make sure you note the index size so that you can decode your image correctly...

Link to comment
Share on other sites

  • 1 month later...

Also, does anyone have any other good ideas about how I could easily compress images?

The first and the most important question you should ask yourself- do you want compression without losing data (GIF,BMP,PNG,TGA,PSD etc), or compression with lost (JPEG,MPEG).

 

The first group, lossless compression methods, are utilizing any binary compression method you can develop or find on Internet.

If it'll be working with txt, exe, or whatever other file format, it might be tried to compress raw image data, to see how good it is with such kind of data.

 

To improve compression ratio, you can simply add some pre-procession like grouping r,g,b so they're continuous in memory (instead of r,g,b,r,g,b, etc. there is r,r,r ... g,g,g, ... b,b,b...)

Or subtracting them from left pixels of image.

If r[x] is same as r[x+1] then after subtraction you have 0, regardless of gb components that might vary.

 

Compression method might be bad for rgb pixels that are in chunk format, and might be much better after rearranging.

 

You can change RGB to HSV (or other) format.

Then lost some precision of one or multiple components,

and compress them using regular binary compression method.

 

f.e. if image is gray scale, HSV will have the all HS=0, and the only component that will be varying will be V.. That's immediately 33% of original raw data even without compressing it.

 

Furthermore, does anyone know of a good way to compare the quality of a compressed image with its original?

 

Subtract RGB of original image from compressed and decompressed RGB image, then calculate absolute, or power of them, add together and sqrt().

 

unsigned char rgb[ 3 ];

unsigned char rgb2[ 3 ];

 

int dr = rgb[ 0 ] - rgb2[ 0 ];

int dg = rgb[ 1 ] - rgb2[ 1 ];

int db = rgb[ 2 ] - rgb2[ 2 ];

int d = sqrt( dr*dr + dg*dg + db*db );

 

For lossless image compression method d will be always 0, because what is compressed and decompressed is the same at binary level.

The smaller d, the less data has been lost.

Instead of power ^2 you can use other more powerful values

 

dr = abs(dr);

dr*dr*dr

 

so if r=0 and r1=10

10-0 = 10

10*10*10=1000

 

Pixels with larger change will have higher weight than those with smaller.

Edited by Sensei
Link to comment
Share on other sites

  • 2 months later...

there are many compressing method you can use to get the value you are asking for. i have found this compress and decompress tutorial might be what you are looking for. among all the compressing technology wavelet compressing and many others, jpeg compressing is what i am using mostly.

HI there

Thanks for your nice sharing.It is really important for me.I am looking for a image tool which supports to compress image direcly.I want to know that if there is a free trial for new users in this program.Thanks a lot

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.