A spatial image compression algorithm based on run length encoding

Image compression is vital for many areas such as communication and storage of data that is rapidly growing nowadays. In this paper, a spatial lossy compression algorithm for gray scale images is presented. It exploits the inter-pixel and the psycho-visual data redundancies in images. The proposed technique finds paths of connected pixels that fluctuate in value within some small threshold. The path is calculated by looking at the 4-neighbors of a pixel then choosing the best one based on two conditions; the first is that the selected pixel must not be included in another path and the second is that the difference between the first pixel in the path and the selected pixel is within the specified threshold value. A path starts with a given pixel and consists of the locations of the subsequently selected pixels. Run-length encoding scheme is applied on paths to harvest the inter-pixel redundancy. After applying the proposed algorithm on several test images, a promising quality vs. compression ratio results have been achieved.


INTRODUCTION
With the vast increase of usage of social media among people, the amount of images and videos transmitted is increasing dramatically [1], [2]. For example, on January 2019; over 240 billion photos had been uploaded by Facebook website users, with 350 million new daily photos. This requires a huge amount of storage and a very high-speed communication link [3]. Therefore, a scheme to reduce the size of images while maintaining an acceptable image resolution and quality is needed [4]. Image compression is a technique that reduces the size of an image by minimizing the number of bits needed to represent it [5]- [8]. Image compression methods evaluation is an application dependent. A good image compression technique for a given application is not necessarily good for another one [9]- [11].
There are two main categories of image compression techniques; lossless and lossy. In lossless image compression, the quality of the compressed image is similar to the quality of the original image. All information of the original image are preserved in the compressed one [12]. On the other hand, lossy image compression slightly alters the information of original image in a way that decreases the quality of the compressed image comparing to the original one [13]- [15]. In order to judge the efficiency of a compression scheme, a performance measurement is required [16]. The most commonly used performance measurements are: the compression ratio, the execution time of the compression and decompression processes, and the signal ❒ ISSN: 2302-9285 to noise ratio [17]- [19]. The compression ratio (CR), which is calculated using (1), is defined as the ratio between the size of the original image (N 1 ) and the size of the compressed image (N 2 ).
High speed compression and decompression are necessary for real time applications. In lossy approaches, the peak signal to noise ratio (PSNR) measures the quality of the decompressed image [20]. The value of the PSNR in lossless techniques is useless as the original and the decompressed images are identical. The PSNR is calculated using (2) and (3).
where (RMSE) is the root mean square error, M and N represent the length and width of image respectively, f (x, y) is the pixel value in the original image at x and y coordinates, and f ′ (x, y) is the pixel value in the decompressed image at x and y coordinates.
Every image contains some redundant data that can be exploited in image compression [21], [22]. By eliminating such redundant data, the amount of data required to represent the image is reduced. Three different types of redundancy can be found in an image: inter-pixel redundancy, psycho-visual redundancy, and coding redundancy. Inter-pixel redundancy means that the intensities of neighboring pixels are not statistically independent and a correlation do exist among them. Inter-pixel redundancy (also called the spatial redundancy) is related to the fact that a pixel value can be predicted from its location and the values of its neighboring pixels.
The human eye does not respond to all incoming visual information with equal vulnerability; some information has less relative importance. This property is called psycho-visual redundancy and can be used to remove some redundant information without affecting how the human eye perceive the image. Most of image compression algorithms exploit the psycho-visual redundancy, like the discrete cosine transform (DCT) [23]- [25].
Different image compression schemes have been proposed in literature. These schemes are either lossy or lossless. In either case, the ultimate goal is to achieve high compression ratio with reasonable execution time. Some of these schemes are summarized in this section. A review of the compression techniques in digital image processing and a brief description of the main technologies and traditional format that commonly used in image compression have been presented in [26]. Moreover, the formats that are used to reduce redundant information in an image are presented.
The work in [27] presents a new method to find the most redundant image structure elements; like patterns, textures, and edges to compress and encode them. This method is called super-spatial prediction. The goal of super-spatial prediction method is to recognize the best prediction of structure elements by using the idea of motion prediction in video coding. To find the prediction of current image block the super-spatial prediction scheme search for best prediction by calculating the minimum difference between the blocks.
Khalil the authors of [28] introduce a new method of run-length coding; the new method is easier and more efficient than the standard one. It computes the discrete cosine transform (DCT) and works on its quantized coefficients. The experimental results of Khalil enhance the compression ratio compared with the older version of run-length coding method. Because of the speed and the simplicity of his method; it may be used in data compressors such as video compression.
A new compression scheme based on the pixels' locations is proposed in [29]. The scheme simply divides the image into 4×4 non-overlapping blocks and works on each block separately. It takes the most frequent pixel in that block and deletes all of its occurrences, and does that for the other less frequent pixels with the same way. They compare their scheme with known schemes like GIF, TIFF, and PNG. When applying them on 200 textbook images, they claim gaining better compression ratio in 83% of the cases.
Adaptive thinning algorithms by using recursive point removal methods is a new concept for compression that has been proposed by [30]. This scheme is more appropriate for modeling sharp edges and related features in digital images. Their results are compared with another compression method called SPIHT, and the

ISSN: 2302-9285
❒ 2609 quality is represented by the peak signal to noise ratio (PSNR). Their scheme is tested on some sample images and got better results than SPIHT at the sharp edges in the decompressed image. Azman authors in [31] have combined predictive differential pulse code modulation (DPCM) and integer wavelet transform (IWT) in order to achieve a hybrid prediction lossless image compression approach. They have analyzed the performance of their proposed algorithm by calculating the entropy and the compression ratio. Experimentally, sequence of DPCM-IWT-huffman gives the best compression results.
The work of [32] presents two lossless compression schemes, the min-max differential (MMD) and the min-max predictive. The two methods are combined with the existing compression methods to enhance them. The authors apply the proposed schemes for medical images. They test some images of CT brain scans and the results show an improvement over other compression methods such as Huffman encoding, arithmetic coding, and Lempel-Zif.
Two algorithms are proposed by [33] to maximize the performance of JPEG by combining the optimization of discrete cosine transform, huffman code, and quantization of JPEG encoder. The goal of first algorithm is to find the optimal discrete cosine transform indices in the form of run-size pairs. Then, the second algorithm iteratively optimized run-length coding, quantization, and Huffman coding. Both of the proposed algorithms can be used in digital camera image compression, transcoding, and MPEG frame optimization.
A new predictive lossless image compression scheme is found in [34]. The proposed algorithm uses the gradient edge detector as a predictor to remove the spatial data redundancy. The performance of the proposed algorithm is efficient for 12-bit medical images and has a good compression ratio versus JPEG-LS.
A novel medical image compression approach has been proposed in [35]. The approach combines geometric active contour model and quincunx wavelet transform. A level set for an optimal reduction is has been used to localize all the part that contain the pathological. The quincunx wavelet coupled is then used with the set partitioning in hierarchical trees (SPIHT) algorithm. A satisfactory results have been achieved.
The rest of this paper is organized is being as: section 2 discusses the proposed compression technique and illustrates it using an example. The experimental results are presented and discussed in section 3. Finally, the conclusions are provided in section 4.

RESEARCH METHOD
The proposed algorithms exploit the psycho-visual and inter-pixel data redundancies. They are lossy compression algorithms that work with pixels' values in the spatial domain. Thus, the decompressed image will not be exactly similar to the original image.
The proposed method finds paths between neighboring pixels that have same values within some threshold. The path is calculated by investigating the 4-neighbors of a pixel, as shown in Figure 1 (a). One of the neighbors of the central pixel is chosen according to a certain technique. The location of the selected pixel with respect to the central pixel is stored in the path and marked as visited. The locations are relative to the middle pixel. A location can be UP, DOWN, LEFT, or RIGHT as illustrated in Figure 1 (b). A path consists of a starting pixel value and continues with a sequence of pixels locations that are subsequently selected. The criterion to select a pixel out of 4-neighbors is based on two conditions: the selected pixel must be unvisited and the difference between the starting pixel value and the selected pixel value is within a threshold. When there are two or more pixels, among the 4-neighboring pixels, satisfy the conditions, the selection is performed based on the values of the neighbors of those neighbors. In such case, the pixel that has the minimum difference with one of its neighbors is selected. A path ends once none of the 4-neighbors of a selected pixel satisfies the condition. The image is investigated from top-left moving down-right column by column. In the compression stage, the compressed image consists of a series of connected paths. Each path is represented using the value of the starting pixel followed by the consecutive pixels' locations (UP, DOWN, LEFT, or RIGHT). Finally, a STOP code is inserted at the end to indicate the termination of the path. One example of a path is shown in Figure 2. The number of bits needed to represent the value of the starting pixel is 8. The four directions that are used to specify the location of a pixel can be replaced by three path directions (LEFT, RIGHT, and STRAIGHT), because the previous pixel will not be chosen again. Also, it should be noted that a right location will be added at the beginning of any path so that when the path directions are calculated the length of the path is preserved due to the fact that the number of directions will be one less than the number of locations.  Table 1 shows how to map the location of a pixel by the equivalent path direction. It should be pointed out that the STOP code can be represented using three consecutive LEFT or RIGHT direction codes. This can be done because such a sequence is impossible to occur in a real path. This leads to having any path being represented using only the path directions (LEFT, RIGHT, and STRAIGHT) and the starting 8-bit pixel value. As an example, Figure 3 (a) shows a path of pixels (R, D, R, D, D, R, U, R, U, L, U). The path is created based on the location of next pixel with respect to current one. After adding a right at the beginning, the path locations become (R, R, D, R, D, D, R, U, R, U, L, U). All selected pixels must be within the threshold, and each pixel value will be replaced by the value of the first pixel. Figure 3 (b) shows how to construct the path using its direction (S, R, L, R, S, L, L, R, L, L, R).
In the decompression stage, we need to reconstruct the compressed image using all paths. Each path contains the value of the starting pixel, path directions (LEFT, RIGHT, and STRAIGHT), and ends with the STOP code. The directions of each path are converted to pixel locations (as shown in Table 1). Then the decompressed image is constructed by filling the value of the starting pixel on each pixel location.
There are three different approaches that will be explained in the next three sections. In the (D=1) approach, initially, the top-left pixel value is chosen as a starting pixel and is added to the path. Then, one of the 4-neighbors (UP, DOWN, LEFT, or RIGHT) that is unvisited and has minimum difference within the threshold value is selected. The process is repeated for the selected pixel until all neighbors of the newly selected pixel are visited or none of them is within the threshold value. The Stop code is then added to the path. An unvisited pixel from top left moving down right is chosen to be a starting pixel in the second path. The pseudo-code in Figure 4 demonstrates this approach. During constructing the path, if there are two or more neighbors that have the same difference then one of them is randomly chosen. Once all pixels in the image are marked as visited, the path set is completed and is ready to be converted from path location codes to path direction codes. DOWN Neighbor is the down neighbor of a pixel, I is the x Coordinate of the pixel, J is the y coordinate of the pixel, and counter is incremented when a new pixel is marked as visited.

Compression with search depth (D=2)
In search depth (D=2), the top-left pixel value is initially added to path as in search depth (D=1). However, the next pixel in the path is not directly determined based on investigating the 4-neighbors. Instead, in this case all neighbors pixels that are within the specified threshold are determined. Let assume that these pixels belongs to group A. Then the 4-neighbors of each pixel in group A are investigated and those satisfy the criteria are specified. Hence, the pixel from group A that owns more pixels that satisfy the criteria is chosen to be in the path. When all neighbors of selected pixel are visited or none of them is within the threshold value, the stop code is added to path and we select an unvisited pixel from the top left moving down right. The pseudo-code of this approach is depicted in Figure 5. After visiting all pixels in the image, the paths are complete and are ready for conversion from path location to path direction. Figure 5. A pseudo-code which presents (D=2) approach

Compression with search depth D=5
Similarly as in search depth (D=1) and (D=2), in this approach, the top-left pixel value is initially added to the path. The 4-neighbors of the pixel are investigated and their neighbors are investigated and the neighbors of the neighbors are investigated up to 5 levels as long as the pixels do satisfy the unvisited and threshold criteria. Therefor, this approach covers the search depth (D=3)and (D=4). The direction which has more pixels that satisfy the criteria is adopted and the pixel in that direction is added to the path. This added pixel should have up to five unvisited sub-neighbors that are within the threshold. It might have four or three sub-neighbors depending on how many levels we could span. When all neighbors of the selected pixel are visited or none of them is within threshold value, the stop code is added to path and we select an unvisited pixel from top left moving down right. The pseudo-code of this approach is depicted in Figure 6. After visiting all pixels in the image, the paths are complete and are ready to be converted from path location to path direction.

RESULT AND DISCUSSION
The compression techniques presented above have been implemented using Matlab. The efficiency of the algorithms has been tested using some standard images like Peppers, Lena, and Cameraman. The test is carried out by compressing and decompressing the images and then comparing the output (decompressed) image with the input (original) image. For each image, the signal to noise ratio and the root mean square error is calculated as measures for the quality of the output image. Also, the compression ratio for the compressed images are calculated. All of these results have been studied versus increasing the threshold value. The results have been collected for threshold values of 4, 8, 12, 16, and 20. All the different search depth approaches (D=1), (D=2), and (D=5) have been considered. All of the collected results have been listed in Table 2. To demonstrate the impact of increasing the threshold value on the compression ratio (CR) and on the peak signal to noise ratio (PSNR), in Figure 7 (a), the CR is plotted versus the threshold value. Also, in Figure  7 (b), the PSNR is plotted versus the threshold value. This has been done for the Lena image. It can be easily figured out that the CR increases with the increase of the threshold value as shown by Figure 7 (a). While, the quality of the image decreases as the threshold is increased as shown by Figure 7 (b).

CONCLUSION
A spatial lossy compression technique that is based on inter-pixel and psych-visual redundancies has been proposed. The approach in the proposed technique is to find paths between neighboring pixels that have close values within some threshold. Paths are constructed by investigating the neighborhood of the current pixel for a certain depth. In the nearest depth, the next level neighborhood is investigate. While, in next higher depth neighborhood of the neighborhood is investigate and son. In order to determine the direction of the path, the value of the current pixel is compared with the values of all unvisited pixels in its neighborhood. This is repeated for a number of times that is basted on the chosen depth. Three different depths have been tested and the compression ratio versus the image quality results have been reported for five different threshold values. Increasing the search depth from (D=1) to (D=2) would improve the compression ration with a loss in the image quality. On the other hand, very minor changes, on the compression ration and the image quality, arise when moving from search depth (D=2) to (D=5). The techniques have been implemented using MATLAB. Promising compression results have been achieved.