1 Introduction

Images containing noise are called corrupt or noisy images. Noise in images refer to the random variations in their brightness and color information. Noisy images will be visually displeasing as they may not reveal all the details of corresponding original scenes. This makes the removal of noise from such images essential, prior to any subsequent processing. Image enhancement techniques play a key role in removing the noise from noisy images and generating visually pleasing and more informative images. Such techniques may be grouped into spatial and frequency domain methods. Spatial domain methods operate directly on individual pixels (intensity transformations) or group of pixels (spatial filtering). Frequency domain methods, on the other hand, operate on the Fourier transforms of the images [1]. An inverse Fourier transform is performed to get the enhanced image [2].

Histogram manipulation forms the basis for numerous image enhancement techniques in the spatial domain. The histogram processing methods operate on individual pixels of noisy images to enhance them by performing intensity transformations. As the present work focuses on spatial filtering, the interested reader is directed elsewhere [3,4,5,6,7] for some recent research related to histogram processing. The spatial filtering techniques initially use linear filters that operate on a group of pixels, selected using a window of specified size. Severe blurring in images filtered by linear filters led to the development of nonlinear filters. A median filter is the most widely used nonlinear filter [8]. The simplicity in implementing the conventional median filters resulted in many variants such as the weighted [9], center-weighted [10], improved [11] median filters and some based on higher-order ternary patterns [12] and average filter residual [13]. The median filters not only perform well in smoothing noise and preserving edges, but also produce interest regions of almost constant pixel values. While the techniques based on local binary patterns were found to be inefficient for capturing texture details, second-order local ternary pattern (SOLTP) proved to be better even than its higher-order derivative variants. The higher-order derivatives have been found to introduce noises from source other than artifacts caused by median filtering. Another drawback with the SOLTP technique is that it has a large dimensonality and some heuristic technique is applied to split it [12]. As the conventional median filters also result in image blurring as they replace each pixel, whether noisy or not, with the median of their neighboring pixels within the selected window [14], switching median filters were developed. These filters perform noise reduction prior to filtering to overcome the blurring problem of conventional median filters. They first classify each pixel as corrupted or uncorrupted based on some criteria and then replace only the corrupt pixels. Some of the switching median filters from the literature are presented here.

Switching median filters with adaptively changing window sizes have been shown to yield better results [15]. These filters involve complex computational procedures for restoring the images. A progressive switching median filter has been shown to remove low to medium level noise densities [16]. This filter, however, fails to preserve the edges as noise level increases. Another switching median-based nonlinear adaptive algorithm with non-stationary assumption has been proposed to remove the impulse noise in images [17]. This algorithm fails to remove impulse noise in high frequency regions such as edges. An algorithm based on convolution kernels convolves the degraded images with four kernels and uses the minimum of the result to classify the pixels as corrupted or uncorrupted [18]. In another attempt, a two-phased switching median filter has been proposed. In the first phase, an adaptive median filter (AMF) is used to classify the pixels into corrupted or uncorrupted. The second phase applies a specialized regularization method to the corrupted pixels to preserve the edges and suppress the noise [19]. Both phases use windows of 39 \(\times \) 39 size, making the method more complex and computationally expensive.

Decision-based switching median filters, realized by appropriate thresholding operations, have been introduced to avoid modifying good pixels. This filtering method first uses an impulse detector to classify the input pixels into corrupted or uncorrupted pixels and then a noise reduction filter to modify the corrupted pixels. The threshold value chosen dictates the efficacy of the filter. As single threshold may not suffice the entire image with varying noise levels, multiple threshold switching (MTS) hence came into use [20]. Combining boundary discriminative noise detection (BDND) into switching median filters has shown more effectiveness than some of the AMFs [16], especially for images having high noise levels [21]. This method uses a 21 \(\times \) 21 sized window initially for noise detection. The boundaries calculated during the detection process are used to classify the pixels into corrupted or uncorrupted. A 3 \(\times \) 3 sized window is then used to confirm the classified pixel based on a more confined local statistics. Finally, an adaptive window is used for filtering based on the number of uncorrupted pixels within the prescribed window.

The AMF and other switching median filtering methods use only the median values for modifying the corrupted pixels. Alternatively, a sorting based algorithm in which the corrupted pixels are replaced by either the median pixel or neighborhood pixel has been proposed [22]. This algorithm does not preserve edge and fine details satisfactorily at higher noise densities. Robust statistics-based nonlinear estimation techniques are also useful for addressing the problem of image enhancement. A novel robust estimation-based filter has been proposed to effectively remove fixed value impulse noise [23]. The Lorentzian estimator has been used to estimate the original image from noisy image. This filter is shown to remove low to high density fixed value impulse noises of up to 90% density with edge and detail preservation. An improved decision-based algorithm that suppresses the impulse noise effectively has been proposed [24]. This filter replaces a corrupted pixel with the mean value of already processed neighboring pixels inside the filter window. This filter is also found to exhibit inadequate performance in terms of preserving edges and fine details. An adaptive decision-based robust statistics estimation filter for high density impulse noise in images and videos has been proposed [25]. This filter replaces the corrupted pixel by the median of pixels inside the filter window. If the median is also corrupt, size of the window is increased for eliminating the corrupted pixel. Another highly effective, high density impulse noise detection algorithm for switching median filtering has been proposed [26]. Despite satisfactorily removing the impulse noise, these filters are computationally expensive.

Modification to the filtering step of BDND [21] has been shown to result in improved peak signal-to-noise ratio (PSNR) values [27]. The modified algorithm detects the noisy pixels via two stages. The first stage uses the BDND detector, and the second stage uses convolution kernels to classify the pixel as noisy. Another modified BDND detector has also been shown to yield better results on both color and gray-scale images [28]. The larger window size still poses serious problems of increased computational complexity and time. This issue led to the development of an adaptive window size to reduce the computational complexity and time without deteriorating the performance of filtering in terms of subjective (visual) and objective (PSNR) quality [29]. In a similar attempt, another switching median filtering method for removing impulse noise from the images has been proposed [30]. This method replaces a noisy pixel by the median pixel value if it is not impulse, otherwise by the already processed immediate top neighboring pixel in the filtering window. This filter has been shown to sufficiently filtering out the noise besides exhibiting good response at the image edges. In another attempt, a novel method for removing impulse noise from magnetic resonance (MR) images has been tried [31]. An effective noise detection method, called decision method (DM) filter, has been introduced for enhancing the median filter. This method is shown to be capable of attenuating impulse noise, while preserving the high portion of noise-free pixels. More recent improvements in the switching median filtering technique have found application in the field of image compression and decompression [32]. The technique initially used filtered images but did not get the expected performance. Median filtering was then implemented to retain the image texture for further use. A recursive switching adaptive median filter was also proposed to restore noisy images using an adaptive mask of varying size  [33]. The methodology of choosing the noisy pixel was based on the immediate neighboring pixel and hence does not show outstanding performance if the noise was level was high.

The foregoing discussions reveal that switching median filters also blur the original images significantly. Some of the reported variants of switching median filters are complex and computationally expensive, while some of them fail to preserve the finer details of the images and often yield blur images especially with the Guassian noise pattern. This motivated the authors to develop new image enhancement algorithms that do not blur the original image significantly and also enhance the contrast of the image. The proposed algorithms are based on switching median filtering technique (originally proposed in [30]). Performance of these algorithms is evaluated in terms of visual appearance of enhanced images, peak signal-to-noise ratio (PSNR) and structural similarity index (SSIM). The experimental results reveal that the proposed algorithms succeed in their intended objectives.

The rest of the paper is organized as follows. In Sect. 2, we describe about the switching median filtering algorithm and the proposed switching median filtering based algorithms. In Sect. 3, we describe about the noises considered in the present work and the method of simulating them. The results obtained using the algorithms are described in Sect. 2 and are presented and discussed in Sect. 4, and finally, the conclusions and future scope are presented in Sect. 5.

2 Image Enhancement Algorithms

Enhancement of images is aimed at improving the contrast and color information contained in noisy images. Image enhancement methods entail operations that improve the appearance of images to a human viewer and for providing superior input to other programmed image processing techniques. The following subsections describe about the switching median filtering algorithm and proposed switching median-based filtering algorithms.

2.1 Switching Median Filtering Algorithm [30]

The median filter and its variants are among the most commonly used filters for image enhancement. The median filter modifies both noisy (corrupted) and noiseless (uncorrupted) pixels, resulting in image blurring and distortions. In contrast, the switching median filters modify a pixel value only when it is identified to have been corrupted. The switching median filtering algorithm (Algorithm 1) first computes the values of all pixels of the image. A 3 \(\times \) 3 window is then slid over the image starting from top left corner. The value of a pixel is compared with the highest and lowest pixel values within the window to identify it as uncorrupted or corrupted. A pixel is considered uncorrupted if its value lies between the highest and lowest values and if the value lies outside the range, otherwise it is considered as corrupted. An uncorrupted pixel is left undisturbed, while a corrupted pixel is replaced by the median pixel value or already managed immediate adjacent pixel within the selected filtering window. The procedure is repeated for all pixels in the image.

figure a
Fig. 1
figure 1

Comparison of image enhancement using the switching median filtering and proposed algorithms for different images without superimposing any noise (i.e., 0% noise level) a original images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

Table 1 PSNR and SSIM values for different images

2.2 Proposed Filtering Algorithms

The proposed filtering algorithms are based on the switching median filtering algorithm. In the first algorithm (Algorithm 2), pixel matrix of the simulated image is computed first. Then, a 3 \(\times \) 3 window is slid over the image. The first pixel of current window is updated first, and then, all the pixels are sorted in ascending order. The central pixel of current window is assigned as the median pixel. Then, the 2nd and 8th pixels are checked whether corrupted or not. Any pixel lying between the 2nd and 8th pixels is considered as uncorrupted pixel, otherwise as corrupted. There is no need to update the uncorrupted pixels, and only corrupted pixels are updated. Corrupted pixels are replaced by the median pixel of the current window. If the median pixel is corrupted, it is replaced by already processed immediate pixel.

The second algorithm (Algorithm 3) uses a 4 \(\times \) 4 window size. The first pixel of current window is updated first and all the pixels are then sorted in ascending order, eliminating the need for padding or stuffing the image with extra bits. As there are 16 pixels inside the current window, there will be two middle pixels. The median pixel is calculated as the mean value of these middle pixels. The median pixel is compared with the 4th and 13th pixels to check whether the pixels are corrupted or not. The pixels lying between the 4th and 13th pixels are considered as uncorrupted pixels and others as corrupted. The uncorrupted pixels are retained and the corrupted pixels are replaced by the median pixel of the current window. If the median pixel is corrupted, it is replaced by the already processed immediate pixel.

figure b
figure c
Fig. 2
figure 2

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for cameraman image with different Gaussian noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

Fig. 3
figure 3

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for peppers image with different Gaussian noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

Fig. 4
figure 4

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for Scenery image with different Gaussian noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

3 Simulation of Images

Noises corrupt the digital images during their acquisition, transmission and processing. Images are acquired using the image sensors, and the performance of imaging sensors is affected by factors such as quality of sensing elements, environmental conditions during image acquisition, knowledge and expertise of the person, etc. For example, sensor temperature and lighting levels affect the noise level in images acquired using CCD cameras and the interference in transmission channels corrupt the images during their transmission. The quality of image processing circuitry and image processing algorithms also contribute to noise in the images. Three benchmark images have been used for studying the performance of image enhancement algorithms discussed in Sect. 2. The images superimposed with Gaussian and salt-and-pepper noises of varying % levels are taken as input to these algorithms. A brief description of Gaussian and salt-and-pepper noises and the method of simulating them are described in the subsections below [34].

3.1 Gaussian Noise

The Gaussian noise, occurring during image acquisition, includes the sensor noise caused by poor illumination, high temperature, transmission (electronic circuit noise), etc. The Gaussian noise model is frequently used in practice due to its mathematical tractability in both the spatial and frequency domains. This noise, though can be reduced using a spatial filter, may lead to blurring of fine-scaled image edges as they correspond to the blocked high frequencies. The Gaussian noise can be simulated using the Gaussian distribution. The probability density function (PDF) of a Gaussian random variable z is given by Eq. (1).

$$\begin{aligned} p(z) = \frac{1}{2\pi \sigma }e^{\frac{-(z-\mu )^{2}}{2\sigma ^{2}}} \end{aligned}$$
(1)

Here, z represents the gray level, \(\mu \) represents the mean or average value of z and \(\sigma \) is its standard deviation.

3.2 Salt and Pepper Noise

The salt-and-pepper noise is also called as impulse or spike noise. This type of noise can be caused by analog-to-digital converter errors, bit errors in transmission, etc. An image with impulse noise will have dark pixels in bright regions and bright pixels in dark regions. This noise severely reduces the image quality and causes loss of details. The impulse noise can be simulated using the probability density function (PDF) of random variable z using Eq. (2).

$$\begin{aligned} p(z) = {\left\{ \begin{array}{ll} P_a &{} \quad \text {for z = a} \\ P_b &{} \quad \text {for z = b} \\ 0 &{} \quad \text {otherwise} \end{array}\right. } \end{aligned}$$
(2)

Here, a and b are the minimum and maximum allowed pixel values in the image and \(P_{a}\) and \(P_{b}\) are their respective probabilities. If \(b > a\), gray level b will appear as light dot in the image and as dark dot otherwise. If \(P_{a}\) and \(P_{b}\) are nearly equal, impulse noise values will resemble salt-and-pepper granules randomly distributed over the image.

4 Performance Metrics

The performance of image enhancement algorithms can be expressed qualitatively or quantitatively. The first metric is based on visual quality of the image. Second metric enables the user to quantify the image enhancement efficacy in terms of certain parameters. The following subsections describe two such parameters. All these are full reference metrics, i.e. the measure is based on the original image.

Table 2 PSNR values for different Gaussian noise levels
Table 3 SSIM values for different Gaussian noise levels
Table 4 Computational time (in seconds) for the algorithms tested on Gaussian noise-induced images

4.1 Peak Signal-to-Noise Ratio (PSNR)

The PSNR value determines the peak value of signal-to-noise ratio between the original and filtered images in dB. It is generally used as a feature measurement between the images. The PSNR values can be calculated using Eq. (3).

$$\begin{aligned} PSNR = \mathrm{log}_{10}\left( \frac{\mathrm{MAX} \times \mathrm{MAX}}{MSE}\right) \end{aligned}$$
(3)

Here, MAX is the maximum pixel value of the image matrix and MSE is the mean square error [28]. A larger PSNR value implies better quality of the enhanced image. The MSE value can be calculated using Eq. (4).

$$\begin{aligned} MSE = \sum _{i=0}^{m-1}{\sum _{j=0}^{n-1}{\frac{[X(i,j) - F(i,j)]^{2}}{m \times n} } } \end{aligned}$$
(4)

Here, X(ij) and F(ij) represent the (ij)th pixel of the original image and the enhanced image respectively, and m and n are the row and column of the image matrix [20].

4.2 Structural Similarity Index (SSIM)

Pixels have strong inter-dependencies especially when they are spatially close. These dependencies carry important information about the structure of the objects in the visual scene. SSIM makes use of this dependency and quantifies the visual quality with a similarity measure between two images, original and noise-removed, as the product of three components, viz. luminance comparison, contrast comparison and structure comparison, calculated using the mean, variance and standard deviation as shown in Eq. (5). The value of SSIM lies in the range \([-1, 1]\). Two images will have their SSIM indices to be 1 when they are identical [35].

$$\begin{aligned} SSIM=[l(x,y)]^{\alpha }\times [c(x,y)]^{\beta }\times [s(x,y)]^{\gamma } \end{aligned}$$
(5)

where l(xy) represents the luminance comparison, c(xy) represents the contrast comparison, s(xy) represents the structure comparison, and \(\alpha ,\beta ,\gamma \) are positive exponents that adjusts the components contribution to the overall SSIM. All their values have been taken as 1.

$$\begin{aligned} l(x,y)= & {} \frac{(2\mu _{x}\mu {}_{y}+C_{1})}{(\mu _{x}^{2}+\mu _{y}^{2}+C_{1})},\nonumber \\ c(x,y)= & {} \frac{(2\sigma _{x}\sigma _{y}+C_{2})}{(\sigma _{x}^{2}+\sigma _{y}^{2}+C_{2})},\nonumber \\ s(x,y)= & {} \frac{(\sigma _{xy}+C_{3})}{(\sigma _{x}+\sigma _{y}+C_{3})} \end{aligned}$$
(6)

Here, \(\mu _{x}\), \(\mu _{y}\) denote the mean, and \(\sigma _{x}\), \(\sigma _{y}\) denote the standard deviation of an image x and y. These terms can be calculated using Eqs. (7 and 8).

$$\begin{aligned} \mu _{x}= & {} \frac{1}{N}\sum _{i=1}^{N}x_{i}~\text {and}~\mu _{y} =\frac{1}{N}\sum _{i=1}^{N}y_{i}\end{aligned}$$
(7)
$$\begin{aligned} \sigma _{x}= & {} \sqrt{\frac{1}{N-1}\sum _{i=1}^{N}(x_{i}-\mu _{x})^{2}}\,\mathrm{and}~\nonumber \\ \sigma _{y}= & {} \sqrt{\frac{1}{N-1} \sum _{i=1}^{N}(y_{i}-\mu _{y})^{2}} \end{aligned}$$
(8)
Fig. 5
figure 5

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for cameraman image with different salt-and-pepper noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

Fig. 6
figure 6

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for peppers image with different salt-and-pepper noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

Fig. 7
figure 7

Comparison of image enhancement using the switching median filtering and proposed filtering algorithms for Scenery image with different salt-and-pepper noise levels; a simulated images; b images enhanced with switching median filtering algorithm; c images enhanced with proposed algorithm 1; d images enhanced with proposed algorithm 2

The term \(\sigma _{xy}\) in Eq. (6) denotes correlation, which is calculated using Eq. (9)

$$\begin{aligned} \sigma _{xy}=\frac{1}{N-1}\sum _{i=1}{N}(x_{i}-\mu _{x})(y_{i}-\mu _{y}) \end{aligned}$$
(9)

The terms \(C_{1}\), \({C}_{2}\) and \({C}_{3}\) in Eq. (6) can be calculated using Eq. (10). These are small positive constants that combat the stability issues when the sum of squares of \(\mu _{x}\) and \(\mu _{y}\) or that of \(\sigma _{x}\) and \(\sigma _{y}\) becomes close to zero.

$$\begin{aligned} C_{1}=(K_{1}.L)^{2},~C_{2}=(K_{2}.L)^{2},~C_{3}=\frac{C_{1}}{2} \end{aligned}$$
(10)

where \({K}_{1}\) and \({K}_{2}\) are small constant less than 1 and L is the dynamic range of pixel values. The values of \({K}_{1}\), \({K}_{2}\) and L have been taken as 0.01, 0.03 and 255, respectively.

5 Results and Discussion

The image enhancement algorithms (Sect. 2) and the simulation methods (Sect. 3) for simulating all the images considered in the present work have been implemented using MATLAB. These image enhancement algorithms are compared in terms of visual appearance, peak signal-to-noise ratio (PSNR) and structural similarity index (SSIM). The results obtained using the selected images are presented and discussed in the subsections below. The acronyms SMFA, PFA1 and PFA2 in the tables and figures below denote the switching median filter algorithm and proposed filtering algorithms No. 1 and 2, respectively. The SSIM values are calculated using original image as reference.

5.1 Image Superimposed with 0% Noise

Images superimposed with 0% noise, i.e., original images, are given as input to the image enhancement algorithms considered and the output images generated by these algorithms are shown in Fig. 1. The objective of this attempt is to study the changes in output images in the absence of noise and its extent. The visual appearance of these images show some corruption in the case of switching median filtering algorithm while almost no change has been observed in the cases of proposed algorithms. The PSNR and SSIM values shown in Table 1 reveal that the proposed filtering algorithms yield consistently higher values for these metrics than the switching median filtering algorithm. The SSIM value for noise-free image is expected to be 1. The proposed filtering algorithms yield SSIM values very close to 1, while that of the switching median filtering algorithm is significantly less than 1. This proves that the proposed algorithms do not make any significant unwanted changes in output images in comparison to the switching median filtering algorithm.

5.2 Images Superimposed with Gaussian Noise

Original images superimposed with varying levels of Gaussian noise are given as input to the image enhancement algorithms and the corresponding output images generated by these algorithms are shown in Figs. 23 and 4. Visual appearance of the output images shows that the proposed algorithms filter the noise better than the switching median filtering algorithm. The extent of enhancement obviously decreases with increase in noise levels. The PSNR (Table 2) and the SSIM (Table 3) values reveal that the proposed filtering algorithms yield consistently higher values than that of the switching median filtering algorithm. The ideal value of SSIM is 1, which seldom happens in reality. The proposed filtering algorithms yield SSIM values very close to 1, way ahead of the switching median filtering algorithm. This also proves that the proposed algorithms filter Gaussian noises better than switching median filtering algorithm. The computational times in Table 4 correspond to 5% noise. It may be seen that the proposed algorithms use almost same amount of computational time used by switching median filtering algorithm, but produce better filtering effect. The data corresponding to noisy column of Table 3 indicate the SSIM values of unfiltered images.

5.3 Images Superimposed with Salt and Pepper Noise

The output images generated by different image enhancement algorithms using the original images superimposed with varying levels of salt-and-pepper noise are shown in Figs. 56, and 7. Their visual appearance of images reveal that the proposed algorithms filter the noise better than switching median filtering algorithm and that their quality decreases as the noise levels increase. The tabulated values of PSNR (Table 5) and SSIM (Table 6) reveal that the proposed filtering algorithms consistently yield significantly higher values than the switching median filtering algorithm, which proves that the proposed algorithms filter the salt-and-peppers noise better than the switching median filtering algorithm. It may be noted that values in noisy column of Table 6 indicate the SSIM values of unfiltered images. The computational times in Table 7, which correspond to 5% noise, indicate that the proposed algorithms take only slightly more computational time than the switching median filtering algorithm to produce better filtering effect.

Table 5 PSNR values for different salt-and-pepper noise levels
Table 6 SSIM values for different salt-and-pepper noise levels
Table 7 Computational time (in seconds) for the algorithms tested on salt-and-pepper noise-induced images

6 Conclusion and Future Scope

Two new algorithms, based on the switching median filtering technique, have been proposed in the present research. The proposed algorithms avoid misclassification of the pixels into corrupted and uncorrupted pixels. The algorithms preserve the uncorrupted pixels and replace only the corrupted pixels thereby generating visually pleasing images. They also preserve the essential features of the images, namely edges and other finer details. The proposed filtering algorithms are thus more effective in terms of noise elimination and feature preservation capabilities. The second algorithm with 4 \(\times \) 4 selection window performs decently better than the first algorithm that uses a 3 \(\times \) 3 selection window. Based on the appearance of output images, PSNR values, SSIM values and computational times presented in Sect. 5, it can be concluded that the proposed algorithms enhance the noisy images better than the switching median filter algorithm that too with just a marginally increased computational times. The development of adaptive versions of the proposed algorithms may be taken up as future work.