A Simple Approach for Blur Image Detection

Tania Gupta
Data Science @ Ecom Express
5 min readAug 22, 2023

In modern e-commerce logistics, visual content is generated for different use cases. For example, during the reverse pickup of shipments, field executives are expected to photograph the items being picked for the purposes of documentation and verification. Another example is the self-onboarding process of field executives, where they are supposed to capture images of their identity documents for the purposes of KYC and records.

Fig 1. Two blur images (during reverse pickup): one due to camera shake and the other due to improper focus.

In these processes, we found that quite a fraction of captured images were blurred, as there was no step in the flow to enforce the quality of the image while taking the photographs. A blurred image can be caused by various factors such as camera shake, improper focus, and low light scenarios. To get around this issue, we decided to include a feature in the camera view of our application, which would instantly alert the user if they had taken a blurred picture and force the user to capture the image again. This would ensure that our processes collect only clear images. At the core of this feature, we have a blur image classifier that detects whether the image is blurry or clear. In this post, we discuss the design of a blur image detector that is simple yet results in good accuracy.

Methodology

We first use various edge detection techniques to extract edge features from the images, and then Support Vector Machine (SVM) as a classifier on these features to detect blur in the images using those features. Edge detection techniques in computer vision are an effective method for capturing the sharpness or clarity of an image, and SVM is a popular machine-learning algorithm for classification tasks.

Below is the flowchart explaining all the steps involved in our approach:

Fig 2. Flowchart of the approach used.

Step 1: Prepared a diverse and real-world training dataset containing both sharp and blurred images.

Step 2: After pre-processing, loading, and gray scaling the images, we used three edge detection algorithms Sobel, Roberts, and Laplacian to extract edges from each image in the dataset.

Sobel filter applies a 3x3 matrix to each pixel in a picture. It is used to determine the first-order gradient of the image at that pixel point (both in terms of magnitude and direction). The gradient of an image represents the degree of intensity change at that location.

Robert's filter, like the Sobel operator, is a first-order derivative filter that employs horizontal and vertical filters to identify edges (in terms of amplitude and direction). Thus, it draws attention to edge-related high spatial frequency regions.

Laplacian, in contrast to Sobel and Roberts, employs a second-order derivative of an image, which provides the edge’s location (the zero crossing), only magnitude, and not its direction.

Fig 3. Edge detection from a clear and a blurred Image using three filters (Sobel, Roberts, and Laplacian). Certainly, a clear image has sharper edges than the edges in its blurred image.

Step 3: After applying the three operators mentioned above, we first computed variance over the pixel values of the filtered images. To make the model generalize better on a variety of images, we then calculated two more features, mean and maximum over the filtered pixels. From the figure shown above, we can easily infer the following:

· Clear images have sharp edges; therefore, they will have a high value of mean and maximum as compared to blur images.

· Also, high variance corresponds to a large number of sharp edges in the image.

Since we have 3 edge filters and 3 features (mean, maximum, and variance) corresponding to each filter, thus we have 9 features per image, in all.

Step 4: After extracting the above features, we trained an SVM with these features and corresponding labels (1 for blur, and 0 for clear) for all images in the dataset.

The results were quite decent after validation, but to further enhance the model’s performance, we did hyperparameter tuning for parameters such as the regularization parameter (‘C’ for the SVM) and gamma (‘γ’ for the kernel function). For our use case, after fine-tuning, the best-fit parameters we got were C = 1 and γ = 0.1 with RBF kernel. The value of these parameters may vary according to the use case/ data you are working on.

Results

We used a dataset of 1000 photos, 500 of which were clear and 500 of which were blurry, to test our blur classification algorithm. The model performs remarkably well, with an accuracy rate of 93.80%. This indicates that roughly 94 out of 100 photos were predicted correctly by our model. Furthermore, our model’s precision rate of 94.88% ensured that the majority of anticipated classifications for blur or clarity were correct. Our model proved its capacity to recognize a sizable number of clear and blurry images with a recall rate of 92.60%. The overall F1-Score turned out to be 93.73%.

Table 1. Confusion matrix for a test set containing 1000 images.

Neural network-based blur detection

Although Neural networks can also be a better approach when we talk about images so to compare the inference time, we conducted an experiment, using our model and the neural network-based ResNet 50 model (trained for blur classification tasks). The following results are obtained:

Table 2. Comparison between model trained using a Neural network and an SVM.

For various batch sizes in blur detection, Table 2 shows the time taken by the two models, ResNet 50 and SVM in predicting whether an image is blurred or not. In comparison to ResNet50, SVM consistently displays lower latencies for all batch sizes. For example, even with a batch size of 2000, SVM takes significantly less time compared to ResNet 50. Clearly, our model is a great option for effective and instantaneous blur detection applications.

The goal is to make the solution as simple as feasible while still minimizing latency and complexity to make the model usable in real-time. Neural networks typically consist of multiple layers of interconnected neurons, resulting in many parameters to be learned. In contrast, SVMs aim to find a hyperplane that separates the data, which involves a smaller number of parameters. The simplicity of the model leads to faster training and inference time for SVMs.

Conclusion

We discussed the design of a simple algorithm comprising edge detection techniques from computer vision combined with an SVM classifier that can yield good results vs complex approaches. Additionally, the method presented is relatively faster and computationally efficient than the others.

Authors

Tania Gupta - Associate Data Scientist @ Ecom Express Limited
Uman Niyaz -Data Scientist @ Ecom Express Limited

--

--