tensorflow 图像处理

在《TensorFlow实战Google深度学习框架》中,关于图像处理的程序因为版本变更,运行时会报错,现在按照最新的函数方式调试通过,记录下来备查:


# -*- coding: utf-8 -*-
"""
Created on Tue Dec 26 11:27:16 2017


@author: Administrator
"""


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os


PIC_DIR = './datasets'
PIC_FILE= 'cat.jpg'


'''
print(os.getcwd())
os.chdir(os.getcwd()+"\\datasets")
print(os.getcwd())
'''


image_raw_data = tf.gfile.FastGFile(os.path.join(PIC_DIR,PIC_FILE),'rb').read()


with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    
    # 输出解码之后的三维矩阵。
    # 解码之后的结果是一个张量,使用它的取值前需要明确调用运行的过程
    print(img_data.eval())
    img_data.set_shape([1797,2673,3])
    print(img_data.get_shape())


with tf.Session() as sess:
    plt.imshow(img_data.eval())
    plt.show()
    
    # 重新编码保存为 jpg 图像:
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.FastGFile(os.path.join(PIC_DIR,'catsave.jpg'),'wb') as f:
        f.write(encoded_image.eval())
    
'''
# 重新调整图片大小
with tf.Session() as sess:
    # method:图像调整算法:取值 0 - 3
    # 0: 双线性插值法
    # 1: 最近邻居法
    # 2: 双三次插值法
    # 3: 面积插值法
    # 注意:这里:600是 y 坐标,300是 x 坐标
    resized = tf.image.resize_images(img_data,[600,300],method=0)
    print(img_data.get_shape())
    
    # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片
    print('Digital type: ',resized.dtype)
    
    cat = np.asarray(resized.eval(),dtype='uint8')
    # tf.image.convert_image_dtype(img_data,dtype=tf.float32)
    plt.imshow(cat)
    plt.show()
    
# 裁剪和填充图片
with tf.Session() as sess:
    croped = tf.image.resize_image_with_crop_or_pad(img_data,1000,1000)
    padded = tf.image.resize_image_with_crop_or_pad(img_data,3000,3000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()
    
# 截取中间50%的图片¶
with tf.Session() as sess:
    central_cropped = tf.image.central_crop(img_data,0.5)
    plt.imshow(central_cropped.eval())
    plt.show()
    
#  翻转图片
with tf.Session() as sess:
    # up and down turnover:
    flipped1 = tf.image.flip_up_down(img_data)
    
    # left right turnover:
    flipped2 = tf.image.flip_left_right(img_data)
    
    # transpose turnover:
    transposed = tf.image.transpose_image(img_data)
    
    plt.imshow(flipped1.eval())
    plt.show()
    
    plt.imshow(flipped2.eval())
    plt.show()
    
    plt.imshow(transposed.eval())
    plt.show()
    
    # 以一定概率上下翻转图片。
    flipped3 = tf.image.random_flip_up_down(img_data)
    plt.imshow(flipped3.eval())
    plt.show()
    
    # 以一定概率左右翻转图片。
    flipped4 = tf.image.random_flip_left_right(img_data)
    plt.imshow(flipped4.eval())
    plt.show()
    
# 图片色彩调整
with tf.Session() as sess:
    
    adjusted1 = tf.image.adjust_brightness(img_data,-0.5)
    
    adjusted2 = tf.image.adjust_brightness(img_data,0.5)
    
    adjusted3 = tf.image.random_brightness(img_data, max_delta=0.5)
    
    adjusted4 = tf.image.adjust_contrast(img_data,-5)
    
    adjusted5 = tf.image.adjust_contrast(img_data,5)


    adjusted6 = tf.image.random_contrast(img_data,1,8)


    plt.imshow(adjusted6.eval())
    plt.show()    
    
# 添加色相和饱和度
with tf.Session() as sess:
    hue_adjusted1 = tf.image.adjust_hue(img_data,0.1) 
    hue_adjusted2 = tf.image.adjust_hue(img_data, 0.3)
    hue_adjusted3 = tf.image.adjust_hue(img_data, 0.6)
    hue_adjusted4 = tf.image.adjust_hue(img_data, 0.9)
    
    # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
    hue_adjusted5 = tf.image.random_hue(img_data, 0.5) 
    
    # 将图片的饱和度-5
    sat_adjusted1 = tf.image.adjust_saturation(img_data,-5)
    sat_adjusted2 = tf.image.adjust_saturation(img_data,-5)   
    
    # 在[lower, upper]的范围随机调整图的饱和度。
    sat_adjusted3 = tf.image.random_saturation(img_data, 1, 3)
    
    # 图像标准化过程:将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
    std_adjusted = tf.image.per_image_standardization(img_data)


    plt.imshow(std_adjusted.eval())
    plt.show()   
'''




# 添加标注框并裁减
with tf.Session() as sess:
    # 4个数字分别代表:[ymin,xmin,ymax,xmax]
    # 这里有2组数据,代表有2个标注框
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    
    img_data2 = tf.image.resize_images(img_data,[180,267],method=1)
    print(type(img_data2),' ** ', img_data2.shape, ' ** ', img_data2.dtype)
    print(img_data.get_shape())
    
    plt.imshow(img_data2.eval())
    plt.show()    
    
    batched = tf.expand_dims(tf.image.convert_image_dtype(img_data2,tf.float32),0)
    image_with_box = tf.image.draw_bounding_boxes(batched,boxes)    
    plt.imshow(image_with_box[0].eval())
    plt.show()




    begin,size,bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data2),bounding_boxes=boxes,min_object_covered=0.1)    
    distorted_image = tf.slice(img_data2,begin,size)
    plt.imshow(distorted_image.eval())
    plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值