Saturday, November 23, 2024

Lossless Image Compression Using MATLAB

MATLAB is a powerful tool for analyzing images and signals for developing applications. One of the applications of image compression with MATLAB using a graphical user interface is described in this article. Here we talk about lossless image compression using MATLAB.

fig 1
Fig. 1: Flowchart showing the compression process

Cameras are nowadays being provided with more and more megapixels to improve the quality of captured images. With improvement in image quality, size of the image file also increases.

Due to speed limitation of the Internet, it takes more time to upload good-quality images that are of bigger sizes. A user needs to compress the image without degrading its quality. Mobile manufacturers need algorithms in their cameras that enable storing the images in reduced sizes without degrading their quality.

- Advertisement -

There are two types of compression algorithms, namely, loss-less and lossy-image compression. This article proposes a technique to compress the captured image to reduce its size while maintaining its quality. A number of images were considered to check the veracity of the proposed algorithm.

fig 2
Fig. 2: Original image sample 1

In this article, discrete cosine transform algorithm is used, which compresses the image with a good compression ratio.

- Advertisement -

The flowchart of the process is shown in Fig. 1.

The image is read through MATLAB to capture its pixels. After obtaining the compressed image, peak-signal-noise ratio (PSNR) and mean-square error (MSE) are calculated using the following relationships:

relationship--1

where m and n are the number of rows and columns. Image1 and Image2 are the original and compressed images, respectively.

Lossless Image Compression Using MATLAB
Fig. 3: Lossless Image Compression Using MATLAB of sample 1
fig 4
Fig 4: Original image sample 2

After compression, there should not be much change in the quality of the image. MSE indicates an error between the original image and compressed image. It should be as small as possible.

where R is the maximum fluctuation in the input image data type (maximum possible pixel value of image). PSNR is related to MSE and it gives the amount of noise in a compressed image. PSNR should be as high as possible.

relationship--2

All equations are implemented in MATLAB in the form of functions.

fig 5
Fig. 5: MATLAB implementation of image sample 2

Some images and their associated MATLAB graphical interfaces are shown in Figs 2 to 5.

Download source code: click here


Lalit G. Patil is a lecturer in Department of Electrical Engineering, M.S. University of Baroda, Gujarat. His areas of research include signal processing, image processing and control systems

86 COMMENTS

    • Decompression is not possible once it is compressed.

      For video compression, you can convert video into frames.
      Then compress each frame.
      Then merge all frames into video again.

      Check video size. It will be compressed.!!

    • Open ImageCompression1.m Matlab code. Run the program by clicking ‘Run’ button. A screen opens up. Click on ‘Select the image’ option on the left side. You need to load any .jpg format image file from your PC. Once image is loaded, you can see the image on the screen. Now, click on ‘Compress image’ option on the right side. Wait for some time until the compressed image pops up on the screen.

  1. i am getting the following errors:
    >> ImageCompression1
    Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

    Error in ImageCompression1>pushbutton1_Callback (line 89)
    SIZE = fileinfo.bytes;

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in ImageCompression1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ImageCompression1(‘pushbutton1_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback
    Can someone help?
    ASAP

  2. Warning: MATLAB has disabled some advanced graphics rendering features by switching to software OpenGL. For more information, click here.
    Undefined function ‘dctmtx’ for input arguments of type ‘double’.

    Error in ImageCompression1>pushbutton2_Callback (line 110)
    T = dctmtx(8);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in ImageCompression1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ImageCompression1(‘pushbutton2_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback
    what to do about this errors??

  3. This is the reply from the author Lalit Patil.
    “This is the code written in MATLAB 2013 version.! As the years passes, functionality is changed in newer versions. You might be using some higher version. Try in MATLAB 2013.”

  4. Sir, I am having trouble in displaying video on the axes.
    Because the axes is not the size of video, the video resizes and become pixelated.

    How can I have an axes resizing based on the input video?

    here is my code.

    handles.output = hObject;
    axes(handles.axes1);
    vid = videoinput(‘winvideo’, 2);
    himage = image(zeros(160,120,3),’parent’,handles.axes1);
    preview(vid,himage);
    guidata(hObject,handles);

  5. Once it is compressed, You can not decompress. Because you have modified pixels in mathematical form. Compress and Decompress means only reduction in Size of image without reducing Quality. So no need to Decompress.!!

  6. i have used the code for Lossless Image Compression Using MATLAB which is present in your site
    its compressing the image file ,
    but its not displaying
    images of uncompressed and compressed images in figure window.
    its displaying only size of uncompressed image

    pls help me

    i love your website
    thanks

  7. sir i have used the code for Lossless Image Compression Using MATLAB which is present in your site
    its compressing the image file ,
    but its not displaying
    images of uncompressed and compressed images in figure window.
    its displaying only size of uncompressed image

  8. Sir, I run the code for image compression but it is showing the images in figure window. It is showing the size of compressed image and original image but not the images ?
    Why ? Kindly tell me !!!!

SHARE YOUR THOUGHTS & COMMENTS

EFY Prime

Unique DIY Projects

Electronics News

Truly Innovative Electronics

Latest DIY Videos

Electronics Components

Electronics Jobs

Calculators For Electronics

×