tensorflow实现数据增强的API

#实现image open
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

name='./library.jpg'
image_string=tf.read_file(name)#读取的图片为一个字符串
image_decoded=tf.image.decode_image(image_string)#将字符串解析成图片

with tf.Session() as sess:
	image_decoded_val=sess.run(image_decoded)
	print(image_decoded_val.shape)
	#%matplotlib inline #jupyter使用
	imshow(image_decoded_val)
	plt.show()

以上是tensorflow实现的图片的打开,查看。

接下来实现图片大小的转换,resize,将图片大小扩大一倍。

#实现resize
#tf.image.resize_area
#tf.image.resize_bicubic 
#tf.image.resize_nearest_neighbor
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

name='./library.jpg'
image_string=tf.read_file(name)#读取的图片为一个字符串
image_decoded=tf.image.decode_image(image_string)#将字符串解析成图片
image_decoded=tf.reshape(image_decoded,[1,217, 500, 3])#1-batch size
resize_image=tf.image.resize_bicubic(image_decoded,[217*2,500*2])
resize_image=tf.cast(resize_image,tf.uint8)#float->uint8
with tf.Session() as sess:
	image_decoded_val=sess.run(resize_image)
	print(image_decoded_val.shape)
    #转换为imshow可显示的shape
	image_decoded_val=np.reshape(image_decoded_val,[217*2, 2*500,3])
	imshow(image_decoded_val)
	plt.show()

crop,将图像进行裁剪或填充

#实现crop
#tf.image.pad_to_bounding_box  填充
#tf.image.crop_to_bounding_box 裁剪
#tf.image.random_crop
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

name='./library.jpg'
image_string=tf.read_file(name)#读取的图片为一个字符串
image_decoded=tf.image.decode_image(image_string)#将字符串解析成图片
image_decoded=tf.reshape(image_decoded,[1,217, 500, 3])#1-batch size
pad_image=tf.image.pad_to_bounding_box(image_decoded,50,100,217*2, 2*500)
#第二三个参数为图像在画布显示的起始点,4,5参数代表画布大小
pad_image=tf.cast(pad_image,tf.uint8)#float->uint8
with tf.Session() as sess:
	image_decoded_val=sess.run(pad_image)
	print(image_decoded_val.shape)
	image_decoded_val=np.reshape(image_decoded_val,[217*2, 2*500,3])
	imshow(image_decoded_val)
	plt.show()

显示效果:

flip实现图像的翻转

#实现flip
#tf.image.flip_up_down
#tf.image.flip_left_right 
#tf.image.random_flip_up_down
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

name='./library.jpg'
image_string=tf.read_file(name)#读取的图片为一个字符串
image_decoded=tf.image.decode_image(image_string)#将字符串解析成图片
#image_decoded=tf.reshape(image_decoded,[1,217, 500, 3])#1-batch size
flip_image=tf.image.flip_up_down(image_decoded)
#pad_image=tf.cast(pad_image,tf.uint8)#float->uint8
with tf.Session() as sess:
	image_decoded_val=sess.run(flip_image)
	print(image_decoded_val.shape)
	#image_decoded_val=np.reshape(image_decoded_val,[217*2, 2*500,3])
	imshow(image_decoded_val)
	plt.show()

显示效果:

brightness&constrast对比度和亮度

#实现brightness
#tf.image.adjust_brightness
#tf.image.adjust_constrast
#tf.image.random_brightness
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

name='./library.jpg'
image_string=tf.read_file(name)#读取的图片为一个字符串
image_decoded=tf.image.decode_image(image_string)#将字符串解析成图片
image_decoded=tf.reshape(image_decoded,[1,217, 500, 3])#1-batch size
bright_image=tf.image.adjust_brightness(image_decoded,-0.5)#原来brightness的50%
#pad_image=tf.cast(pad_image,tf.uint8)#float->uint8
with tf.Session() as sess:
	image_decoded_val=sess.run(bright_image)
	print(image_decoded_val.shape)
	image_decoded_val=np.reshape(image_decoded_val,[217, 500,3])
	imshow(image_decoded_val)
	plt.show()

显示效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值