我想检测图像中的模糊度。为此,我将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尺寸,大概是按conv2d
的要求制作了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
用于插入轴。