Python Conv2D产生奇怪的输出
我试图在我的图像上使用via TensorFlowPython Conv2D产生奇怪的输出,python,image,tensorflow,image-processing,convolution,Python,Image,Tensorflow,Image Processing,Convolution,我试图在我的图像上使用via TensorFlowtf.nn.conv2d。但是输出非常奇怪,我不知道我做错了什么 我通过以下方式加载图片: 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],
tf.nn.conv2d
。但是输出非常奇怪,我不知道我做错了什么
我通过以下方式加载图片:
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
原图:
失败的拉普拉斯:
更新:
灰色输出:
我做错了什么?我无法理解这一点…我认为问题在于
casted_image=tf.cast(rs_coulded_image,tf.uint8)
将[0255]之外的数据截断为纯黑色或纯白色(0和255)
我认为在转换到UTIT8
之前,您缺少一个返回到[0255]
范围的标准化步骤
试一试
使用更新编辑问题please@SebastianG. 这张照片怎么了?你期望的产量是多少?有白色和黑色像素。如果您希望找到更大的BLOB,那么应该增加内核的大小。数据就是这样。请参阅示例.@SebastianG。例如,您可以应用一些颜色更改,将某些位值映射为较暗或较亮的颜色。通常,此过滤器不用于视觉目的,并且对其输出外观进行干扰是毫无用处的。正常情况下,“灰色”并不重要,甚至转换到
uint8
也会产生干扰。它是“卷积的”,而不是“卷积的”。应用卷积的过程是“卷积”。
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)