# Efficient Implementation of Matrix-Based Image Processing Algorithms for IoT Applications
## 2 The Algorithm Description  
The above-mentioned algorithm is based on sparsity of discrete cosine transform. The CS-RANSAC algorithm is used to choose the non-noisy pixels. The DCT coefficients are determined using the compressive sensing method. The image to be processed is divided into blocks of smaller size \((B,B)\) (chosen according to the size of the two-dimensional DCT transform) and each block is processed separately. Two-dimensional DCT transforms (direct and inverse) will be calculated as matrix multiplications, considering that the block to be processed is transformed into a column vector, \(\mathbf{x}_{b}\) of dimensions \((N,1)\) with \(N=B^{2}\), which will be multiplied by a matrix \(\mathbf{W}\) of dimensions \(\left(B^{2},B^{2}\right)\) determined by the DCT transformation matrix \(\mathbf{T}\) with \(\mathbf{W}=\mathbf{T}\otimes\mathbf{T}\), where \(\otimes\) is the Kronecker product. DCT transform will be \(\mathbf{X}=\mathbf{W}\mathbf{x}\), and the inverse DCT transform is determined as \(\mathbf{x}=\mathbf{W}^{-1}\mathbf{X}=\mathbf{W}^{T}\mathbf{X}\). For each of the blocks, a subset \(\mathbf{y}_{b}\) of set \(\mathbf{x}_{b}\) with \(S<B^{2}\) pixels are randomly chosen. Matrix \(\mathbf{A}\) is then determined, which contains the lines of the transpose of \(\mathbf{W}\) corresponding to the indices of the elements chosen from \(\mathbf{x}_{b}\). Using matrix \(\mathbf{A}\) and vector \(\mathbf{y}_{b}\), the first \(k\) coefficients of the two-dimensional DCT transform are determined. Using these coefficients, the \(\mathbf{x}_{rb}\) vector is reconstructed, and the error between \(\mathbf{x}_{b}\) and \(\mathbf{x}_{rb}\) is determined. These steps are repeated until the error is small enough or the number of iterations exceeds a maximum imposed number. The last determined DCT coefficients will be the coefficients used to restore the pixels from the processed block. The algorithm is designed so that it can be implemented as efficiently as possible (both on a computer with infinite precision and on a microcontroller with fewer resources and lower precision). The critical elements for microcontroller implementation are matrix multiplication and calculation precision, especially when implementing the inverse matrix function. These aspects will be discussed in the microcontrollers' implementation section. The algorithm is illustrated in Figure 1. In this figure, the necessary explanation has been included as comments enclosed between \"/*\" and \"*/\". Figure 1a illustrates the core of the algorithm, and Figure 1b,c show the compressive sensing function and pseudo-inverse function, respectively. The algorithm uses two functions:  
* The _CSrec_ function, which receives as parameters the set of randomly chosen elements for determining non-noisy pixels, the modified transformation matrix, and the number of DCT coefficients (sparsity factor) and returns the DCT coefficients that will be used to reconstruct the elements in the block [13]. * The _pseudoinv_ function, which calculates the inverse of a matrix using an iterative method [17]. It was implemented in MATLAB R2024b (on a computer with an Intel I5-10210U processor at 1.60 GHz, 4 cores, 6 MB cache memory, 16 GB memory RAM, and operating system Windows 10 Pro 64-bit) in order to validate the its functionality and to evaluate the performances obtained with infinite precision.