What is a JPEG

JPEG stands for Joint Photographic Experts Group, a Group of Experts in image processing that develops an Image compression standard (ISO).

So, JPEG is not really an image format, but an image compression standard. It contains a set of parameters and rules for color space. It is not as widely used, however, and the more common standard is JFIF. When you talk about JPEG compression you mean time. Note that the image file suffixes are.jpeg and.jpg, but the underlying algorithm is also JFIF.

Underlying assumptions of the JPEG algorithm

JPEG algorithm is designed for the human eye, it uses the following biological characteristics of the human eye:

  1. I’m more sensitive to the brightness of colors, less sensitive to colors;
  2. We’re not as sensitive to high frequency areas of the image;

The algorithm can be broken down into several steps: For the input (an image), the following is done:

  1. Color transformation;
  2. DCT for every 8 by 8 block;
  3. Quantization stage;
  4. Huffman coding;

Finally, a compressed image is created in a.jpg file. In this file, there is not only information about unzipping, but also information about reexpanding the image.

An uncompressed image

MATLAB has all kinds of pictures, and here’s a bunch of chili peppers.

I = imread('peppers.png');
imshow(I);
Copy the code

It currently requires approximately 4,718,592 bytes of space to store:

ImageSize = 8*prod(size(i))
Copy the code

ImageSize = 4718592;

Color transform: Convert RGB to YCbCr

The graphics card displays a color by setting RED, BLUE and GREEN (also known as RGB) values. These three colors form the three axes of the Cartesian coordinate system, and each color is a point in the color’s “three-dimensional” space. When the RED, GREEN and BLUE values of a point are the same, a gray picture will be displayed. In JPEG’s compression algorithm, we use another color space, YCrCb, which also has three dimensions: brightness, denoted by Y; Blue is U; Red is represented by V. This three-dimensional value can be converted from an RGB system using the following formula:

Y = 0.299r + 0.587g + 0.114b U = -0.1687r-0.3313g + 0.5b + 128V = 0.5r-0.4187g-0.0813b + 128Copy the code

To reduce the sampling

Now that we have the YCrCb representation for each pixel, let’s do the first step of compression: de-sampling. First, cut the picture into 8*8 pieces. When sampling, the brightness value of each pixel is retained, while the UV value is retained in every four pieces, which is roughly as follows:For example, if there are 8 pixels in a row, and each of the 3 dimensions of each pixel is represented by 3 bytes, that used to be

8*3=24 bytes for storageCopy the code

After lowering the sampling, you just need to

8+2+2=12 bytes to storeCopy the code

The amount of space you need is cut in half.

Next step: DCT