4-CNN-demo-0402-基于TF的图像预处理相关的API

1.第一版

# -- encoding:utf-8 --


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


# 打印numpy的数组对象的时候,中间不省略
np.set_printoptions(threshold=np.inf)


def show_image(image):
    shape = np.shape(image)
    if len(shape) == 3 and shape[2] == 1:
        # 黑白图像
        plt.imshow(image[:, :, 0], cmap='gray')
        plt.show()
    elif len(shape) == 3:
        # 彩色图像
        plt.imshow(image)
        plt.show()


# 1. 启动一个交互式的会话
sess = tf.InteractiveSession()

# todo 2. 读取图像数据
image_path = "./images/xiaoren.png"
# image_path = "./gray.png"
# image_path = "./black_white.jpg"
# image_path = "./images/timg.gif"

"""
def read_file(filename, name=None):
  filename:给定一个待读取文件的路径
"""

file_contents = tf.read_file(image_path)
#print(file_contents.eval())
print('todo2-**' * 40)

# todo 将数据转换为图像数据
"""
def decode_image(contents, channels=None, name=None):
    将图像数据转换为像素点的数据格式,返回对象为: [height, width, num_channels], 
                 如果是gif的图像返回[num_frames, height, width, num_channels]
        height: 图片的高度的像素大小
        width: 图片的水平宽度的像素大小
        num_channels: 图像的通道数,也就是API中的channels的值
        num_frames: 因为gif的图像是一个动态图像,可以将每一个动的画面看成一个静态图像,num_frames相当于在这个gif图像中有多少个静态图像
    一、contents: 给定具体的数据对象
    二、参数channels:可选值:0 1 3 4,默认为0, 一般使用0 1 3,不建议使用4
        0:使用图像的默认通道,也就是图像是几通道的就使用几通道
        1:使用灰度级别的图像数据作为返回值(只有一个通道:黑白)
        3:使用RGB三通道读取数据
        4:使用RGBA四通道读取数据(R:红色,G:绿色,B:蓝色,A:透明度)
"""
image_tensor = tf.image.decode_image(contents=file_contents, channels=3)
#show_image(image_tensor.eval())

image_tensor = tf.image.decode_png(contents=file_contents, channels=3, dtype=tf.uint8)
print('原始数据shape is:{}'.format(image_tensor.eval().shape))
#show_image(image_tensor.eval())

image_tensor = tf.image.decode_gif(contents=file_contents)
#print(image_tensor)
#print(image_tensor.eval())

print("原始数据形状:{}".format(np.shape(image_tensor.eval())))
#show_image(image_tensor.eval()[6])


# todo 3. 图像大小的缩放
"""
def resize_images(images,
                  size,
                  method=ResizeMethod.BILINEAR,
                  align_corners=False):
    重置大小,放大或者缩小
        images: 给定需要进行大小重置的tensor对象,shape要求为: [batch_size, height, width, channel] 或者 [height, width, channel]; 表示可以一次对很多图像做大小重置,也可以仅仅对一个图像做一个大小重置操作;
        size:给定一个二元组,也就是(new_height, new_width)
        method: 做一个放大和缩小的时候,采用什么算法放大缩小(如何产生新的像素点的值)
            class ResizeMethod(object):
              BILINEAR = 0 # 默认值,二次线性插值
              NEAREST_NEIGHBOR = 1 # 使用邻居的像素值作为新的像素值
              BICUBIC = 2 # 三次插值,一般建议使用BICUBIC,但是运行速度比较慢。
              AREA = 3 # 使用一个区域的所有颜色的均值作为新的像素值
    返回的数据类型和输入的images的数据shape格式一致
"""
resize_image_tensor = tf.image.resize_images(
    images=image_tensor, size=(128, 80),
    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

float_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
resize_image_tensor = tf.image.resize_images(
    images=float_image_tensor, size=(128, 80), method=3)
# print(resize_image_tensor)
print("todo3-rize后的数据形状:{}".format(resize_image_tensor.eval().shape))
show_image(resize_image_tensor.eval())


# todo 4. 图像的剪切和填充
# 图像剪切+填充+大小重置,如果给定大小小于原始图像的大小,那么进行剪切操作,如果给定的大小大于原始图像的大小,那么进行填充操作
"""
def resize_image_with_crop_or_pad(image, target_height, target_width):
    image:需要进行操作的图像tensor对象
    target_height, target_width: 新图像的高度和宽度
做填充和剪切的时候,是从中心位置开始计算
"""
crop_or_pad_image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor,
                                                                  target_height=800,
                                                                  target_width=200)
print("4-crop后的数据形状:{}".format(np.shape(crop_or_pad_image_tensor.eval())))
show_image(crop_or_pad_image_tensor.eval())


#从中心位置等比例的剪切
central_crop_image_tensor = tf.image.central_crop(image_tensor, central_fraction=0.6)
print("4-central_crop后的数据形状:{}".format(np.shape(central_crop_image_tensor.eval())))
show_image(central_crop_image_tensor.eval())


# 基于给定的坐标进行数据的剪切
"""
def crop_to_bounding_box(image, offset_height, offset_width, target_height,
                         target_width):
        offset_height:给定从高度那个位置进行剪切,其实给定的是剪切的左上角的像素下标
        offset_width: 给定从宽度那个维度进行剪切,其实给定的是剪切的左上角的像素下标
"""
crop_to_bounding_box_image_tensor = tf.image.crop_to_bounding_box(
    image_tensor, 100, 20, 500, 490
)
print("4-crop_to_bounding后数据形状:{}".format(np.shape(crop_to_bounding_box_image_tensor.eval())))
show_image(crop_to_bounding_box_image_tensor.eval())


# 给定位置进行数据的填充
"""
def pad_to_bounding_box(image, offset_height, offset_width, target_height,
                        target_width):
"""
# pad_to_bounding_box_image_tensor = tf.image.pad_to_bounding_box(
#     image_tensor, 200, 100, 1000, 1000
# )
# print("pad_to_bounding_box数据形状:{}".format(np.shape(pad_to_bounding_box_image_tensor.eval())))
# show_image(pad_to_bounding_box_image_tensor.eval())


# todo 5. 旋转
# 上下交换
flip_up_down_image_tensor = tf.image.flip_up_down(image_tensor)
print("5上下交换-flip_up_down后形状:{}".format(np.shape(flip_up_down_image_tensor.eval())))
show_image(flip_up_down_image_tensor.eval())


# 左右交换
flip_left_right_image_tensor = tf.image.flip_left_right(image_tensor)
print("5左右交换-新的数据形状:{}".format(np.shape(flip_left_right_image_tensor.eval())))
show_image(flip_left_right_image_tensor.eval())


# 转置(行,列互换)
transpose_image_tensor = tf.image.transpose_image(image_tensor)
print("5转置(行,列互换)transpose_image后形状:{}".format(np.shape(transpose_image_tensor.eval())))
show_image(transpose_image_tensor.eval())


# 旋转(90、180、270、360)
random_int = np.random.randint(low=0, high=3)
rot90_image_tensor = tf.image.rot90(image_tensor, k=random_int)
print("5旋转-新的数据形状:{}".format(np.shape(rot90_image_tensor.eval())))
show_image(rot90_image_tensor.eval())


# todo 6. 颜色空间的转换
# NOTE: 如果要进行颜色空间的转换,那么必须将Tensor对象中的数据类型转换为float类型
# NOTE: 对于图像像素点的表示来讲,可以使用0~255的uint8类型的数值表示,也可以使用0~1之间的float类型的数据表示
# print(image_tensor.eval())
float_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
# print(float_image_tensor.eval())

# RGB -> Gray
gray_image_tensor = tf.image.rgb_to_grayscale(float_image_tensor)
print("6-新的数据形状:{}".format(np.shape(gray_image_tensor.eval())))
show_image(gray_image_tensor.eval())


# RGB -> HSV(RGB: 颜色是由三原色构成的,也就是R红色、G绿色、B蓝色;HSV:描述的是颜色的色彩信息,H:图像的色彩、色度,S表示的图像的饱和度;V表示亮度)
# 注意:必须使用float数据类型的图片,若用uint8的会报如下错误:
#    TypeError: Value passed to parameter 'images' has DataType uint8 not in list of allowed values: float32, float64
hsv_image_tensor = tf.image.rgb_to_hsv(float_image_tensor)
print("6-新的数据形状:{}".format(np.shape(hsv_image_tensor.eval())))
# # hsv的图像展示不是特别好...
show_image(hsv_image_tensor.eval())

# hsv -> rgb
rgb_image_tensor = tf.image.hsv_to_rgb(hsv_image_tensor)
print("6-新的数据形状:{}".format(np.shape(rgb_image_tensor.eval())))
show_image(rgb_image_tensor.eval())


# todo gray -> rgb 注意:只是通道增加了,并不能转为彩色
rgb_image_tensor = tf.image.grayscale_to_rgb(gray_image_tensor)
print("6-新的数据形状:{}".format(np.shape(rgb_image_tensor.eval())))
show_image(rgb_image_tensor.eval())


# todo 灰度图作用:可以从颜色空间中提取图像的轮廓信息(图像的二值化)
# 图像的二值化
a = gray_image_tensor
b = tf.less_equal(a, 0.9)

# 0就是黑,1就是白
"""
def where(condition, x=None, y=None, name=None):
      condition: 给定一个bool数据组成的tensor对象
      x:当condition中的值为true的时候,返回的值
      y:当condition中的值为false的时候,返回的值
      NOTE: 要求condition、x、y的数据形状是一致的
"""
# 对于a中所有大于0.9的像素,设置为0,小于等于0.9的像素值设置为原始值
c = tf.where(condition=b, x=a, y=a - a)   # todo 注意y 这里只能用 a-a ,而不能直接用0,因为是一个矩阵
# show_image(c.eval())


# 对于a中所有小于等于0的像素,设置为1,大于0.9的像素设置为c的值
d = tf.where(condition=b, x=tf.ones_like(c), y=c)
print("6-新的数据形状:{}".format(np.shape(d.eval())))
show_image(d.eval())


# todo 7. 图像的调整
# 亮度调整
"""
def adjust_brightness(image, delta):
  image: 需要调整的图像tensor对象
  delta:调整的参数值,取值范围:(-1,1); 该值表示亮度增加或者减少的值。
底层是将image转换为hsv格式的数据,然后再进行处理。# rgb -> hsv -> h,s,v+delta -> rgb
"""
adjust_brightness_image_tensor = tf.image.adjust_brightness(image_tensor, delta=-0.5)
print("7亮度调整-新的数据形状:{}".format(np.shape(adjust_brightness_image_tensor.eval())))
show_image(adjust_brightness_image_tensor.eval())


# 色调调整
# delta: 调整的参数值,取值范围:(-1,1); 该值表示色调增加或者减少的值。
adjust_hue_image_tensor = tf.image.adjust_hue(image_tensor, delta=-0.8)
# rgb -> hsv -> h+delta,s,v -> rgb
print("7色调调整-新的数据形状:{}".format(np.shape(adjust_hue_image_tensor.eval())))
show_image(adjust_hue_image_tensor.eval())


# 饱和度调整
#saturation_factor: 饱和度系数值
# rgb -> hsv -> h,s*saturation_factor,v -> rgb
adjust_saturation_image_tensor = tf.image.adjust_saturation(image_tensor, saturation_factor=20)
print("7饱和度调整-新的数据形状:{}".format(np.shape(adjust_saturation_image_tensor.eval())))
show_image(adjust_saturation_image_tensor.eval())


# 对比度调整(在每一个通道上,让通道上的像素值调整)
# 底层计算:(x-mean) * contrast_factor + mean
adjust_contrast_image_tensor = tf.image.adjust_contrast(image_tensor, contrast_factor=100)
print("7对比度调整-新的数据形状:{}".format(np.shape(adjust_contrast_image_tensor.eval())))
show_image(adjust_contrast_image_tensor.eval())


# 图像的校验(要求输出的图像必须是浮点型的)
# 用途:检出图像信号中的深色部分和浅色部分,并使两者比例增大,从而提高图像的对比度
adjust_gamma_image_tensor = tf.image.adjust_gamma(float_image_tensor, gamma=100)
print("7图像的校验-新的数据形状:{}".format(np.shape(adjust_gamma_image_tensor.eval())))
show_image(adjust_gamma_image_tensor.eval())


# 图像的归一化API(只能每次对一张图像做归一化操作)
# per_image_standardization_image_tensor = tf.image.per_image_standardization(image_tensor)
# print("7图像的归一化API-新的数据形状:{}".format(np.shape(per_image_standardization_image_tensor.eval())))
# show_image(per_image_standardization_image_tensor.eval())


# 给图像加一个噪音
noisy_image_tensor = image_tensor + tf.cast(5 * tf.random_normal(shape=[600, 510, 3], mean=0, stddev=0.1), tf.uint8)
print("7给图像加一个噪音-新的数据形状:{}".format(np.shape(noisy_image_tensor.eval())))
show_image(noisy_image_tensor.eval())


# 将上面转换步骤模型图,保存
# writer = tf.summary.FileWriter("./model/test03", sess.graph)
# writer.close()


# todo 图像保存(基于scipy的相关API做图像处理)
from scipy.misc import imsave, imread, imresize, imshow, imfilter, imrotate

file_path = "./images/xiaoren.png"
img = imread(file_path)
img = imresize(img, size=(200, 200))
imsave('小人儿.png', img)
print(type(img))
print(np.shape(img))
#imsave('test.png', noisy_image_tensor.eval())
print('done!!')
D:\Anaconda\python.exe D:/AI20/HJZ/04-深度学习/3-CNN/20191207/04_02基于TF的图像预处理相关的API.py
todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**todo2-**
2019-12-29 12:48:23.672201: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
原始数据shape is:(600, 510, 3)
原始数据形状:(1, 600, 510, 3)
todo3-rize后的数据形状:(1, 128, 80, 3)
4-crop后的数据形状:(1, 800, 200, 3)
4-central_crop后的数据形状:(1, 360, 306, 3)
4-crop_to_bounding后数据形状:(1, 500, 490, 3)
5上下交换-flip_up_down后形状:(1, 600, 510, 3)
5左右交换-新的数据形状:(1, 600, 510, 3)
5转置(行,列互换)transpose_image后形状:(1, 510, 600, 3)
5旋转-新的数据形状:(1, 600, 510, 3)
6-新的数据形状:(1, 600, 510, 1)
6-新的数据形状:(1, 600, 510, 3)
6-新的数据形状:(1, 600, 510, 3)
6-新的数据形状:(1, 600, 510, 3)
6-新的数据形状:(1, 600, 510, 1)
7亮度调整-新的数据形状:(1, 600, 510, 3)
7色调调整-新的数据形状:(1, 600, 510, 3)
7饱和度调整-新的数据形状:(1, 600, 510, 3)
7对比度调整-新的数据形状:(1, 600, 510, 3)
WARNING:tensorflow:tf.op_scope(values, name, default_name) is deprecated, use tf.name_scope(name, default_name, values)
7图像的校验-新的数据形状:(1, 600, 510, 3)
7给图像加一个噪音-新的数据形状:(1, 600, 510, 3)
<class 'numpy.ndarray'>
(200, 200, 3)
done!!

Process finished with exit code 0

2.第二版

# -- encoding:utf-8 --


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


# 打印numpy的数组对象的时候,中间不省略
np.set_printoptions(threshold=np.inf)


def show_image(image):
    shape = np.shape(image)
    if len(shape) == 3 and shape[2] == 1:
        # 黑白图像
        plt.imshow(image[:, :, 0], cmap='gray')
        plt.show()
    elif len(shape) == 3:
        # 彩色图像
        plt.imshow(image)
        plt.show()


# 1. 启动一个交互式的会话
sess = tf.InteractiveSession()

# todo 2. 读取图像数据
image_path = r"C:\Users\hjz\python-project\project\02_lianxi\02_FaceDetecton/3.jpg"
# image_path = "./gray.png"
# image_path = "./black_white.jpg"
# image_path = "./images/timg.gif"

"""
def read_file(filename, name=None):
  filename:给定一个待读取文件的路径
"""

file_contents = tf.read_file(image_path)
# print(file_contents.eval())
print('**' * 40)

# todo 将数据转换为图像数据
"""
def decode_image(contents, channels=None, name=None):
    将图像数据转换为像素点的数据格式,返回对象为: [height, width, num_channels], 
                 如果是gif的图像返回[num_frames, height, width, num_channels]
        height: 图片的高度的像素大小
        width: 图片的水平宽度的像素大小
        num_channels: 图像的通道数,也就是API中的channels的值
        num_frames: 因为gif的图像是一个动态图像,可以将每一个动的画面看成一个静态图像,num_frames相当于在这个gif图像中有多少个静态图像
    一、contents: 给定具体的数据对象
    二、参数channels:可选值:0 1 3 4,默认为0, 一般使用0 1 3,不建议使用4
        0:使用图像的默认通道,也就是图像是几通道的就使用几通道
        1:使用灰度级别的图像数据作为返回值(只有一个通道:黑白)
        3:使用RGB三通道读取数据
        4:使用RGBA四通道读取数据(R:红色,G:绿色,B:蓝色,A:透明度)
"""
image_tensor = tf.image.decode_image(contents=file_contents, channels=3)
show_image(image_tensor.eval())
image_tensor_0 = tf.image.decode_image(contents=file_contents, channels=3)
image_tensor = tf.image.decode_png(contents=file_contents, channels=3, dtype=tf.uint8)
print('1原始数据shape is:{}'.format(image_tensor.eval().shape))
show_image(image_tensor.eval())

image_tensor = tf.image.decode_gif(contents=file_contents)
# print(image_tensor)
# print(image_tensor.eval())

print("2原始数据形状:{}".format(np.shape(image_tensor.eval())))
# show_image(image_tensor.eval()[6])


# todo 3. 图像大小的缩放
"""
def resize_images(images,
                  size,
                  method=ResizeMethod.BILINEAR,
                  align_corners=False):
    重置大小,放大或者缩小
        images: 给定需要进行大小重置的tensor对象,shape要求为: [batch_size, height, width, channel] 或者 [height, width, channel]; 表示可以一次对很多图像做大小重置,也可以仅仅对一个图像做一个大小重置操作;
        size:给定一个二元组,也就是(new_height, new_width)
        method: 做一个放大和缩小的时候,采用什么算法放大缩小(如何产生新的像素点的值)
            class ResizeMethod(object):
              BILINEAR = 0 # 默认值,二次线性插值
              NEAREST_NEIGHBOR = 1 # 使用邻居的像素值作为新的像素值
              BICUBIC = 2 # 三次插值,一般建议使用BICUBIC,但是运行速度比较慢。
              AREA = 3 # 使用一个区域的所有颜色的均值作为新的像素值
    返回的数据类型和输入的images的数据shape格式一致
"""
resize_image_tensor = tf.image.resize_images(
    images=image_tensor, size=(128, 80),
    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

float_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
resize_image_tensor = tf.image.resize_images(
    images=float_image_tensor, size=(128, 80), method=3)
# print(resize_image_tensor)
print("3 rize后的数据形状:{}".format(resize_image_tensor.eval().shape))
show_image(resize_image_tensor.eval())


# todo 4. 图像的剪切和填充
# 图像剪切+填充+大小重置,如果给定大小小于原始图像的大小,那么进行剪切操作,如果给定的大小大于原始图像的大小,那么进行填充操作
"""
def resize_image_with_crop_or_pad(image, target_height, target_width):
    image:需要进行操作的图像tensor对象
    target_height, target_width: 新图像的高度和宽度
做填充和剪切的时候,是从中心位置开始计算
"""
crop_or_pad_image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor,
                                                                  target_height=800,
                                                                  target_width=200)
print("4 crop后的数据形状:{}".format(np.shape(crop_or_pad_image_tensor.eval())))
show_image(crop_or_pad_image_tensor.eval())


# 从中心位置等比例的剪切
central_crop_image_tensor = tf.image.central_crop(image_tensor, central_fraction=0.6)
print("5 central_crop后的数据形状:{}".format(np.shape(central_crop_image_tensor.eval())))
show_image(central_crop_image_tensor.eval())


# 基于给定的坐标进行数据的剪切
"""
def crop_to_bounding_box(image, offset_height, offset_width, target_height,
                         target_width):
        offset_height:给定从高度那个位置进行剪切,其实给定的是剪切的左上角的像素下标
        offset_width: 给定从宽度那个维度进行剪切,其实给定的是剪切的左上角的像素下标
"""
crop_to_bounding_box_image_tensor = tf.image.crop_to_bounding_box(
    image_tensor, 100, 20, 500, 490
)
print("6 crop_to_bounding后数据形状:{}".format(np.shape(crop_to_bounding_box_image_tensor.eval())))
show_image(crop_to_bounding_box_image_tensor.eval())


# 给定位置进行数据的填充
"""
def pad_to_bounding_box(image, offset_height, offset_width, target_height,
                        target_width):
"""
pad_to_bounding_box_image_tensor = tf.image.pad_to_bounding_box(
    crop_to_bounding_box_image_tensor, 200, 100, 1000, 1000
)
print("7 pad_to_bounding_box数据形状:{}".format(np.shape(pad_to_bounding_box_image_tensor.eval())))
show_image(pad_to_bounding_box_image_tensor.eval())


# todo 5. 旋转
# 上下交换
flip_up_down_image_tensor = tf.image.flip_up_down(image_tensor)
print("8-flip_up_down后形状:{}".format(np.shape(flip_up_down_image_tensor.eval())))
show_image(flip_up_down_image_tensor.eval())


# 左右交换
flip_left_right_image_tensor = tf.image.flip_left_right(image_tensor)
print("9-左右交换新的数据形状:{}".format(np.shape(flip_left_right_image_tensor.eval())))
show_image(flip_left_right_image_tensor.eval())


# 转置(行,列互换)
transpose_image_tensor = tf.image.transpose_image(image_tensor)
print("10 transpose_image后形状:{}".format(np.shape(transpose_image_tensor.eval())))
show_image(transpose_image_tensor.eval())


# 旋转(90、180、270、360)
random_int = np.random.randint(low=0, high=3)
rot90_image_tensor = tf.image.rot90(image_tensor, k=random_int)
print("11 旋转后新的数据形状:{}".format(np.shape(rot90_image_tensor.eval())))
show_image(rot90_image_tensor.eval())


# todo 6. 颜色空间的转换
# NOTE: 如果要进行颜色空间的转换,那么必须将Tensor对象中的数据类型转换为float类型
# NOTE: 对于图像像素点的表示来讲,可以使用0~255的uint8类型的数值表示,也可以使用0~1之间的float类型的数据表示
# print(image_tensor.eval())
float_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
# print(float_image_tensor.eval())

# RGB -> Gray
gray_image_tensor = tf.image.rgb_to_grayscale(float_image_tensor)
print("12 RGB -> Gray新的数据形状:{}".format(np.shape(gray_image_tensor.eval())))
show_image(gray_image_tensor.eval())


# RGB -> HSV(RGB: 颜色是由三原色构成的,也就是R红色、G绿色、B蓝色;HSV:描述的是颜色的色彩信息,H:图像的色彩、色度,S表示的图像的饱和度;V表示亮度)
# 注意:必须使用float数据类型的图片,若用uint8的会报如下错误:
#    TypeError: Value passed to parameter 'images' has DataType uint8 not in list of allowed values: float32, float64
hsv_image_tensor = tf.image.rgb_to_hsv(float_image_tensor)
print("13 RGB -> HSV新的数据形状:{}".format(np.shape(hsv_image_tensor.eval())))
# hsv的图像展示不是特别好...
show_image(hsv_image_tensor.eval())

# hsv -> rgb
rgb_image_tensor = tf.image.hsv_to_rgb(hsv_image_tensor)
print("14 hsv -> rgb新的数据形状:{}".format(np.shape(rgb_image_tensor.eval())))
show_image(rgb_image_tensor.eval())


# todo gray -> rgb 注意:只是通道增加了,并不能转为彩色
rgb_image_tensor = tf.image.grayscale_to_rgb(gray_image_tensor)
print("16 gray -> rgb新的数据形状:{}".format(np.shape(rgb_image_tensor.eval())))
show_image(rgb_image_tensor.eval())


# todo 灰度图作用:可以从颜色空间中提取图像的轮廓信息(图像的二值化)
# 图像的二值化
a = gray_image_tensor
b = tf.less_equal(a, 0.9)

# 0就是黑,1就是白
"""
def where(condition, x=None, y=None, name=None):
      condition: 给定一个bool数据组成的tensor对象
      x:当condition中的值为true的时候,返回的值
      y:当condition中的值为false的时候,返回的值
      NOTE: 要求condition、x、y的数据形状是一致的
"""
# 对于a中所有大于0.9的像素,设置为0,小于等于0.9的像素值设置为原始值
c = tf.where(condition=b, x=a, y=a - a)   # todo 注意y 这里只能用 a-a ,而不能直接用0,因为是一个矩阵
# show_image(c.eval())


# 对于a中所有小于等于0的像素,设置为1,大于0.9的像素设置为c的值
# d = tf.where(condition=b, x=tf.ones_like(c), y=c)
# print("新的数据形状:{}".format(np.shape(d.eval())))
# show_image(d.eval())


# todo 7. 图像的调整
# 亮度调整
"""
def adjust_brightness(image, delta):
  image: 需要调整的图像tensor对象
  delta:调整的参数值,取值范围:(-1,1); 该值表示亮度增加或者减少的值。
底层是将image转换为hsv格式的数据,然后再进行处理。# rgb -> hsv -> h,s,v+delta -> rgb
"""
adjust_brightness_image_tensor = tf.image.adjust_brightness(image_tensor, delta=-0.5)
print("17 亮度调整新的数据形状:{}".format(np.shape(adjust_brightness_image_tensor.eval())))
show_image(adjust_brightness_image_tensor.eval())


# 色调调整
# delta: 调整的参数值,取值范围:(-1,1); 该值表示色调增加或者减少的值。
adjust_hue_image_tensor = tf.image.adjust_hue(image_tensor, delta=-0.8)
# rgb -> hsv -> h+delta,s,v -> rgb
print("18 色调调整新的数据形状:{}".format(np.shape(adjust_hue_image_tensor.eval())))
show_image(adjust_hue_image_tensor.eval())


# 饱和度调整
# saturation_factor: 饱和度系数值
# rgb -> hsv -> h,s*saturation_factor,v -> rgb
adjust_saturation_image_tensor = tf.image.adjust_saturation(image_tensor, saturation_factor=20)
print("19 饱和度调整新的数据形状:{}".format(np.shape(adjust_saturation_image_tensor.eval())))
show_image(adjust_saturation_image_tensor.eval())


# 对比度调整(在每一个通道上,让通道上的像素值调整)
# 底层计算:(x-mean) * contrast_factor + mean
adjust_contrast_image_tensor = tf.image.adjust_contrast(image_tensor, contrast_factor=100)
print("20 对比度调整新的数据形状:{}".format(np.shape(adjust_contrast_image_tensor.eval())))
show_image(adjust_contrast_image_tensor.eval())


# 图像的校验(要求输出的图像必须是浮点型的)
# 用途:检出图像信号中的深色部分和浅色部分,并使两者比例增大,从而提高图像的对比度
adjust_gamma_image_tensor = tf.image.adjust_gamma(float_image_tensor, gamma=100)
print("21 图像的校验新的数据形状:{}".format(np.shape(adjust_gamma_image_tensor.eval())))
show_image(adjust_gamma_image_tensor.eval())


# 图像的归一化API(只能每次对一张图像做归一化操作)
per_image_standardization_image_tensor = tf.image.per_image_standardization(image_tensor_0)
print("22 图像的归一化新的数据形状:{}".format(np.shape(per_image_standardization_image_tensor.eval())))
show_image(per_image_standardization_image_tensor.eval())


# 给图像加一个噪音
# noisy_image_tensor = image_tensor_0 + tf.cast(5 * tf.random_normal(shape=[600, 510, 3], mean=0, stddev=0.1), tf.uint8)
noisy_image_tensor = image_tensor_0 + tf.cast(5 * tf.random_normal(shape=[1820,1024,3], mean=0, stddev=0.1), tf.uint8)
print("23 给图像加一个噪音新的数据形状:{}".format(np.shape(noisy_image_tensor.eval())))
show_image(noisy_image_tensor.eval())


# 将上面转换步骤模型图,保存
writer = tf.summary.FileWriter("./models/test", sess.graph)
writer.close()


# todo 图像保存(基于scipy的相关API做图像处理)
from scipy.misc import imsave, imread, imresize, imshow, imfilter, imrotate

file_path = r"C:\Users\hjz\python-project\project\02_lianxi\02_FaceDetecton/3.jpg"
img = imread(file_path)
img = imresize(img, size=(200, 200))
imsave('小人儿.png', img)
print(type(img))
print(np.shape(img))
imsave('test.png', noisy_image_tensor.eval())
print('done!!')
C:\ProgramData\Anaconda2\envs\py36\python.exe C:/Users/hjz/Work/AI/03_DL/02_demo/01_CNN/04_02基于TF的图像预处理相关的API.py
C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
2020-08-03 14:31:16.040467: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
********************************************************************************
1原始数据shape is:(1820, 1024, 3)
2原始数据形状:(1, 1820, 1024, 3)
3 rize后的数据形状:(1, 128, 80, 3)
4 crop后的数据形状:(1, 800, 200, 3)
5 central_crop后的数据形状:(1, 1092, 616, 3)
6 crop_to_bounding后数据形状:(1, 500, 490, 3)
7 pad_to_bounding_box数据形状:(1, 1000, 1000, 3)
8-flip_up_down后形状:(1, 1820, 1024, 3)
9-左右交换新的数据形状:(1, 1820, 1024, 3)
10 transpose_image后形状:(1, 1024, 1820, 3)
11 旋转后新的数据形状:(1, 1820, 1024, 3)
12 RGB -> Gray新的数据形状:(1, 1820, 1024, 1)
13 RGB -> HSV新的数据形状:(1, 1820, 1024, 3)
14 hsv -> rgb新的数据形状:(1, 1820, 1024, 3)
16 gray -> rgb新的数据形状:(1, 1820, 1024, 3)
17 亮度调整新的数据形状:(1, 1820, 1024, 3)
18 色调调整新的数据形状:(1, 1820, 1024, 3)
19 饱和度调整新的数据形状:(1, 1820, 1024, 3)
20 对比度调整新的数据形状:(1, 1820, 1024, 3)
WARNING:tensorflow:tf.op_scope(values, name, default_name) is deprecated, use tf.name_scope(name, default_name, values)
21 图像的校验新的数据形状:(1, 1820, 1024, 3)
22 图像的归一化新的数据形状:(1820, 1024, 3)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
23 给图像加一个噪音新的数据形状:(1820, 1024, 3)
<class 'numpy.ndarray'>
(200, 200, 3)
done!!

Process finished with exit code 0

2.1原始图片:

在这里插入图片描述

2.2 归一化图片:

在这里插入图片描述

2.3 加噪音图片:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值