tensorflow--图像数据处理

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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值