【发布时间】:2021-07-26 20:59:16
【问题描述】:
我正在尝试通过 TensorFlow tf.nn.conv2d 在我的图像上使用 Laplace Filter。但是输出非常奇怪,我不知道我做错了什么。
我通过以下方式加载图片:
file = tf.io.read_file("corgi.jpg")
uint_image = tf.io.decode_jpeg(file, 1)
image = tf.cast(uint_image,tf.float32)
kernel = tf.constant(np.array([[1, 1, 1],
[1, -8, 1],
[1, 1, 1]]), dtype=tf.float32)
convoluted_image = self.convoluteTest(image, kernel)
rs_convoluted_image = tf.reshape(convoluted_image,
[tf.shape(image)[0] - tf.shape(kernel)[0] + 1,
tf.shape(image)[1] - tf.shape(kernel)[0] + 1, 1])
casted_image = tf.cast(rs_convoluted_image, tf.uint8)
encoded = tf.io.encode_jpeg(casted_image)
tf.io.write_file("corgi-tensor-laplace.jpg", encoded)
但是图像参数不能传递给 tf.nn.conv2d 函数,因为图像张量需要是 4d 张量。
这里的这个函数重塑并应用我的拉普拉斯过滤器:
def convoluteTest(image_tensor, kernel_tensor):
shape = tf.shape(image_tensor)
reshaped_image_tensor = tf.reshape(image_tensor, [1, shape[0].numpy(), shape[1].numpy(), 1])
reshaped_kernel_tensor = tf.reshape(kernel_tensor,
[tf.shape(kernel_tensor)[0].numpy(), tf.shape(kernel_tensor)[0].numpy(), 1,
1])
convoluted = tf.nn.conv2d(reshaped_image_tensor, reshaped_kernel_tensor, strides=[1, 1, 1, 1], padding='VALID')
return convoluted
原图:
拉普拉斯失败:
更新:
灰色输出:
我做错了什么?我无法解决这个问题......
【问题讨论】:
-
它是“卷积的”,而不是“卷积的”。应用卷积的过程是“卷积”。
标签: python image tensorflow image-processing convolution