Computer Science Engineering
Computer Science-2021.gz:143201
Computer Science
1d50bef9ebb8baac410f332f9589bf26f1552b06
Single image deep defocus estimation and its applications

Depth information is useful in many image processing applications. However, since taking a picture is a process of projection of a 3D scene onto a 2D imaging sensor, the depth information is embedded in the image. Extracting the depth information from the image is a challenging task. A guiding principle is that the level of blurriness due to defocus is related to the distance between the object and the focal plane. Based on this principle and the widely used assumption that Gaussian blur is a good model for defocus blur, we formulate the problem of estimating the spatially varying defocus blurriness as a Gaussian blur classification problem. We solved the problem by training a deep neural network to classify image patches into one of the 20 levels of blurriness. We have created a dataset of more than 500000 image patches of size $32\times32$ which are used to train and test several well-known network models. We find that MobileNetV2 is suitable for this application due to its low memory requirement and high accuracy. The trained model is used to determine the patch blurriness which is then refined by applying an iterative weighted guided filter. The result is a defocus map that carries the information of the degree of blurriness for each pixel. We compare the proposed method with state-of-the-art techniques and we demonstrate its successful applications in adaptive image enhancement, defocus magnification, and multi-focus image fusion.


Introduction
An image captured by a camera is the projection of a 3-D scene onto a 2-D plane. When an object in a scene is outside the focal plane, it is blurred due to defocus. A larger distance from the focal plane leads to a higher level of defocus resulting in more blurriness of the object [1]. The defocus blur is one of the simplest and efficient depth cues used in photography. It allows the viewer to have a rich appreciation of the 3D space [2]. In computer vision, the defocus blur is used in a wide range of applications such as deblurring [3][4][5], blur magnification [6], image quality assessment [7,8], image sharpening [9,10] and depth estimation [11][12][13][14].
Defocus blur estimation has been actively studied in the literature. Some estimation methods used multiple images or special hardware [15][16][17] while others methods rely on a single image. In this paper, we focus on single image defocus blur estimation methods. A common approach is to model the defocus blur in the spatial domain as a linear filter with the impulse response of a Gaussian function. The defocus estimation problem becomes finding the standard deviation σ of the Gaussian kernel for each pixel in the image. The gradient information is usually employed to estimate σ. Tai et al. [18] uses the relationship between the gradient and the local contrast to estimate an initial defocus map which is refined using a Markov Random Field. Others generate a coarse map using a ratio of gradient magnitudes at the edges [19][20][21]. The map is refined using a matting algorithm [22,23] or guided filter [24]. Although these methods Inspired by these works, the motivation of this study is to explore the application of deep neural networks in solving the challenging problem of estimating the defocus map from a single image. The main contribution of this work is that unlike previous works [36][37][38][39][40], we treat the blurriness estimation as a self-supervised multi-class classification problem which is solved by training a CNN to classify a patch of the input image into one of the 20 levels of blurriness. The output of the CNN is a patch-based estimation of blurriness. To obtain an estimate of a pixel-based blurriness, we use an iterative weighted guided filter to perform the refinement which generates the defocus map.
Another main contribution of this work is the development of three algorithms based on the defocus map which carries the blurriness information for each pixel. Since the blurriness is related to the distance between an object and the focal plane, the blur map provides useful information about the depth which is used in the following applications: • Adaptive image enhancement: The defocus map is used to control the level of enhancement in the image to reduce halos and artifacts due to over enhancement of high contrast regions and the enhancement of heavily blur regions.
• Defocus magnification or shallow depth of field: The defocus map is non-linearly transformed to estimate the weights for the combination of the sharpened and blurred images to create a shallow depth of field effect.
• Multi focus image fusion: The defocus map is used to generate a decision map to blend partially in-focus images to form an all-in-focus image.
The organization of this paper is summarized as follows. In section 2 we briefly review the modelling of defocus blur as a Gaussian blur process. We then formulate the problem of defocus estimation as a classification of spatially varying Gaussian blur. In section 3 we present the details of the proposed method including dataset creation, model testing and selection, the training process to classify the blurriness of a patch, a sliding window algorithm for transferring patch-based blurriness information to the pixel-based format, and a refinement process that produces final pixel-based defocus map. In section IV, we present a comparison of the proposed method with 7 other methods for defocus estimation. In section 5 we present 3 algorithms to demonstrate the usefulness of the defocus map on 3 typical computational photography applications: adaptive image enhancement, generating effect of shallow depth of field, and fusion of images which are focused on different parts of the scene. We present experimental results and comparisons for qualitative and quantitative analysis. We summarize the main ideas and contributions of this work in Section 6.

Modelling of defocus and problem formulation
In this section, we briefly review the optical model of out-of-focus and show how it can be approximated by a Gaussian filter [44][45][46][47]. Although a practical optical system usually has more than one moving lens element to correct and fix the focal plane, Fig. 1 allows us to understand the two main reasons for the presence of defocus blur in the observed image: (1) limited depth of field and (2) lens aberrations [45]. To model the defocus by a Gaussian filter, we limit our discussion on the limited depth of field property. As shown in Fig. 1, the point p is in perfect focus because it is placed right in the focal plane. The projection of p on the sensor plane is a single point. According to the thin lens law, the relationship between the distances is given by: where f is the focal length and f 1 is the distance between the sensor plane and the lens plane. We can see that since the point q is placed in a different plane than the focal plane at a distance S 2 from the lens plane, the projection of point q in the sensor plane is not a single point but a region (or a blurred spot) called circle of confusion. The diameter of the circle of confusion for the point q can be calculated as: where A is the diameter of the aperture. The diameter d is directly proportional to A and increases when the difference |S 2 − S 1 | increases. From a system point of view, we can approximate the lens as a linear shift-invariant system. The point source can be regarded as the impulse input signal and the resulting blurred spot due to defocus blur is thus the impulse response. The shape of the impulse response (the blurred spot) can be modelled as a 2D Gaussian where σ is the scale parameter proportional to d, and x, y are spatial variables. Because a scene usually has many objects placed at different distances from the focal plane, these objects will appear to have different degrees of blurriness. Using the linear system model, we can thus model the observed image as J(x, y) = k(x, y; σ x,y ) * I(x, y) + Z(x, y) where I(x, y) is the desired image where all objects are in focus, k(x, y; σ x,y ) is a spatially varying Gaussian kernel, "*" represents the convolution operations, and Z(x, y) models the possible sensor noise [44][45][46][47]. The spatial varyingparameter σ x,y is theoretically determined by the distance between the object at location (x, y) and the focal plane.
From the above brief discussion of the Gaussian model for the defocus blur, We can see that (1) the blurriness is caused by the distance of an object from the focal plane, (2) the defocus blur can be modelled as applying a spatial varying Gaussian filter to an image, and (3) the degree of blurriness can implicitly measure by the scale parameter σ x,y of the Gaussian smoothing kernel. In other words, if we know the parameter σ x,y for an image patch, we can infer its blurriness and reveal its relative distance from the focal plane. Thus, the problem of inferring depth information or the spatially varying blurriness from a single image reduces to determining the parameter σ x,y of the Gaussian smoothing kernel. In this paper, we proposed a CNN-based approach to first classify an image patch into one of the 20 classes. Each class represents a Gaussian smoothing model of a particular parameter σ x,y . The patch-based blurriness map is refined to obtain a pixel-based blurriness map.

Defocus map estimation via CNN based blurriness classification
The proposed method for defocus map estimation has two main building blocks. The first block is a CNN based classifier which takes a patch of 32 × 32 pixels as input and outputs the blur level of the patch. The classifier is applied to pixels in an image using the sliding window algorithm. The result is an image of patch blurriness. The second block refines the image obtained from the first block to produce an image of pixel blurriness. An additional threshold operation can be applied to produce a binary map for applications that require it.
In this section, we first present the details of the dataset creation, the training of a CNN, and how to use the CNN to estimate the blurriness at a patch level. We then describe the refinement method for producing the pixel-level blur map.

Blurriness classification
As mentioned earlier, out-of-focus blur is usually modelled as the result of applying a Gaussian low pass filter on an image. The problem of blur estimation is reduced to finding the kernel of the filter [21] which is parameterized by the standard deviation σ of the Gaussian function. In this work, we follow this model. We take a different approach by casting the estimation problem as a classification problem that can be efficiently solved by a CNN.
More specifically, we aim to train a CNN which can classify 20 different levels of blurriness. To train the model, we need to prepare the dataset. The dataset consists of image patches of different levels of blurriness. A blurred patch is produced by filtering a sharp/unblurred image patch by a Gaussian filter with a specific σ. Different levels of blurriness are generated by using different values of σ. For an image patch, we generate 19 blurred patches of different blur levels resulting in 20 classes (including the unblurred patch). The 19 levels of blurriness are generated by Gaussian filters of σ = 1, 2, ..., 19. The unblurred patch can be regarded as being filtered by the filter with σ = 0 whose impulse response is a unit sample sequence. An example of patches of 20 levels of blurriness can be seen in Fig.2, from top to bottom and left to right the standard deviation σ ranges from 0 to 19. We use the value of σ as a label for the patch. The justification of why 20 levels of blurriness are selected can be seen in Fig. 2, the difference in blurriness for patches where σ > 15 is almost imperceptible for the human eye so 20 levels of blurriness is the optimal equilibrium between efficiency and complexity.
The size of the dataset depends on the number of unblurred/sharp patches. To find as many as possible sharp patches to generate the dataset for training the CNN, we use the DIV2K [48] database which has 1000 high-resolution images of diverse contents. To find sharp image patches from the database, we convert each image in the database to gray-scale and divide them into patches of 32 × 32 pixels, denoted I p . We first filter each patch I p by a 3 × 3 Gaussian kernel to produce a slightly smoothed patch B p . The Gaussian filter is used to remove noise in the patch which could lead to a wrong measurement of the sharpness. We use the variance of the Laplacian of a patch as a measure of the sharpness for the pth patch, which is calculated as where ∇ is the Laplacian operator, ∇B p is the mean value of ∇B p (q) over the all pixels in the patch, q is the pixel index, and N is the number of pixels in the patch. Patches satisfying φ p > 1000 have a high level of sharpness and are regarded as in sharp focus and are assigned the blur level 0. Using the above procedure for patch selection and generation, we create a gray-scale image dataset with more than 5 × 10 5 patches of 20 classes. The dataset is split into 3 sets for training, validation, and testing. Table 1 details the number of patches of each set. Using this dataset, we train a neural network to classify the blurriness of a 32 × 32 gray-scale image patch into one of the 20 classes. As such, the trained network has an input array of size (32 × 32) and the network output is an integer ranging from 0 to 19 indicating the level of blurriness. The development framework is TensorFlow Keras. Because our goal is to train a classifier for blurriness estimation, we do not attempt to develop new neural network models. Instead, we test some well-known models such as MobileNetV2, NASNet and DenseNet and ShufflenetV2 [29,[49][50][51]] to find a model which has good accuracy and is of small size. We have trained these models and presented results in Table 2. We can see that the MobileNetV2 [29] has a relatively higher accuracy for the test data (0.97) and is computationally more efficient with low complexity and low memory requirements (the model size is 28MB). This indicates that it is a more efficient and accurate tool for the estimation of the defocus map. ShufflenetV2 [51] presented the worst performance among the models, even after we tuned the hyper-parameters. The sub-optimal performance can be attributed to the small patch size for which the ShufflenetV2 is not designed to work with. It is expected that using a larger patch size will lead to an improvement in the accuracy. However, this is at the increase of the computational complexity in the refinement process of the proposed algorithm because of excessive overlaps of pixels in neighbouring patches. We train MobileNetV2 using the following parameters. The batch size is 128. The sparse categorical cross-entropy function is used to calculate the loss. Adam optimization method is used with a learning rate of α = 0.001, an exponential decay rates for the 1st and 2nd moment estimates are β 1 = 0.9 and β 2 = 0.999 and a stability constant = 10 −8 [53]. After 100 epochs, classification accuracy is 97.04% on the test set. The accuracy and loss for the validation and training set after each training epoch is shown in Fig. 3.  Table 3 displays results of the MobileNetV2 including the precision, recall and F1-score for each class when evaluated using the testing set. The model can predict with high levels of accuracy on the testing dataset. However, we can identify two sources of error that are not considered here: 1) error due to noise in the image, which can lead to classify the patch as sharper than it is and 2) error because the patch being classified is not represented by those examples in the dataset, which could result on inaccurate predictions. We minimize the effect of 1 and 2 during the refinement stage.

Blur map estimation
The trained neural network predicts the blur level of a patch of 32 × 32 pixels. This section explains how to obtain the blur map of the whole image at a pixel level using this patch estimation. The blur-map (denoted M ) is an image of the same size as the input image (denoted I). A pixel at location q within the pth patch is denoted I p (q). The image I is divided into overlapping patches of (32 × 32). The extraction of patches is implemented by using a sliding window method. The amount of overlap depends on the step-size of moving the widow from the current location to the next one. Results presented in this paper are produced with a step size of 16 pixels unless specified. Each patch, e.g., the pth patch is fed to the trained network. The network output, denoted O p , is assigned to every pixel in the patch. Because of patch overlapping, one pixel belongs to multiple patches. Specifically, let Ω q be the set of patches where the pixel I(q) belongs to. The blurriness of the pixel at location q is defined as the average of classification results due to all patches which contain the pixel I(q): where |Ω q | is the number of patches which have the pixel I(q). The rationale for performing the average is this. A pixel I(q) belongs to |Ω q | patches. Each patch produces a prediction of the blurriness of this pixel. A simple way to aggregate these predictions is by taking the average. In addition, since the classification result O p is in the interval [0, 19], the blur-map M (q) is also in the same interval.
We now discuss the computational complexity. Assume the CNN takes T seconds to process a patch. The processing time for an image is given by: where N is the number of pixels in the image and s is the step-size. The optimal step-size depends on the image resolution and the complexity of the image. Generally, for images of large size the step size should be 32 pixels to avoid long processing times. For images of small size, better results are obtained by using a step size of 4 pixels such that so small objects with different blur levels are preserved. As a rule thumb we suggest to use a step size that can preserve the main structure of the image.

Refinement of the defocus map
The defocus map obtained using a patch and sliding window approach needs to be refined to get rid of the undesirable blocking effect and to make the edges follow those in the original image. A well-known tool for the refinement is the matting Laplacian [22] which produces the alpha matting matrix. However, this method is computationally very expensive. The Guided Image Filter (GIF) [24] has demonstrated a performance comparable to that of the matting Laplacian algorithm at a lower computational cost. The weighted version of the GIF called Weighted Guided Image Filter (WGIF) [54] improves the performance of the GIF and produces fewer halo effects. However both GIF and WGIF transfer details from the guidance image to the processed image, which is an undesirable feature for our applications because texture information from the original image will be transferred to the refined defocus map leading to wrong predictions in pixel blurriness. To overcome this limitation, we propose to use a WGIF [54] based algorithm to refine the defocus map. A new feature in the proposed method is the use of a smoothed version of the original image (through edge-aware filtering) as a guide to producing a sharp defocus map without transferring the texture information.  Figure 4 shows the block diagram of the proposed refinement process. First, we perform edge-aware filtering of the original image to smooth out texture information. The filtered image, which retains information of sharp edges, is used as a guidance image in the WGIF algorithm to refine the defocus map. We aim to keep only silhouettes of elements in the image which should present an equal level of blurriness preserving large scale edges. To produce the guidance image I smooth we use the WGIF [54] iteratively. Figure 5 shows an example of the refinement algorithm. The blur map shown in Fig. 5b was estimated using the proposed CNN approach with a step size of 16 pixels. It was then refined by using the proposed algorithm with a patch radius r = 16, regularization parameter = 0.005, and the number of iterations N iter = 7. The guidance image is shown in Fig 5c and the refinement result is shown in Fig 5g. Only the fish's silhouette has been transferred to the refined defocus map. The darker area represents the area in sharp focus, while the brighter area represents the area of more out of focus. We can see that the refined defocus map accurately represents objects in focus such as the fish in the foreground and blurred objects such as those fish in the background. Figure 5 also shows the result of refining the blur map with other methods such as an iterative WGIF [54] using the original image as guidance, mutually guided image faltering muGIF [55] and matting Laplacian [15] to visually appreciate the effectiveness of our method. All four methods clearly remove the blocking effect and wrong predictions in the blur map. However, the WGIF does not preserve edge consistency. The smoothing effect of muGIF is very strong, it removes all the background objects and shrinks the main object's silhouette. Apart from being computationally too expensive, the matting Laplacian algorithm properly preserves the objects in the background but it does transfer some undesirable details from the original image to the defocus map (brightness on the upper side and tail of the closest fish). Our method does preserve defined edges and smooths objects at a lower computational cost.
Another important characteristic of a our refinement method is that it accurately preserves the defocus level classified by the CNN, i.e., after the refinement process is applied the effect of wrong predictions are mitigated and the edges are improved while the defocus prediction values are preserved. This can be better appreciated in Figure 6a, where we created a binary version of the original defocus map, the binary image is obtained by assigning a value 0 to every (a) Input (b) Blur map (c) Guidance (d) WFIG [54] (e) muGIF [55] (f) Matting [22] (g) Proposed (a) CNN prediction (b) WFIG [54] (c) muGIF [55] (d) Matting [22] (e) Proposed

Comparison of defocus estimation algorithms
In this section, we compare the proposed method with other methods in two categories: classical methods such as entropy, standard deviation and variance of the laplacian of the patch, and modern methods including three handcrafted features "Lbp-based segmentation of defocus blur" from Yi et al. [56], "Defocus map estimation from a single image" from Zhuo et al. [20] and "Fast defocus map estimation" (FDM) from Chen et al. [57] and a CNN based method from Lee et al. "Deep Defocus Map Estimation Using Domain Adaptation" (DMNet) [41].

Comparison with classical methods
We studied three methods: entropy, standard deviation, and variance of Laplacian [58]. The ideas of these three methods are similar. The blurriness of a pixel is measured by a quantity such as the entropy or standard deviation of a patch of pixels centred at that pixel. For the variance of Laplacian, the image is filtered by using the Laplacian operator. The blurriness of a pixel is measured by the variance of a patch of pixels of the filtered image centred at that pixel. For these three methods, we use a patch size of 16 × 16 to calculate the entropy, standard deviation and variance of the Laplacian. Simulation results are shown in Fig. 7. The proposed CNN-based method classifies the level of defocus properly for both the edges of objects and the featureless patches. On the other hand, the entropy and standard deviation of the patch can only classify adequately the edges and classify all featureless patches as out of focus. Because both methods are trying to capture edge detection rather than blurriness estimation, they failed to capture information that the fish in the foreground is in focus, see e.g., the certain parts of the body of the fish which are in bright area indicating the area is wrongly classified as out of focus by these two methods. We made a similar observation when we compared the results of the CNN-based method with the variance of the Laplacian. For example, the tail of the fish in the foreground is in bright grayscale indicating it is wrongly classified as out of focus by this method.

Comparison with modern methods
The bird image shown in the top row of Fig. 8 presents different levels of defocus blur. Yi's algorithm was implemented using a threshold T LBP = 0.025, for each level we use square local regions of size s 1 = 7, s 2 = 11, s 3 = 15 and finally we set the weight β = 0.25 for the multi-scale inference step. Yi's algorithm does not segment the bird which is completely in focus. Zhuo's algorithm is shown in column (c). We set the edge threshold as e = 0.01 and the standard deviation as σ = 1 for the blur detection step. The interpolation step was performed using λ = 0.001 and a maximum blur level M = 10. Zhuo's method successfully identify that the bird is in focus. However a few details from the background are transferred to the defocus map after the interpolation step. Chen's method (FDM) uses Zhuo's approach to estimate the blur level on edges, but uses the SLIC algorithm [59] for the interpolation. In our simulation, the SLIC parameters are set as: 200 super-pixels and a standard deviation σ = 0.75. As shown in the figure, for this particular image the focused region is not well defined. In comparison, the DMNet has a very good performance in capturing both background and foreground blurriness levels. The result produced by the proposed algorithm is shown in column (f). Our method can represent the different levels of blurriness in the bird image according to the CNN prediction. Our refinement method reduces the number of details transferred from the original image to the defocus map, this can be appreciated more clearly in the background.
Another comparison example is shown in the last row of Fig. 8. We can see that Yi's algorithm performs better on this image capturing and segmenting focused pixels in the foreground. The methods of Zhuo and Chen have difficulty removing details in the foreground especially in the toy's eyes region. DMENet produces a sharp and accurate defocus map. Our method assigns the lowest blur level to the foreground allowing us to differentiate the focused region from the rest of the image.

Applications
In this section, we demonstrate applications of the proposed defocus map estimation in adaptive image enhancement, creating shallow depth of field, and multi-focus fusion.

Adaptive enhancement
Unsharp masking (UM) [60] is frequently used for image enhancement. The Unsharp Masking (UM) algorithm can be described as J(q) = I(q) + λZ(q) (8) where λ is used to control the enhancement level, Z(q) = I(q) − B(q), and B is a low pass filtered version of I. For a fixed λ, UM performs a higher degree of enhancement in higher contrast or dynamic regions of the image. To raise the sharpness level of a region with low contrast, the parameter λ has to be set to a relatively higher value, resulting in over enhancement in regions of high contrast. As such, an undesirable non-natural looking result may be produced. Besides, natural images may present different blur and contrast levels in different areas. Having a constant scale-factor λ for the whole image could also lead to the sharpening of regions that are intentionally produced with a high level of blur, e.g., a highly smoothed background to produce a shallow depth of field. It is not desirable to sharpen those areas. Different methods have been proposed to address those problems, including using non-linear or edge-aware filters [61][62][63] and a pixel adaptive λ that produces a spatially varying enhancement [10,64]. We follow the same idea of using a pixel adaptive λ and propose an adaptive unsharp masking algorithm such that the parameter λ for each pixel is a nonlinear function of the refined defocus blur map. The aim of the nonlinear function is to avoid the enhancement of sharp regions as well as in heavily blurred regions. We use the following nonlinear function: where λ max is the desired maximum gain level, λ 1 and λ 2 are two sigmoid functions: The four parameters α 1 , β 1 , α 2 and β 2 are determined by the user to have control over the sharpening gain. An example of controlling the gain is shown in Fig. 9. In this example, we set α 1 = 50, β 1 = 0.1 and α 2 = 25 are kept fixed while β 2 varies. The scale of the blur map is linearly normalized from the interval [0, 19] to [0, 1] for better visualization. β 1 and β 2 indicate the points where the gain is 0.5 × λ max and α 1 and α 2 set the growth speed of the curve.
In Fig. 10, we present an example of obtaining the gain map through a non-linear transformation of the defocus map. Fig. 10a shows the input image with different levels of blurriness. For example, the bird in the foreground is relatively sharp in comparison with the branches and the background which is heavily defocused. Enhancing this image is a real challenge. Fig.10b and Fig.10c show the defocus blur map and the gain map respectively. The gain map was calculated using the nonlinear transformation setting α 1 = 46, β 1 = 0.1, α 2 = 183, β 2 = 0.27 and λ max = 2. We aim to adaptively enhance the image by preventing over-sharpening in focused regions and avoiding sharpening in blur sections. Fig.10 also shows the comparison of our method with the classical unsharp masking (UM) and two newer methods contrast adaptive sharpening (CAS) 2 and Generalized Unsharp Masking (GUM) [65]. We can see that applying UM with a fixed global gain λ = 2 produces an unpleasant sharpening in the background (green box) and over-sharpening of areas in focus such as the indicated by the orange, red and blur boxes. The CAS algorithm was implemented using a gain λ = −0.125. We can see in Fig. 10f that the algorithm performs very well in regions with medium and high defocus blur. However, it can not deal with the blur areas properly producing a noisy result(green and red boxes). The GUM algorithm (Fig. 10e) handles both background and foreground properly with a gain λ = 7 and a contrast factor κ = 5 × 10 −4 . But it produces strong halo artifacts when the gain increases. The result of applying the proposed method is shown in Fig. 10g. It does not sharpen the heavily blurred background (green box) and prevents over-sharpening of highly focused pixels by limiting the sharpening gain (orange, red and blue boxes). We perform the image quality assessment (IQA) of the images shown in Fig.10 and present the results in Table 4. We use a series of no-reference (NR) IQA methods such as BRISQUE [66], IL-NIQE [67], NBIQA [68], PSS [69] and BLIINDS-II [70] to objectively evaluate the performance of the proposed algorithm. These metrics take into account features from the spatial domain or transform domain to assess the quality and naturalness of an image. We should point out that these quality measurements use different scales. For example, while for the BRISQUE method a smaller score means a better image quality, for the NBIQA method a larger score means a better quality. In Table 4 we can see that our method achieves the best result on BRISQUE, IL-NIQUE and PSS, and second-best on NBIQA, This indicates that the proposed method produces a more natural-looking image enhancement than the other three methods. However, BLIINDS-II gives the second best score to UM algorithm leaving our method in 3rd position. In fact, from Fig.10 we can see that UM algorithm produces excessive halo artifacts due to enhancement in high contrast regions such as shown in the red box. The BLIINDS-II score for the UM result is thus debatable. Therefore, such quality metrics do not always generalize well as a full-reference metric. It is always advisable to perform a visual comparison when possible. Indeed, in image enhancement applications in which the user of the image is human, he/she will manipulate parameters of the algorithm to adjust the image until a satisfactory outcome is produced. This a very subjective process which cannot be replaced by a quality metric.

Defocuss magnification
In photography, the shallow depth-of-field (DoF) technique is used to make the main object stand out from the background by producing an image in which in-focus objects are with great details and contrast, while out-of-focus objects are greatly smoothed. In a camera, the DoF is controlled by a combination of three factors: the f-number, the focal length, and the distance of the camera from in-focus objects. Due to practical limitations such as a lens with a small diameter, it is not always possible to obtain the desired shallow DoF (SDoF) effect. Enhancement of an image by producing/increasing the SDoF effect is thus a practical image processing problem.
Computer vision algorithms were recently developed to achieve the shallow depth of field effect. For example, in DeepLens [71], the depth information from a RGB-D sensor was used to train a CNN to create the SDoF effect from an image that has everything in focus. P. Sakurikar et al. [72] proposed an end-to-end GAN architecture that selectively increases the level of blurriness in the input image while keeping the edges of the in-focus object intact. Also, in [73]  an object segmentation based algorithm differentiates main objects from the those in the background. The latter are blurred to create the SDoF effect.
A key idea in creating a SDoF effect from a single image is to perform a pixel adaptive weighted combination of a smoothed version denoted B(p) and a sharpened version of the input image denoted S(p), We take this approach in this paper by using the defocus map to estimate the weights W (q). The smoothed image B is produced by using one of the edge-aware smoothing filters such as guided filter [24] or one of its weighted versions [74,75]. The guided filter has two parameters: the radius of the neighbourhood r and the smoothing parameter . A bigger value of r results in larger structures in the image being smoothed out, while a bigger value of leads to the overall greater degree of smoothness. To better control the smoothing result, we iterate the guided filter in a self-guidance mode. In our experiments presented in Fig. 11, we use r = 33, = 128, and 5 iterations. The sharpened image S is produced by the classical unsharp masking algorithm with a fixed λ = 0.25 for illustration purpose.
The weight map W is determined by the nonlinear mapping: where σ and γ are two parameters.
The justification for the nonlinear transformation and the calculation of σ and γ can be explained as follows. When a pixel is within an in-focus area which corresponds to M (q) ≤ c 0 (e.g., c 0 = 1 is user defined constant) the sharpened image should have greater weight such that the in-focus area is not blurred in the output image. Referring to equation (12), this requires that W (q) = W 0 → 1. Similarly, when a pixel is within an out-of-focus area which corresponds to M (q) ≥ c 1 (e.g., c 1 = 7 is also a user defined constant), the blurred image should have a greater weight such that in the output image the out-of-focus area is further blurred to create the SDoF effect. This requires W (q) = W 1 → 0. With these considerations, the two parameters σ and γ can be determined. For example, we can set W 0 = 0.999 (for M (q) = c 0 ) and W 1 = 0.001 (for M (q) = c 1 ) and calculate the two parameters based on equation (13). In our experiments, the values of c 0 and c 1 were set depending on the image and the defocus levels present application.
Experimental results are shown in Fig. 11 which show that the proposed algorithm can effectively produce the SDoF effect. In the original images shown in Fig. 11a the foreground is in focus while the background is slightly out of focus. After applying the non-linear transformation the weight map splits the image in two well-defined regions. Pixels in focus are with W (q) ≈ 1 while pixels out of focus are with W (q) ≈ 0 as shown in Fig. 11b. The resulting SDoF image is shown in Fig. 11c it is evident that the main subject remains sharp while the defocused background is further smoothed. The last column of this figure shows the results from using the DeepLens algorithm. We can see that the proposed algorithm is able to smooth the background to a greater degree.

Multi-focus image fusion
Most of the lenses have an inherent reduced depth of field, so capturing an image where all the objects in the scene are properly focused is almost impossible. Multi-focus image fusion is a technique that combines two or more partially focused images to produce an image in which objects of interests are in focus. A common approach for multi-focus image fusion is through a weighted combination of partially in focus images where the weights define which pixels in the original image are in focus and which ones aren't. Multi focus image fusion is a widely studied topic and many algorithms have been proposed. A recent literature review [76] divided the fusion algorithms into 4 main categories: transform domain, spatial domain, combined transform and spatial domains, and deep learning methods. In this paper, we propose a method to combine 2 or more images using the defocus map to recognize in-focus and out-of-focus regions on each input image. We generate a pixel-based weight map for each input image which allows us to blend them through a linear combination of the weighted images on a pixel level aiming to obtain an all in-focus image. Fig.12 presents the block diagram for the proposed algorithm. The diagram only considers two input images for simplicity but the algorithm can handle multiple input images. The fusion of a set of N images denoted {I n } n=1:N can be described as: where the weights W n (q) are normalized such that N n=1 W n (q) = 1. To determine the weight for each image I n , we first use the proposed CNN model to obtain blur map which is refined to produce M n . We then generate the decision map as follows Thus the decision map D n (q) has a value of 1 only if the pixel of I n (q) presents the lowest level of blurriness among all N input images. This map allows us to keep from each input image only those pixels that are most in focus.
To avoid halo artifacts around edges we use a guided filter [24] to refine the edges of the decision map, where we use GF r, (r, are the two parameters) to denote the guided filtering process with D n being the image to be filtered and I n being the guidance image. Since we applied the guided filter to each decision map, we must normalize each map to obtain the weights W n (q) as follows, δ represents a small positive regularization term to avoid the division by zero problem and to guaranty that there will be a contribution of each image to the result even when all the weights are zero.
To demonstrate the work-flow of the proposed method, we present the fusion process of two partially in-focus images in Fig.13. Input one has the foreground in focus while the background is out of focus. On the other hand, input two has the background in focus and the foreground out of focus. The defocus maps are presented in Fig. 13b. The resulting decision maps and the normalized weight maps are shown in Fig.13c and 13d, respectively. The fused result is shown in Fig.13e which clearly shows that the resulting image has both foreground and background in focus. Next, we present a comparison of the proposed method with four state-of-the-art methods: "Guided filter-based multifocus image fusion through focus region detection" (GFDF) [77], "Image fusion with guided filtering" (GFF) [78], "Multi-exposure and multi-focus image fusion in gradient domain" (IFGD) [79] and a recent method called "SESF-Fuse: an unsupervised deep model for multi-focus image fusion" (SESF) [80]. In Fig. 14 we present two fusion results, we applied these algorithms to two pairs of images from the Lytro dataset [81].
The first example is shown on the first row on Fig. 14, images on column a and b are the inputs to the fusion algorithm. Input A has the foreground in focus and input B has the background in focus. Both foreground and background contain text. The magnified boxes show that our algorithm fuses both images successfully. The resulting image, shown in column f contains sharp text in both foreground and background. The result is comparable with GFF, GFDF and SESF in terms of sharpness and edge definition. Our method has better contrast than IFGD producing a more pleasant fusion effect and sharper text. A similar outcome can be seen in the second row on Fig.14, this example uses two input images with a small defocus blur difference between them (columns a and b), we can see in column g that our algorithm can still fuse the images and obtain an all in focus result. The proposed method produces a similar result as GFF, GFDF and SESF but outperforms IFGD. Quantitative measurement is a complicated task due to the absence of ground truth. We use a set of standard quality metrics that have been used to evaluate the performance of the fusion methods: QG [82] is a gradient-based method that measures the edge information transferred from the input images to the result, QP [83] uses image phase congruence to measure the quality of the fused image, QY [84] employs the structural similarity metric (SSIM) [85] between the inputs and fused image using local windows to evaluate at which level the fusion result contains all the complementary information from the inputs, QCB [86] considers contrast and saliency to evaluate the fusion result, and finally QFMI [87] uses the mutual information to calculate the amount of information transferred to the fused image. Table 5 shows the result of the objective assessment. The proposed method has high scores as GFDF and SESF but it is clearly better than GFF and IFGD in these 3 images.

Conclusion
In this paper, we address a challenge problem of estimating the degree of defocus blur from a single image. This work is based on the assumption that the defocus blur can be modelled as spatially varying Gaussian blur. As such the variance of the Gaussian kernel can be used as a proxy for the degree of blurriness. We train a CNN to classify the blurriness of an image patch as one of the 20 classes. Each class label is the variance of a Gaussian kernel. We create a dataset by (1) choosing sharply focused patches of size (32 × 32) from a high resolution image dataset, and (2) filtering those patches by using Gaussian filters of different standard deviations σ ranging from 1 to 19 to produce 20 classes of blurriness (the original patch is regarded as being filtered by a kernel with σ = 0). The compiled dataset is used to train a deep CNN model called MobileNetV2 to classify patches into 20 different levels of blurriness. Using a sliding window algorithm and a weighted guided filter-based refinement, a pixel level defocus map is obtained.
We have tested and validated the proposed method by comparing it with classical and state-of-the-art methods. We have also demonstrated applications of the blur map estimation in adaptive image enhancement, producing shallow of depth of field effects, and multi-focus image fusion. Results have shown that the proposed algorithms lead to competitive performance and are thus effective tools in single image blurriness estimation and a range of applications.