检测 RGB 图像中的模糊度

Detecting blurriness in a RGB image

我想检测图像中的模糊度。为此,我将 RGB 图像转换为灰度图像并将其与拉普拉斯核进行卷积并计算方差。如果方差很高,则图像会聚焦,否则会模糊。我在下面附上了代码片段的 ss。但我收到一个错误。请帮我处理这段代码。下面是对图像进行卷积并计算方差的函数:

def is_blur(image) :
   """
   This function convolves a grayscale image with
   laplacian kernel and calculates its variance.
   """
   #Laplacian kernel
   laplacian_kernel = np.array([[0,1,0],[1,-4,1],[0,1,0]])
   laplacian_kernel = tf.expand_dims(laplacian_kernel, 0)
   laplacian_kernel = tf.expand_dims(laplacian_kernel, 0)
   laplacian_kernel = tf.cast(laplacian_kernel, tf.float32)
   #Convolving image with laplacian kernel
   new_img = tf.nn.conv2d(image, laplacian_kernel, stride=[1, 1, 1, 1], padding="SAME")
   #Calculating variance
   img_var = tf.math.reduce_variance(new_img)
   return img_var

下面是加载图片和调用上面函数的代码:

from tqdm import tqdm
path = "/content/Data/Train/{0}/"
N_CLASSES = 43
blurness_list = []
for itr in tqdm(range(N_CLASSES)) :
   files = os.listdir(path.format(itr))
   for img in files :
      image_string = tf.io.read_file(path.format(itr) + img)
      #decoding image
      image = tf.image.decode_png(image_string, channels=3)
      #Converting image to grayscale
      image = tf.image.rgb_to_grayscale(image)
      # This will convert to float values in [0, 1]
      image = tf.image.convert_image_dtype(image, tf.float32)
      #Reshaping image since conv2d accepts a 4-d tensor.
      image = tf.reshape(image, shape=[1, image.shape[0], image.shape[1], 1])
      blurness_list.append(is_blur(image))

以下是我遇到的错误:

InvalidArgumentError:输入深度必须被过滤器深度整除:1 vs 3 [Op:Conv2D]

请帮我看看这个代码。

这应该很容易修复:您正在构建 2d 3x3 过滤器,然后通过 tf.expand_dims 在开头插入 size-1 维度,大概是为了使过滤器成为 [=13] 所要求的 4d =].但是,conv2d 需要过滤器为 width x height x in_channels x out_channels,即过滤器维度需要在末尾 。这应该可以解决它:

laplacian_kernel = np.array([[0,1,0],[1,-4,1],[0,1,0]])
laplacian_kernel = tf.expand_dims(laplacian_kernel, -1)
laplacian_kernel = tf.expand_dims(laplacian_kernel, -1)
laplacian_kernel = tf.cast(laplacian_kernel, tf.float32)

或者,请注意,您也可以这样做:

laplacian_kernel = laplacian_kernel[..., None, None]

其中 None 用于插入坐标轴。