1.3 Compression 压缩
压缩(Compression)是指减少数据文件大小的过程与技术。
为什么需要压缩?
压缩技术的主要目的是减小数据文件的大小,从而 提高传输效率、节省存储空间 和 降低带宽需求。压缩也有助于 提高处理速度,特别是在 网络通信 和 大数据存储 中尤为重要。
Note
Compression:
reduce file size
with / without losing information
cannot / can be reconstructed
使用压缩技术的原因:
reduce memory storage requirements
improve transmission rates
常见的应用场景包括:
图像压缩:减少图像文件大小,使得图片能够快速加载和传输。
音频压缩:减少音频文件大小,便于存储和流式传输。
视频压缩:压缩视频文件,减少占用带宽,提高流媒体质量。
文件存储:通过压缩存档文件,节省存储空间。
压缩的类型:有损和无损
压缩方法通常分为两种:有损压缩(Lossy Compression) 和 无损压缩(Lossless Compression)。
有损压缩(Lossy Compression)
有损压缩 会丢失一些数据,从而降低文件的质量,但能够大幅度减小文件大小。它常用于对质量要求不高的文件,如图像、音频和视频。
例如:JPEG 图像、MP3 音频、MPEG 视频。
Note
Lossy Compression: coding techniques that cause some information to be lost so that the exact
original file cannot be recovered in subsequent decoding.
使用有损压缩的优点:
Provides significant storage savings, which is crucial for devices with limited storage capacity.
Essential for efficient online transmission and streaming of multimedia.
使用有损压缩的缺点:
There can be a noticeable reduction in quality, particularly at higher compression ratios. (如果举例)This might manifest as blurred images, muffled audio, or pixelated video.
Introduces artefacts, which are distortions in the compressed file, (如果举例)like blockiness in images or warbling in audio.
进行有损压缩的常见方法:
位图:
reduce color depth / bit depth;
reduce resolution.
音频:
reduce sampling resolution
reduce sampling rate
Perceptual Music Shaping(详见下文)
无损压缩(Lossless Compression)
无损压缩 不会丢失任何数据,压缩后的文件可以完全恢复到原始状态。它常用于对数据完整性要求较高的文件,如文本文件、程序文件和一些图像。
例如:PNG 图像、ZIP 文件。
优点:不会丢失数据,压缩后可恢复到原始质量。
缺点:压缩比低,文件尺寸减小幅度有限。
Note
Lossless Compression: Coding techniques that allow subsequent decoding to recreate exactly the original file.
使用无损压缩的优点:
Preserves the exact original data, crucial for legal, scientific, and archival purposes.
Ensures that the quality of the data remains untouched, which is vital for certain types of data like medical images.
使用无损压缩的缺点:
The extent of reduction is generally less pronounced compared to lossy compression.
如何压缩不同类型的文件
不同类型的文件使用不同的压缩方法。以下是几种常见文件类型的压缩方法:
1. 文本文件的压缩
文本文件中的信息是由字符组成的。常见的文本文件压缩方法是 Huffman Coding(霍夫曼编码) 算法。对于文本文件,无损压缩 通常是首选,因为任何数据丢失都可能导致信息的丢失。
不要求掌握 Huffman Coding 的具体内容,但要记住它是用于压缩文本文件的,而且是一种无损压缩算法。
2. 位图图像的压缩
位图图像(如 JPEG 文件)可以使用 有损压缩(如 JPEG)来减小文件大小,方法包括删除图像中肉眼无法察觉的细节。对于重要的图像内容(如图形设计),可能使用 无损压缩(如 PNG)。
Note
Bitmap Compression: Bitmap images are compressed by removing unnecessary data or reducing color depth. Lossy methods like JPEG remove data, while lossless methods like PNG preserve the original data.
3. 矢量图像的压缩
矢量图像(如 SVG 文件)通常使用 无损压缩,因为它们由数学公式描述,信息损失不利于精确呈现。
Note
Vector graphics can undergo file compression. Scalable vector graphics (.svg) are defined in text files which allows them to be compressed.
4. 声音文件的压缩
声音文件(如 AAC 或 WAV 文件)可以使用 有损压缩(如 MP3)来减小文件大小,通常通过去除频率较低的音频信息来实现。无损音频格式如 FLAC 保证高音质但文件较大。
Note
压缩声音文件通常使用 知觉声音成型(Perceptual Music Shaping)技术。
PMS 技术会移除特定的声音来减小文件尺寸,例:
frequencies that are outside the human hearing range
if two sounds are plaved at the same time, only the louder one can be heardby the ear, so the softer sound is eliminated.
有损压缩后的 MP3 文件大小仍有不同,这取决于其自身的 比特率(Bit Rate),比特率越大,文件大小越大。
运行长度编码(RLE)
运行长度编码(Run-Length Encoding, RLE) 是一种常见的 无损 压缩方法,尤其适用于包含大量连续相同数据的文件。该方法通过记录数据的重复次数来减少文件的大小。
例如,文本中连续的字母“A”可以表示为“A5”,表示连续出现五次“A”。对于图像或其他数据类型,RLE 也可以应用于相同的数据压缩。
当文本中的连续字母多的时候,RLE 算法很高效;但当出现连续的不同字母时,RLE 算法的压缩率就很有限了(如:字符串 bcbcbcbc
基本无法被 RLE 压缩)。
Note
Run-Length Encoding (RLE): The compression converts sequences of the same bit pattern into a code that defines the bit pattern and the number of times it is repeated.
压缩算法选择的考量因素
在选择压缩算法时,考虑以下因素:
压缩率:算法能够将文件压缩到什么程度。
处理速度:压缩和解压缩的速度。
文件类型:不同的文件类型(文本、图像、音频、视频)适合不同的压缩算法。
数据丢失:有些应用需要完美的数据恢复,有些则能容忍一些数据丢失。
例 18:
Photographs are compressed before they are uploaded to a web server. Customers download photographs from the server.
(a) Explain reasons why compressing photographs will benefit customers.
(b) An image can be compressed using RLE. Explain reasons why RLE may not reducethe file size of a bitmap image. Give one example in your answer.
解答:
(a) Customers will be able to download photographs in less time and they will takeless of bandwidth. Photographs will take up less space on customer's storage medium therefore customers can store more images and will have more space for other files.
(b) RLE stores a colour and number of times it occurs consecutively. An image may not have many sequences of the same colour. lt would need to store each colour and then the count/number 1 which adds data. Example: Red-Green-Blue would become Red 1 Green 1 Blue 1.
练习 8
Explain the difference between lossy and lossless compression and give examples where each method would be appropriate.
Discuss how RLE works and provide an example.
Compare the advantages and disadvantages of using JPEG versus PNG for image compression.
章末测试
A file contains binary coding. The following are two successive bytes in the file:
10010101 00110011
a One possibility for the information stored is that the two bytes together represent one unsigned integer binary number.
i Give the denary number corresponding to this. Show your working.
ii Give the hexadecimal number corresponding to this. Show your working.
b Give one example of when a hexadecimal representation is used.
c Another possibility for the information stored is that the two bytes individually represent two signed integer binary numbers in two's complement form.
i State which byte represents a negative number and explain the reason for your choice.
ii Give the denary number corresponding to each byte. Show your working.**
d Give two advantages from representing signed integers in two’s complement form rather than using a sign and magnitude representation.
e Give three different examples of other options for the types of information that could be represented by two bytes. For each example, state whether a representation requires two bytes each time, just one byte or only part of a byte each time.
A designer wishes to include some multimedia components on a web page.
a If the designer has some images stored in files there are two possible formats for the files.
i Describe the approach used if a graphic is stored in a vector graphic file.
ii Describe the approach used if a graphic is stored in a bitmap file.
iii State which format gives better image quality if the image has to be magnified and explain why.
b The designer is concerned about the size of some bitmap files.
i If the resolution is to be 640 × 480 and the colour depth is to be 16, calculate an approximate size for the bitmap file. Show your working and express the size using sensible units.
ii Explain why this calculation only gives an approximate file size.
c The designer decides that the bitmap files need compressing.
i Explain how a simple form of lossless compression could be used.
ii Explain one possible approach to lossy compression that could be used.
An audio encoder is to be used to create a recording of a song. The encoder has two components.
a One of the components is an analogue-to-digital converter (ADC).
i Explain why this is needed.
ii Two important factors associated with the use of an ADC are the sampling rate and the sampling resolution. Explain the two terms. Use a diagram if this will help your explanation.
b The other component of an audio encoder has to be used before the ADC is used.
i Identify this component.
ii Explain why it is used.
c The recorded song is to be incorporated into a video. Sound-editing software is to be used as part of this process. Describe two techniques that the sound-editing software could provide.
最后编辑:admin 更新时间:2025-03-17 12:13