【问题标题】:How to plot the image tensor returned by tf.image.sobel_edges如何绘制 tf.image.sobel_edges 返回的图像张量
【发布时间】:2020-01-18 12:01:43
【问题描述】:

我想通过tf.image.sobel_edges在张量流中使用sobel edge。

以下是代码

import tensorflow as tf
import skimage.io
import numpy as np
from tensorflow import keras
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

image = skimage.io.imread('table1.jpg')

image=np.array(image)
image = tf.cast(image, tf.float32)
image=tf.compat.v1.expand_dims(image, 0)

sobel= tf.image.sobel_edges(image)

sess = tf.Session()
SobelImage=sess.run(sobel)
plt.imshow(SobelImage)
sess.close() 

当我运行程序时,我得到了

runfile('E:/projects/Github 程序/图像识别/sobleEdge.py', wdir='E:/projects/Github 程序/图像识别') Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('E:/projects/Github程序/图像识别/sobleEdge.py', wdir='E:/projects/Github程序/图像识别')

运行文件中的文件“C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 827 行 execfile(文件名,命名空间)

文件“C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 110 行,在 execfile exec(编译(f.read(),文件名,'exec'),命名空间)

文件“E:/projects/Github 程序/图像识别/sobleEdge.py”,第 29 行,在 plt.imshow(SobelImage)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py”,第 2677 行,在 imshow 没有别的{}), **kwargs)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib__init__.py”,第 1589 行,在内部 return func(ax, *map(sanitize_sequence, args), **kwargs)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py”,第 369 行,在包装器中 返回函数(*args, **kwargs)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py”,第 369 行,在包装器中 返回函数(*args, **kwargs)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes_axes.py”,第 5660 行,在 imshow im.set_data(X)

文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py”,第 683 行,在 set_data .format(self._A.shape))

TypeError:图像数据的形状无效(1、565、750、3、2)

谁能帮我绘制tf.image.sobel_edges返回的张量

【问题讨论】:

    标签: tensorflow


    【解决方案1】:
    sobel= tf.image.sobel_edges(image)
    print(sobel.shape)
    cv2.imshow("image", image)
    sobel_x = np.asarray(sobel[0, :, :, :, 0])  #  Sobel_X
    sobel_y = np.asarray(sobel[0, :, :, :, 1])  #  Sobel_Y
    cv2.imshow("Sobel_x",sobel_x)
    cv2.imshow("Sobel_y",sobel_y)
    cv2.waitKey(0)
    

    由于 sobel_edge 函数的输出是一个形状为 [batch_size, h, w, d, 2] 的 5D 张量,其中最后两个维度保持 X 和 Y 方向的 Sobel 边缘响应。此外,要显示,您需要先将张量转换为 np 数组,因为 cv2 无法直接处理张量。 输出:click to see the output image here

    【讨论】:

      猜你喜欢
      • 2017-06-20
      • 2019-12-14
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2017-07-11
      相关资源
      最近更新 更多