tensorflow中的随机裁剪函数random_crop

来源:互联网 发布:centos和redhat的区别 编辑:程序博客网 时间:2024/06/01 23:13

tf.random_crop是tensorflow中的随机裁剪函数,可以用来裁剪图片。我采用如下图片进行随机裁剪,裁剪大小为原图的一半。
截取至百度图片中的千与千寻
如下是实验代码

import tensorflow as tfimport matplotlib.image as imgimport matplotlib.pyplot as pltsess = tf.InteractiveSession()image = img.imread('D:/Documents/Pictures/logo3.jpg')reshaped_image = tf.cast(image,tf.float32)size = tf.cast(tf.shape(reshaped_image).eval(),tf.int32)height = sess.run(size[0]//2)width = sess.run(size[1]//2)distorted_image = tf.random_crop(reshaped_image,[height,width,3])print(tf.shape(reshaped_image).eval())print(tf.shape(distorted_image).eval())fig = plt.figure()fig1 = plt.figure()ax = fig.add_subplot(111)ax1 = fig1.add_subplot(111)ax.imshow(sess.run(tf.cast(reshaped_image,tf.uint8)))ax1.imshow(sess.run(tf.cast(distorted_image,tf.uint8)))plt.show()

如下是随机实验两次的结果

第一次运行结果
第二次运行结果