# Determining Thresholds for Optimal Adaptive Discrete Cosine Transformation
## 2 Materials and Methods
### Proposed Adaptive DCT Algorithm  
Let us represent the image as a function \(g\):  
\[IMG=g(x,y), \tag{1}\]  
where \(x\) and \(y\) are the coordinates of an image pixel. The restored image after compression with thresholds \(pm\) and \(ps\) can be defined as follows:  
\[IMG^{\prime}=\widehat{g}_{pm,ps}(x,y). \tag{2}\]  
Let us introduce an evaluation function \(f\). We will assume that it possesses a local minimum at certain optimal threshold values \(pm\), \(ps\). Thus, the mathematical definition of the chosen problem is to find the value  
\[\underset{pm,ps}{\text{min}}\{f(g(x,y),\widehat{g}_{pm,ps}(x,y))\}. \tag{3}\]  
for each image. Then, a data-fitting approximation of arbitrary order is applied to the set of \"original level of image detail-optimal threshold value\" pairs for each of the two thresholds. It should be noted that, when the value of the function \(f\) is close to the local minimum, the value of MS-SSIM approaches 1. The standard SSIM formula is as follows:  
\[SSIM(x,y)=\frac{(2\mu_{x}\mu_{y}+c_{1})(2\sigma_{xy}+c_{2})}{(\mu_{x}^{2}+\mu_ {y}^{2}+c_{1})(\sigma_{x}^{2}+\sigma_{y}^{2}+c_{2})}. \tag{4}\]  
where \(\mu_{x}\) is the pixel sample mean of \(x\), \(\mu_{y}\) the pixel sample mean of \(y\), \(\sigma_{x}^{2}\) the variance of \(x\), \(\sigma_{y}^{2}\) the variance of \(y\), \(\sigma_{xy}\) the covariance of \(x\) and \(y\), \(c_{1}=(k_{1}L)^{2},c_{2}=(k_{2}L)^{2}\) two variables to stabilize the division with weak denominator, and \(L\) the dynamic range of the pixel values, \(L=255\), \(k_{1}=0.01\) and \(k_{2}=0.03\). MS-SSIM is a composition of several SSIM measurements taken for downscaled versions of the image. The relative importance coefficients for the 5 measurements are default and as such 0.0448, 0.2856, 0.3001, 0.2363 and 0.1333, respectively, for the scales from 1 to 5 [18]. To set up the optimization process, three quality levels were derived. They are different in the target MS-SSIM (minimum acceptable value during optimization) and quantization values:  
* High level corresponds to target MS-SSIM = 0.98, quantization value 10 and typically minor degradation;
* Medium level: target MS-SSIM = 0.90, quantization value 30, noticeable degradation in quality, but many details are preserved;
* Low level: target MS-SSIM = 0.80, quantization value 100, significant quality degradation while preserving the most detailed areas. An example of adaptive DCT is shown in Figure 3.  
Figure 2: Example of adaptive 3D DCT in a video frame, showcasing both fragmenting and replacing entire fragments with their average tones. Figure 3 shows the basic principles of constructing an image quadtree via the proposed method. In this study, we will not use RMSE to determine ADCT thresholds and instead use the values of TDV and ITDV, because expanding the tonal distribution of an image increases tone visibility and does not directly affect detail while changing the RMSE value. Vice versa, narrowing the tonal range does not increase detail, yet it changes the RMSE value as well. Examples of tonal histograms of images are shown in Figure 4.  
One can see that the amount of detail cannot be measured by calculation using actual tone values: the RMSE will change as the histogram changes. However, the image histogram can be used for its original purpose, namely visualizing the tonal distribution. Making an array out of pixel brightness values associated with a histogram results in an array of 256 elements, each one corresponding to a pixel brightness value. Each element is then assigned the number of corresponding pixels of the image. The order of these elements does not affect the calculation of the mean value and the variance, so changes in brightness or histogram normalization also do not affect the calculations.