文章目录

  • 1.第一版
  • 2.第二版
    • 2.1原始图片:
    • 2.2 归一化图片:
    • 2.3 加噪音图片:

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,不建议使用40:使用图像的默认通道,也就是图像是几通道的就使用几通道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, imrotatefile_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,不建议使用40:使用图像的默认通道,也就是图像是几通道的就使用几通道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, imrotatefile_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 加噪音图片:

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

  1. 毕设模块之一 —— 基于PyQt5+openCV图像预处理软件系统设计

    一.要求 完成图像采集系统的设计优化,并能进行简单的图像预处理,包括降噪.轮廓提取.二值化,能为后期图像配准和融合提供良好的工作基础. 二.设计思路&实现过程 刚开始,我是想先写个简单的UI界 ...

  2. 深度学习(二十一)基于FCN的图像语义分割-CVPR 2015-未完待续

    CNN应用之基于FCN的图像语义分割 原文地址:http://blog.csdn.net/hjimce/article/details/50268555 作者:hjimce 一.相关理论     本篇 ...

  3. DL之CycleGAN:基于TF利用CycleGAN模型对apple2orange数据集实现图像转换—训练测试过程全记录

    DL之CycleGAN:基于TF利用CycleGAN模型对apple2orange数据集实现图像转换-训练&测试过程全记录 目录 apple2orange数据集 输出结果 训练&测试过 ...

  4. TF之p2p:基于TF利用p2p模型部分代码实现提高图像的分辨率

    TF之p2p:基于TF利用p2p模型部分代码实现提高图像的分辨率 目录 一.tfimage.py文件功能解释 二.process.py添加一个新操作 一.tfimage.py文件功能解释 1.此处的c ...

  5. CV之FE:基于TF进行FE——去除异常(被损坏)图像 和单通道图像

    CV之FE:基于TF进行FE--去除异常(被损坏)图像 和单通道图像 目录 输出结果 设计思路 部分代码实现 输出结果 去除了异常(被损坏)图像 .单通道图像 设计思路 1. 部分代码实现 impor ...

  6. CV之IE之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成不同尺寸和质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)—五个架构设计思维导图

    CV之IE之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成不同尺寸和质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)-五个架构设计思维导图 ...

  7. CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成带背景的不同尺寸高质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例

    CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成带背景的不同尺寸高质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例 目录 基于 ...

  8. CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成更高质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例应用

    CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成更高质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例应用 目录 基于TF框架利 ...

  9. CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成更大尺寸的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例应用

    CV之IG之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成更大尺寸的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)案例应用 目录 基于TF框架利 ...

最新文章

  1. go micro安装:go get github.com/micro/micro问题
  2. convert-----Linux中的图片处理工具
  3. 360浏览器卸载_如何卸载360浏览器,如何卸载360安全浏览器
  4. c语言中常用的程序,C语言一些常用语句
  5. 网页连接不上java服务端,用Java插入IP时无法连接到服务器
  6. MFC中对话框的操作
  7. 与 OpenCV 1 同时使用
  8. Tableau Desktop认证:为什么要关心以及如何通过
  9. c语言如何输出一维数组字母,C语言一维数组初步学习笔记
  10. 【C/C++】C++98基础上的C++11新特性
  11. 微软再次强调:爱开发 爱 Linux!
  12. 关于日期单双日,星期判断
  13. 机器学习基石 作业三
  14. 软件工程专业和网络工程专业的区别
  15. Docker技术之容器与外部相连
  16. VOIP 语音视频通话 ---总述
  17. 学Web的第二十一天
  18. 私人 — 在 Apple 商务管理或 Apple 校园教务管理上作为自定 App 提供
  19. Spring MVC源码解析——HandlerMapping(处理器映射器)
  20. Mysql 常用 表操作

热门文章

  1. 建网站要云服务器么?网站服务器怎么选?
  2. excel其中一个页签慢,excel表格12个页签数据汇总-Excel怎么实现多张表格数据自动汇总到一张表上...
  3. Prim算法实现最小生成树
  4. 使用更相减损术求最大公约数
  5. linux 命令行 错位,在Linux下使用Firefox浏览器打开网站显示错位的解决
  6. STM32F7 MPU笔记
  7. vue使用原生video标签播放视频
  8. 台式机更换固态硬盘(一)
  9. python实验心得体会范文大全_实验心得体会四篇
  10. STM32F103出现CPU could not be halted问题的解决方案