tensorflow图像裁剪进行数据增强

#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 13mnist.py
@time: 2018/12/17 10:23
@desc:
'''
import tensorflow as tf
import scipy.misc
import matplotlib.pyplot as plt
import random

# 读取图像可任意大小
filenames = ['./tianchi.jpg']
# 创建文件读取队列
filename_queue = tf.train.string_input_producer(filenames)
# 一个阅读器,读取整个文件,返回文件名称key,以及文件中所有的内容value
reader = tf.WholeFileReader()
# Returns the next record (key, value) pair produced by a reader
key, value = reader.read(filename_queue)
images = tf.image.decode_jpeg(value)  # tf.image.decode_png(value)
target_width = target_height = 224

# 裁切图片
with tf.Session() as sess:
    # Coordinator的使用,用于多线程的协调
    coord = tf.train.Coordinator()
    # 启动所有graph收集到的队列运行器(queuerunners)
    threads = tf.train.start_queue_runners(coord=coord)
    height,width,channels = sess.run(tf.shape(images))
    offset_height = random.randint(0,height-target_height)
    offset_width = random.randint(0,width-target_width)
    reshapeimg = tf.image.crop_to_bounding_box(images, offset_height=offset_height, offset_width=offset_width,
                                               target_height=target_height,target_width=target_width)
    print(type(reshapeimg)) # <class 'tensorflow.python.framework.ops.Tensor'>
    reimg1 = reshapeimg.eval() # reimg1的类型是<class 'numpy.ndarray'>
    scipy.misc.imsave('./crop.jpg', reimg1)
    plt.imshow(reimg1)
    plt.axis("off")
    plt.show()
    # 请求线程结束
    coord.request_stop()
    # 等待线程终止
    coord.join(threads)

原始图像480x320x3:

裁剪后224x224x3:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值