Before the speech

This paper mainly recommends a simple method to identify images with frosted glass, Gaussian blur and other effects.

01

Frosted glass effect

The principle of the frosted glass effect is to walk through each pixel, randomly select a pixel around this pixel, and replace the current pixel. Can be implemented using OpencV, the code is as follows:

#coding:utf-8
import cv2
import numpy as np
import random

img = cv2.imread('test.jpg',1)
shape = img.shape
h = shape\[0\]
w = shape\[1\]
dst = np.zeros((h,w,3),np.uint8)
mm = 8          
for m in range(h-mm):             
    for n in range(w-mm):
        index = int(random.random()\*8)
        (b,g,r) = img\[m+index,n+index\]
        dst\[m,n\] = (b,g,r)
cv2.imwrite("result.jpg", dst)
Copy the code

The whole process is to select the range of surrounding pixels with a maximum distance of 8 pixels. The following figure is shown before and after the above code is run:

02

Identify fuzzy effects such as frosted glass

Next, I’ll show you how to use OpenCV, Python, and Laplacian to calculate the amount of blur in an image,

If there is a background in signal processing, the first method to consider is to calculate the fast Fourier transform of the image, and then check the distribution of low and high frequencies: if the image has only a small number of high frequencies, the image is considered fuzzy. However, defining what counts as a low number of high frequencies or a high number of high frequencies is quite difficult.

The method presented in this paper can calculate a single floating point value to represent the degree of ambiguity of a given image by simply taking a single channel of an image (roughly grayscale) and convolving it with the following 3 x 3 kernel, and then taking the variance of the response (i.e. the square of the standard deviation). If the variance is lower than the pre-defined threshold, the image is considered blurred; Otherwise, the image will not blur.

The method is as simple as the following line of code

cv2.Laplacian(image, cv2.CV_64F).var()
Copy the code

The reason this method works is because of the definition of the Laplacian operator itself, which is used to measure the second derivative of the image. Laplacian operators highlight areas of the image that contain rapid gradient changes, much like Sobel and Scharr operators. Like these operators, Laplacian is often used for edge detection. The assumption here is that if an image has a high variance, it means that the image has a wide range of responses, including edges like and non-edges like, which is a representation of a normal focused image. But if the variance is very low, then there is very little response spread, indicating that there are very few edges in the image. And the more blurred the image, the fewer edges it has. So it can be used to detect whether it’s fuzzy.

Obviously, the key here is to set the right threshold, and the threshold setting is dependent on the set of images applied. If the threshold is too low, you will mistakenly mark an otherwise non-fuzzy image as blurred. If the threshold is too high, an image that is actually blurred will not be marked as blurred. This approach works well only with very stable sets of images (of the same type).

The implementation code is as follows: define variance_of_laplacian. This method takes the image we want to calculate the focal length (assuming a single channel, such as gray image) as the parameter, first reads the input image image, and then converts it to gray level, and then applies fuzzy detection with OpenCV. Through the self-set threshold value, fuzzy images and non-fuzzy images can be differentiated.

import cv2 def variance\_of\_laplacian(image): return cv2.Laplacian(image, Cv2.cv \_64F).var() imagePath = "test.jpg" image = cv2.imread(imagePath) gray = cv2.cvtcolor (image, cv2.COLOR\_BGR2GRAY) fm = variance\_of\_laplacian(gray) if fm < 100: print("Blurry")Copy the code