【问题标题】:Conv2D produces weird outputConv2D 产生奇怪的输出
【发布时间】: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


【解决方案1】:

我认为问题在于 casted_image = tf.cast(rs_convoluted_image, tf.uint8) 将 [0, 255] 之外的数据截断为纯黑色或纯白色(0 和 255)。

我认为您在转换为 utint8 之前缺少回到 [0, 255] 范围的标准化步骤。

试试

normalized_convolved = (rs_convoluted_image - tf.reduce_min(rs_convoluted_image) / (tf.reduce_max(rs_convoluted_image) - tf.reduce_min(rs_convoluted_image))
normalized_convolved = normalized_convolved * 255

casted_image = tf.cast(normalized_convolved, tf.uint8)

【讨论】:

  • @SebastianG。这张图片有什么问题?你期望什么输出?有白色和黑色像素。如果您希望找到更大的 blob,则应增加内核大小
  • 数据就是这样。请参阅示例here
  • @SebastianG。您可以应用一些颜色更改来将某些位值映射到更暗或更亮的颜色,例如histogram equalization
  • 通常情况下,这个过滤器不用于视觉目的,并且弄乱它的输出看起来是相当无用的。正常使用是blob detection,“灰色”无关紧要,即使转换为uint8也会干扰。
  • 感谢您的解释!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 2020-11-18
  • 2015-08-18
  • 1970-01-01
  • 2012-07-19
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多