tensorflow使用日记(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xianqianshi3004/article/details/84034190

Arrays and working with Images

首先处理图片

要在一个图片上工作,我们需要安装matplotlib,同时我们也需要安装pillow包(用来支持多种样式的图片格式)
我们可以用conda 来安装(也可以用pip安装)

conda install matplotlib pillow
import matplotlib.image as mpimg
import os
# 首先要加载一个图片
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path + "/MarshOrchid.jpg"
# 把图片用数组的形式读入
image = mpimg.imread(filename)
#print shape#(5528, 3685, 3)分别代表高,宽,RGB三元素
print(image.shape)

结果图

接下来我们把图像逆时针旋转:

代码入下:

x=tf.Variable(image,name='x')
model=tf.global_variables_initializer()
with tf.Session() as session:
    x=tf.transpose(x,perm=[0,1,2])
    session.run(model)
    result=session.run(x)
plt.imshow(result)
plt.show()

其中用到tf.transpose函数:

tf.transpose(
    a,##转置对象,可以理解成一个Tensor
    perm=None,##a 的维数的排列。
    name='transpose',##操作的名称可选
    conjugate=False##若为true则是求共轭
)

所以上述代码实现的效果是如下图:
在这里插入图片描述
接下来是反转一个图片:
(PS:这个转置函数我也没有搞很明白)

height, width, depth = image.shape
x = tf.reverse_sequence(x, np.ones((height,)) * width, 1, batch_dim=0)

效果如下:
在这里插入图片描述

 x = tf.reverse_sequence(x, np.ones((width)) * height, 0, batch_axis=1)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xianqianshi3004/article/details/84034190