0% found this document useful (0 votes)
14 views6 pages

21BCE9902 Assignment-7 DIP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

21BCE9902 Assignment-7 DIP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Digital Image

Processing
Assignment-9
Name: K.Jayanth Nayak
Reg.No: 21BCE9616
Slot: L33+L34
Lab: 9

Image Compression using Run Length Encoding


and Decoding
Aim:

The aim of this exercise is to understand and implement the basic


concept of image compression using the Run Length Encoding (RLE)
algorithm. RLE is a simple and efficient lossless compression technique
that can be used to reduce the file size of an image without losing any
information.

Theory:

Run Length Encoding (RLE) is a data compression algorithm that


encodes a sequence of repeated data values (called runs) as a single
value and a count of how many times that value occurs consecutively.
This is particularly useful for compressing images that contain large
areas of the same color or grayscale value.

The RLE algorithm works as follows:

Encoding (Compression):
Scan the image pixel by pixel.
Whenever a run of the same pixel value is encountered, encode it as a
pair of values: the pixel value and the count of the consecutive
occurrences of that value.
The resulting compressed data is a sequence of these value-count pairs.
Decoding (Decompression):
Read the compressed data, which is a sequence of value-count pairs.
For each pair, repeat the pixel value the number of times specified by
the count.
The resulting decompressed data is the original image.
The main advantages of RLE are its simplicity, low computational cost,
and effectiveness in compressing images with large areas of the same
color or grayscale value. However, RLE may not be as effective for
images with a lot of variation in pixel values, as the compression ratio
may be lower in such cases.
Code:
Output:
Result:

In the provided example, we had an input string str = [5 5 5 5 5 5 4 4


4 3 3 2] with a length of 12 elements.

Encoding (Compression):

The RLE encoding process identified the following runs in the input
string:
Run 1: 6 occurrences of the value 5
Run 2: 3 occurrences of the value 4
Run 3: 2 occurrences of the value 3
Run 4: 1 occurrence of the value 2
The encoded values are: [5, 4, 3, 2]
The encoded counts are: [6, 3, 2, 1]
The total length of the encoded data (values + counts) is 8 elements.
Compression Ratio:

The actual length of the original input string is 12 elements.


The encoded length (compressed data) is 8 elements.
The compression ratio is calculated as: actual_length /
encoded_length = 12 / 8 = 1.5
This means that the compressed data is 1.5 times smaller than the
original data, or in other words, the compression ratio is 1.5.

Decoding (Decompression):
The decoding process takes the encoded values and counts and
reconstructs the original input string by repeating each encoded value
the specified number of times.
The decompressed string is identical to the original input string: [5 5 5
5 5 5 4 4 4 3 3 2].
Theoretical Analysis:

The efficiency of RLE compression depends on the distribution of the


input data. RLE works best for data with long runs of the same value,
as it can effectively compress these runs.
In the provided example, the input string had several long runs of the
same value, which allowed RLE to achieve a good compression ratio
of 1.5.
However, if the input data had more variation, with fewer long runs,
the compression ratio would be lower, and RLE may not be as
effective.
RLE is a lossless compression algorithm, meaning that the
decompressed data is exactly the same as the original data. This is a
key advantage of RLE over lossy compression techniques.
In summary, the theoretical results of the RLE algorithm in the
provided example show that it was able to achieve a compression ratio
of 1.5, which is a good result for this type of data. The decompressed
data was also identical to the original input, as expected from a
lossless compression technique.

You might also like