Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of image compression in unity3d

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail what is the principle of image compression in unity3d. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1 the reason why the image can be compressed

An original image (1920x1080), if each pixel is represented by 32bit (RGBA), then the amount of memory the image needs

1920x1080x4 = 8294400 Byte, about 8m. This is absolutely unacceptable to us. If this is the case, the 1G hard drive only stores more than 100 pictures, so it can't afford to hurt. The video is the same, if the video is 1920x1080 30fps, 1 hour. That doesn't compress the memory you probably need:

8Mx30x60*60 = 864000m, more than 800g! That's crazy!

So, we need image compression.

Then why can the image be compressed? Because it has a lot of redundant information.

Common types of redundancy in image, video, and audio data are as follows:

1. Spatial redundancy

two。 Time redundancy

3. Visual redundancy

The following is described in more detail.

1.1 Spatial redundancy

There is often spatial coherence between the colors of the sampling points on the surface of an image, such as the following picture, the color of two mice, the back wall, the gray floor, all the same color. These blocks of the same color can be compressed.

For example, the first row of pixels is basically the same, assuming that the luminance value Y is stored in this way.

[105 105 105. .105], if the total is 100 pixels, then 1Byte*100 is required.

The simplest compression: [105100], which means that the brightness of the next 100 pixels is 105, so it only takes 2 bytes to represent the whole row of data! Isn't it compressed!

Spatial redundancy mainly occurs in a single picture, such as our photo.

1.2 time redundancy

This redundancy is mainly for video.

A moving image (video) is generally a group of continuous pictures located in a timeline, in which the adjacent frames often contain the same background and moving objects, but the spatial position of the moving objects is slightly different. therefore, the data of the latter frame has a lot in common with the data of the previous frame, which is called time redundancy because the adjacent frames record the same scene at the adjacent time.

As shown in the following picture, there are actually 30 frames per second, and there is a 33ms between each frame. It is so short that there are few changes before and after the frame, perhaps only the mouth moves and the background does not move.

1.3 Visual redundancy

Due to the limitation of physiological characteristics, human visual system pays non-uniform attention to the image field, and people do not feel obvious about subtle color differences.

For example, the general resolution of human vision is 26 grayscale, while the general quantization of images is 28 grayscale, that is, there is visual redundancy.

Human hearing is not very sensitive to some signals, so that people can not feel the change in the original allowable range after compression.

2 Classification of data compression methods 2.1 classify according to whether the compression method produces distortion 2.1.1 lossless compression

Lossless compression requires that the decompressed data is completely consistent with the original data. The data obtained after decompression is a copy of the original data, which is a kind of reversible compression.

The lossless compression method removes or reduces the redundancy in the data, and then reinserts it into the data during recovery, so it is a reversible process.

According to the current technical level, the lossless compression algorithm can generally compress the data of ordinary files to the original 1max 2mi 1max 4. Some commonly used lossless compression algorithms include Huffman algorithm and LZW (Lenpel-Ziv & Welch) compression algorithm.

2.1.2 distorted compression

The decompressed data is not completely consistent with the original data, which is an irreversible compression method. After compression and restoration with distortion, it does not affect the expression of information.

For example, the lossy compression method can be used in the compression of image, video and audio data, because it often contains more data than our visual and auditory systems can receive. Lose some data without misunderstanding the meaning of sound or image, but the compression ratio can be greatly improved. The compression ratio of image, video and audio data can be as high as 100u1, but people's subjective feelings still do not misunderstand the original information.

2.2 classify 2.2.1 predictive coding according to the principle of compression method

The basic idea is to use the data value of the encoded point to predict the data value of an adjacent pixel.

2.2.2 transform coding

The basic idea is to transform the light intensity matrix of the image into the coefficient space, and then encode and compress the coefficients.

2.2.3 Statistical coding

Compression coding according to the distribution characteristics of the occurrence probability of information. Like Huffman coding.

3 elements of image compression

Compression ratio

The higher the ratio of file size before and after compression, the better, but it is affected by speed, resource consumption and so on.

Image quality

Compared with the original image, the evaluation methods include objective evaluation and subjective evaluation.

Compression and decompression speed

It is related to the compression method and the compression coding algorithm, generally, the amount of computation of compression is larger than that of decompression, so compression is slower than decompression.

4. Image compression coding example 4.1 run-length coding (RLE)

This is the best code to understand.

There are many such images in reality, and there are many blocks of the same color in one image. In these blocks, many lines have the same color, or many consecutive pixels on a row have the same color value. In this case, it is not necessary to store the color value of each pixel, but only the color value of one pixel and the number of pixels with the same color, or the color value of the pixel and the number of rows with the same color value.

This compression coding is called stroke coding (run length encoding,RLE), and the number of pixels that have the same color and are continuous is called stroke length.

For example, the string AAABCDDDDDDDDBBBBB

Using RLE principle, it can be compressed to 3ABC8D5B.

RLE coding is simple and intuitive, and the encoding / decoding speed is fast.

Therefore, this method is used to compress many graphics and video files, such as .BMP. Tiff and AVI.

Since there are many blocks of the same color in an image, an integer pair is used to store the color value of a pixel and the number (length) of pixels of the same color. For example:

(G, L) / / G is the color value, L is the length value

The coding is arranged from left to right and from top to bottom, and whenever a string of the same data is encountered, the data and the number of repeats are used to replace the original data string.

For example, the following 18'7 pixels (assuming only grayscale values, 1 byte)

000000003333333333

222222222226666666

111111111111111111

111111555555555555

888888888888888888

555555555555553333

222222222222222222

Only 11 pairs of data are needed.

(0pr 8) (3pr 10) (2pr 11) (6pr 7)

(1) 18) (1) (6) (5) (12) (8)

(5, 14) (3, 4) (2, 18)

Run length coding features:

Intuitive, economical

It's a lossless compression.

The compression ratio depends on the characteristics of the image itself. The larger the image block of the same color is, the less the number of image blocks is, the higher the compression ratio is.

Suitable for computer-generated images, for example. BMP, TIF, etc., are not suitable for natural images with rich colors.

This is not to say that RLE coding method is not suitable for natural image compression. On the contrary, RLE is indispensable in natural image compression, but it can not only use RLE coding method, but needs to be applied in conjunction with other compression coding techniques.

4.2 Huffman coding (Huffman)

Because the probability of the occurrence of the color data in the image is different, the codes with shorter word length are assigned to the codes with high occurrence frequency, and the codes with longer word length are coded with less frequency, so as to reduce the total amount of code, but not the total amount of information.

Coding steps:

(1) initialization, sorting symbols in order from largest to smallest according to the probability of symbols.

(2) the two symbols with the least probability are formed into a node, such as D and E in figure 4-02 to form node P1.

(3) repeat step 2 to get nodes P2, P3 and P4 to form a "tree", in which P4 is called root node.

(4) from the root node P4 to the "leaves" corresponding to each symbol, from top to bottom marked with "0" (upper branch) or "1" (lower branch), it does not matter which is "1" and which is "0". The end result is only that the assigned code is different, and the average length of the code is the same.

(5) write the code of each symbol from the root node P4 along the branch to each leaf.

4.3 basic concepts of DCT coding 4.3.1

The image described in the spatial domain will be described in a certain transform domain after a certain transformation (usually using cosine transform, Fourier transform, Walsh transform, etc.).

In the transform domain, firstly, the correlation of the image is reduced, and then the coding bit rate of the image can be further compressed by some kind of image processing (such as two-dimensional filtering in frequency domain) and entropy coding.

This transformation is often used in JPEG image compression.

4.3.2 transform compression principle block diagram

G: input source image

G': decoded image

U: two-dimensional orthogonal transform

U': two-dimensional orthogonal inverse transform

In addition to these common compression algorithms, there are many other algorithms, here will not be introduced one by one, anyway, you have a perceptual understanding.

On "what is the principle of image compression in unity3d" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report