10 DCT transformation for grayscale image compression (matlab program)

1. Brief description

1. Design tasks

1. In image transformation and compression, discrete cosine transform (DCT) is often used. The DCT transform is used for compression instances of images. Please verify your conclusions on test images.

2. Please program to realize the true color enhancement of the image.

3. Realize the grayscale transformation of the image through the method of histogram equalization, verify your conclusion in the test image, and analyze the program results.

4. Use commonly used filters to process digital images. Such as mean filter, median filter, Wiener filter, etc.

2. Principles of system design

1. Principle of DCT image compression

The DCT transform is the suboptimal orthogonal transform obtained under the minimum mean square error condition, and has been widely used, and has become the core of many international image coding standards. The compression algorithm of the JPEG image format adopts the DCT transform. The transformation kernel of the DCT transform is a cosine function, and the calculation speed is fast, which is beneficial to image compression and other processing. In the encoding process, the JPEG algorithm first converts the RGB components into brightness components and color difference components, and then decomposes the image into 8*8 pixel blocks, and performs two-dimensional discrete cosine transform on the 8*8 blocks, and each block generates 64 DCT coefficients are obtained, one of which is direct current (DC), which represents the average of all values ​​of the 8*8 input matrix, and the remaining 63 coefficients are alternating current (AC) coefficients. Next, the DCT coefficients are quantized, and finally the quantized The DCT coefficients are encoded to form a compressed image format. In the decoding process, the encoded quantized coefficients are first decoded, then the inverse quantization is performed and the DCT coefficients are converted into 8*8 sample blocks by using the two-dimensional DCT inverse transform, and finally the inversely transformed blocks are combined into an image . This completes the image compression and decompression process.

There are two methods to realize the discrete cosine transform DCT in MATLAB, one is a fast algorithm based on FFR, which is realized by the DCT2 function provided by the MATLAB toolbox; the other is that the DCT transform is a matrix method. The transformation matrix method is very suitable for DCT transformation of 8*8 or 16*16 image blocks, and the toolbox provides the dctmtx function to calculate the transformation matrix.

2. True color enhancement

True color enhancement is mainly for false color enhancement. Image color enhancement technology is mainly divided into two types: color enhancement and true color enhancement, and there are essential differences in principle between these two methods. When false color is enhanced, different colors are assigned to different gray value areas in the original gray image, so that people can distinguish different gray levels more clearly. Since the original image has no color in fact, this artificial color is called false color. Pseudo-color enhancement is essentially just an image coloring process, which is a grayscale-to-color mapping technique. True color enhancement is to adjust the color of the original image itself, which is a color-to-color mapping process.

3. Smooth

In the image, some abruptly changing points can be removed by mutual averaging of adjacent points, so as to filter out certain noises and achieve the purpose of smoothing, making the picture look softer and the color more uniform and clearer.

4. Sharpen

Image smoothing often blurs the boundaries and outlines in the image. In order to reduce the impact of such adverse effects, it is necessary to use image sharpening technology to make the edges of the image clear. The purpose of image sharpening is to make the edges, contours and details of the image clearer. The root cause of the smoothed image becoming blurred is that the image has been averaged or integrated, so it can be reversed. (such as differential operation) can make the image clearer. Considering from the frequency domain, the essence of image blur is that its high-frequency components are attenuated, so a high-pass filter can be used to make the image clear. In addition to denoising and contrast expansion in underwater image enhancement, sometimes it is necessary to enhance the edges and contours of the scene in the image. The edges and contours are often located in the place where the gray level changes suddenly in the image, so it is intuitive to think of extracting the edges and contours with the difference of the gray level.

5. Grayscale transformation (histogram equalization)

The basic idea of ​​histogram equalization is to transform the histogram of the original image into a form of uniform distribution, so as to increase the dynamic range of the pixel gray value and achieve the effect of enhancing the overall contrast of the image. Assuming that the grayscale of the original image at (x, y) is f, and the changed image is g, the method of image enhancement can be expressed as mapping the grayscale f at (x, y) to g. The mapping function to the image in the gray histogram equalization process can be defined as: g = EQ (f), this mapping function EQ (f) must meet two conditions (wherein L is the gray level number of the image):

(1) EQ(f) is a single-valued single-increasing function in the range of 0≤f≤L-1. This is to ensure that the enhancement process does not disturb the gray order of the original image, and the gray levels of the original image still maintain the arrangement from black to white (or from white to black) after transformation.

(2) For 0≤f≤L-1, 0≤g≤L-1, this condition ensures the consistency of the dynamic range of the gray value before and after transformation.

6. Image filtering

The filter is a frequency selection device that can pass specific frequency components in the signal, while greatly attenuating other frequency components, and can filter out interference noise. In digital image processing, it is often encountered that there are many noises mixed in the image. Therefore, in image processing, it is sometimes necessary to remove noise first. The most commonly used method of removing noise is to use a filter for filtering. Many filters are also designed in the image processing toolbox of MATLAB. Such as mean filter, median filter, Wiener filter, etc.

7. Wiener filter

The Wiener filter is a linear filter proposed by the mathematician Rorbert Wiener with the least square as the optimal criterion. Under certain constraints, the square of the difference between its output and a given function (usually called the expected output) reaches the minimum, and through mathematical operations, it can finally become a problem of solving the Toblitz equation. The Wiener filter, also known as the least square filter or the least square filter, is currently one of the basic filtering methods. Wiener filtering is a method of filtering a signal mixed with noise by using the correlation characteristics and spectral characteristics of a stationary random process.

8. Median filter

Median filtering is a nonlinear digital filter technique that is often used to remove noise from images or other signals. The design idea is to examine a sample in the input signal and determine whether it is representative of the signal, using an observation window composed of an odd number of samples to achieve this function. The values ​​in the observation window are sorted, and the median value in the middle of the observation window is output. Then, the oldest value is discarded, a new sample is obtained, and the calculation process above is repeated. Median filtering is a common step in image processing, and it is especially useful for speckle noise (en:sckle noise) and salt-and-pepper noise (en:salt-and-pepper noise). The edge-preserving feature also makes it useful in situations where edge blurring is undesirable.

2. Code

%------------------------------------------------- ---------
%% DCT image compression matlab
%---------------------------------- ------------------------
clear all
clc
k=4;
X=imread('girl.bmp'); % open image
A=double(X ); % Convert the image to double type
B=zeros(size(A));
C=zeros(size(A));
%------------------- -----------------------------------------
%% For two-dimensional DCT, each small block is 8×8;
%--------------------------------------------- -------------
s=size(A);
for m=1:s(1)
   if rem(m,8)==0
          for n=1:s(2)
            if rem (n,8)==0
                 B((m-8+1):m,(n-8+1):n)=dct2(A((m-8+1):m,(n-8+ 1):n));
                B((m-k+1):m,(n-8+1):n)=0;
                B((m-8+1):m,(n-k+1):n)=0;
            end
        end
    end
 end
%------------------------------------------------ -------------
%% Inverse DCT, image reconstruction
%---------------------------- ------------------------------
 for m=1:128     
       if rem(m,8)==0
                for n=1 :128
                 if rem(n,8)==0
                 C((m-8+1):m,(n-8+1):n)=idct2(B((m-8+1):m,( n-8+1):n));
                end
              end
    end
  end
 %---------------------------------- ------------------------
 %% output two-dimensional image;
 %------------------------------------------------- ---------
  C=uint8(C);
  subplot(1,2,1);
  subimage(X);
  axis square;xlabel('original image')
  subplot(1,2,2);
  subimage (C);
  axis square; xlabel('compressed image');
 

3. Running results

4. References

[1] Hu Xiaojun, Xu Fei. MATLAB Applied Image Processing (Second Edition). Published by Xidian University

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/131482174