实验环境:windows 7,anaconda 3(Python 3.5),tensorflow(gpu/cpu)
函数介绍:
1.tf.image.random_flip_left_right(image,seed=None)为随机翻转函数,注意这里的image有三个维度[height, width, channels]。还有一点就是此函数是在width方向上随机翻转的,有可能结果就是从左往右翻转,从左往左翻转即没有翻转。
2.tf.image.random_brightness(image,max_delta,seed=None)为随机调整亮度函数,实际上是在原图的基础上随机加上一个值(如果加上的是正值则增亮否则增暗),此值取自[-max_delta,max_delta),要求max_delta>=0。
3.tf.image.random_contrast(image,lower,upper,seed=None)为随机调整对比度函数,对比度调整值取自[lower,upper],要求0
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
image = img.imread('D:/Documents/Pictures/logo4.jpg')
flipped_image = tf.image.random_flip_left_right(image)#随机翻转函数
fig = plt.figure()
fig1 = plt.figure()
ax = fig.add_subplot(111)
ax1 = fig1.add_subplot(111)
ax.set_title('original image')
ax1.set_title('flipped image')
ax.imshow(image)
ax1.imshow(sess.run(tf.cast(flipped_image,tf.uint8)))
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
实验结果:
第一次
第二次
调整亮度实验代码:
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
image = img.imread('D:/Documents/Pictures/logo1.jpg')
imaged = tf.cast(image,tf.float32) #这句必须加上
brightness_image = tf.image.random_brightness(imaged,max_delta=63)#随机调整亮度函数
fig = plt.figure()
fig1 = plt.figure()
ax = fig.add_subplot(111)
ax1 = fig1.add_subplot(111)
ax.set_title('original image')
ax1.set_title('brightness image')
ax.imshow(image)
ax1.imshow(sess.run(tf.cast(brightness_image,tf.uint8)))
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
实验结果:
调整对比度实验代码:
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
image = img.imread('D:/Documents/Pictures/logo6.jpg')
imaged = tf.cast(image,tf.float32)
contrast_image = tf.image.random_contrast(imaged,lower=0.2,upper=1.8)#随机调整对比度函数
fig = plt.figure()
fig1 = plt.figure()
ax = fig.add_subplot(111)
ax1 = fig1.add_subplot(111)
ax.set_title('original image')
ax1.set_title('contrast image')
ax.imshow(image)
ax1.imshow(sess.run(tf.cast(contrast_image,tf.uint8)))
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
实验结果: