import tensorflow as tf
import matplotlib.pyplot as plt
import os
image_path=os.path.join(os.getcwd(),'9.png')
image=tf.gfile.FastGFile(image_path,'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
print(img_after_decode)
plt.imshow(img_after_decode.eval())
plt.show()
encode_image=tf.image.decode_png(img_after_decode)
(1)以一定概率上下翻转图片random_flip_up_down(image,seed)
(2)将图片上下翻转flip_up_down(image)
(3)将图片左右翻转flip_left_right(image)
(4)将图片进行对角线翻转transpose_image(image)
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
'''左右翻转'''
flipped=tf.image.flip_left_right(img_after_decode)
plt.imshow(flipped.eval())
plt.show()
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
adjusted_brightness=tf.image.random_brightness(img_after_decode,max_delta=1)
plt.imshow(adjusted_brightness.eval())
plt.show()
对比度调整
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
adjusted_contract=tf.image.random_contrast(img_after_decode,0.2,18,)
plt.imshow(adjusted_contract.eval())
plt.show()
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20200420095927154.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Flc3Rpb25feXpfMTAwODY=,size_16,color_FFFFFF,t_70)
## 色相调整
```python
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
adjusted_hue=tf.image.adjust_hue(img_after_decode,0.1)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.3)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.6)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.9)
plt.imshow(adjusted_hue.eval())
plt.show()
色相调整
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
adjusted_hue_1=tf.image.adjust_hue(img_after_decode,0.1)
adjusted_hue_2 = tf.image.adjust_hue(img_after_decode, 0.3)
adjusted_hue_3 =tf.image.adjust_hue(img_after_decode, 0.6)
adjusted_hue_4 =tf.image.adjust_hue(img_after_decode, 0.9)
adjusted_hue = [adjusted_hue_1,adjusted_hue_2,adjusted_hue_3,adjusted_hue_4]
for i,img in enumerate(adjusted_hue):
plt.subplot(1,4,i+1)
plt.imshow(img.eval())
plt.show()
调整图像大小
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
'''建材图像'''
croped=tf.image.resize_image_with_crop_or_pad(img_after_decode,300,300)
'''填充图像'''
padded=tf.image.resize_image_with_crop_or_pad(img_after_decode,1000,1000)
img_sz=[croped,padded]
for i,img in enumerate(img_sz):
plt.subplot(1,2,i+1)
plt.imshow(img.eval())
plt.show()
按照比例调整图像大小
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
central_cropped=tf.image.central_crop(img_after_decode,0.4)
plt.imshow(central_cropped.eval())
plt.show()
以图像为中心对图像进行裁剪或者填充
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
croped=tf.image.crop_to_bounding_box(img_after_decode,100,100,300,300)
padded=tf.image.pad_to_bounding_box(img_after_decode,100,100,1000,1000)
img_sz=[croped,padded]
for i,img in enumerate(img_sz):
plt.subplot(1,len(img_sz),i+1)
plt.imshow(img.eval())
plt.show()
图像的标注框
import tensorflow as tf
import matplotlib.pyplot as plt
import os
image=tf.gfile.FastGFile(os.path.join(os.getcwd(),'9.png'),'rb').read()
with tf.Session() as sess:
img_after_decode=tf.image.decode_png(image)
batched=tf.expand_dims(tf.image.convert_image_dtype(img_after_decode,tf.float32),0)
boxes=tf.constant([[[0.05,0.05,0.9,0.7],[0.20,0.3,0.5,0.5]]])
image_boxes=tf.image.draw_bounding_boxes(batched,boxes)
plt.imshow(image_boxes[0].eval())
plt.show()