标签:order 数据类型转化 fast dep float nes 图层 对比度 with
import tensorflow as tf import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", ‘rb‘).read() # 字节 with tf.Session() as session: # 2.图片解码 img = tf.image.decode_jpeg(image_bytes) # print(img) # tensor(‘DecodePnng:0‘, shape=(?,?,?),dtype=uint8) img_array = img.eval() # 将tensor对象转成数组 # 3.图片显示 plt.imshow(img_array) plt.show() # 4.图片数据类型转化(整形) # img = tf.image.convert_image_dtype(img, dtype=tf.float32) # print(img) # 5.图像重编码 encode_image = tf.image.encode_jpeg(img) new_img = encode_image.eval() # 数组 # 6.图片保存 with tf.gfile.GFile("dog_new.png", "wb") as f: f.write(new_img)
import tensorflow as tf import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", ‘rb‘).read() # 字节 with tf.Session() as session: img = tf.image.decode_jpeg(image_bytes) # 翻转图片 img_flipped = tf.image.flip_up_down(img) # 上下反转 img_flipped = tf.image.flip_left_right(img_flipped) # 左右反转 img_flipped = tf.image.transpose_image(img_flipped) # 对角线反转 img_flipped = tf.image.random_flip_up_down(img_flipped) # 随机上下反转 img_flipped = tf.image.random_flip_left_right(img_flipped) # 随机左右反转 # 亮度设置 img_adjust = tf.image.adjust_brightness(img_flipped, -0.5) # 增加亮度 img_adjust = tf.image.adjust_brightness(img_adjust, +0.5) # 降低亮度 img_adjust = tf.image.random_brightness(img_adjust, max_delta=0.3) # 随机调整亮度,亮度在[-max_delta, +max_delta]] # 色度 img_saturation = tf.image.adjust_saturation(img_adjust, 1.5) # 支持random
# 饱和度 img_hue = tf.image.adjust_hue(img_saturation, delta=0.2)
# 对比度 img_contrast = tf.image.adjust_contrast(img_hue, 0.5) # 图片标准化 img_standard = tf.image.per_image_standardization(img_adjust) img_standard = tf.clip_by_value(img_standard, 0.0, 10) # 转成数组 img_array = img_standard.eval() plt.imshow(img_array) plt.show()
import tensorflow as tf import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", ‘rb‘).read() # 字节 with tf.Session() as session: img = tf.image.decode_jpeg(image_bytes) # 调整图片大小 img_resize = tf.image.resize_image_with_crop_or_pad(img, 300, 300) # 按比例截取图片 boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38], [0.38, 0.53, 0.53, 0.71]]]) # 两个标注框 # boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置 # 给原始图片添加一个图层 batched = tf.expand_dims(tf.image.convert_image_dtype(img_resize, tf.float32), 0) # 把boxes标注的框画到原始图片上 image_with_boxes = tf.image.draw_bounding_boxes(batched, boxes) # 重新将原始图片设置为RGB image_with_boxes = tf.reshape(image_with_boxes, [300, 300, 3]) img_array = image_with_boxes.eval() plt.imshow(img_array) plt.show()
import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", ‘rb‘).read() # 字节 with tf.Session() as session: img = tf.image.decode_jpeg(image_bytes) # 给定截取框大小 bounding_boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置 # 选择相关图像截取算法截图 # Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`. begin, size, bboxes = tf.image.sample_distorted_bounding_box( tf.shape(img), bounding_boxes=bounding_boxes, min_object_covered=0.1 ) # 生成概要 # img_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(tf.image.convert_image_dtype(img, dtype=tf.float32), 0), bboxes) # tf.summary.image(‘img_with_box‘, img_with_box) # print(begin.eval(), size.eval()) # 截图 distorted_img = tf.slice(img, begin, size) img_array = distorted_img.eval() plt.imshow(img_array) plt.show()
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt class Sample: def load_jpg(self, path, mode=‘rb‘): image_bytes = tf.gfile.FastGFile(path, mode).read() return tf.image.decode_jpeg(image_bytes, channels=3) def _distort_picture(self, image, color_ordering=0): if color_ordering == 0: image = tf.image.random_brightness(image, max_delta=32./255.) # 随机亮度 image = tf.image.random_contrast(image, lower=0.5, upper=1.5) # 对比度 image = tf.image.random_hue(image, max_delta=0.2) # 饱和度 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度 if color_ordering == 1: image = tf.image.random_hue(image, max_delta=0.2) # 饱和度 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度 image = tf.image.random_flip_left_right(image) image = tf.image.random_flip_up_down(image) return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0) # 归一化 def _preprocess_for_train(self, image, height, width, bounding_boxes=None): if bounding_boxes is None: bounding_boxes = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) if image.dtype != tf.float32: image = tf.image.convert_image_dtype(image, dtype=tf.float32) begin, size, bboxes = tf.image.sample_distorted_bounding_box( tf.shape(image), bounding_boxes=bounding_boxes, min_object_covered=0.1 ) # 随机截图 distorted_image = tf.slice(image, begin=begin, size=size) # 调整随机截图的图片大小 # distorted_image = tf.image.resize_image_with_crop_or_pad(distorted_image, height, width) distorted_image = tf.image.resize_images( distorted_image, size=[height, width], method=np.random.randint(4) ) # 随机调整图片的一些设置 distorted_image = self._distort_picture(distorted_image, np.random.randint(2)) return distorted_image def get_random_picture(self, number, image, *args, **kwargs): with tf.Session() as session: for i in range(number): random_picture = self._preprocess_for_train(image, *args, **kwargs) plt.imshow(random_picture.eval()) plt.show() def main(): sample = Sample() image = sample.load_jpg("dog.jpg", ‘rb‘) # bounding_boxes = tf.constant([0.2, 0.2, 0.8, 0.8], dtype=tf.float32, shape=[1, 1, 4]) bounding_boxes = tf.constant([[[0.2, 0.2, 0.8, 0.8]]]) height = width = 150 sample.get_random_picture(5, image, height, width, bounding_boxes) main()
函数 | 描述 |
tf.gfile.FastGFile | 读取单个图片,返回字节流数据 |
tf.decode_jpeg | 在图片读入操作之后,图片处理之前,对图片进行解码 |
tf.encode_jpeg | 在图片保存时对图片进行重编码 |
tf.gfile.GFile | 写出单个图片 |
tf.image.convert_image_dtype | 转换图片的数据类型 |
tf.resize_images | 剪裁图片大小 |
tf.resize_image_with_crop_of_pad | 剪裁单个图片大小 |
tf.image.random_flip_left_right | 图片随机左右反转 |
tf.image.random_flip_up_down | 图片随机上下反转 |
tf.image.random_brightness | 图片随机调整亮度 |
tf.image.random_hue | 图片随机调整饱和度 |
tf.image.random_contrast | 图片随机调整对比度 |
tf.image.random_saturation | 图片随机调整色度 |
tf.image.per_image_standardization | 单个图片标准化 |
tf.image.clip_by_value | 单个图片归一化,其它还有tf.image.clip_by_XXX等方法 |
tf.expand_dims | 给图片增加维度(图层) |
tf.image.sample_distorted_bounding_box | 生成随机子图 |
tf.image.draw_bounding_boxes | 将标注框标注的子图取出来 |
tf.image.reshape | 调整图片的维度 |
tf.slice | 截取随机子图为单个图片 |
标签:order 数据类型转化 fast dep float nes 图层 对比度 with
原文地址:https://www.cnblogs.com/kuaizifeng/p/9490669.html