__author__ = 'ding' ''' TensorFlow 图像处理 ''' import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile.FastGFile('./path/to/picture1.jpeg', 'rb').read() with tf.Session() as sess: # 将图像以jpeg的格式解码从而得到图像对应的三维矩阵 # tf.image_decode_png 函数对png格式图形进行解码。解码之后得到一个张量 # tf.image_decode_jpeg 函数对jpeg格式图形进行解码。 img_data = tf.image.decode_jpeg(image_raw_data) print(img_data.eval()) img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8) # # 导出 # img_export(img_data) # # # 调整尺寸 # img_size(img_data) # # # 裁剪 # img_fill_cut(img_data) # # # 翻转 # img_transposed(img_data) # # # 色彩调整 # img_color(img_data)
def img_export(img_data): # 将一张图像的三维矩阵重新按jpeg格式编码并存入文件中,可以得到和原始图像一样的图像 encoded_image = tf.image.encode_jpeg(img_data) with tf.gfile.GFile('./path/to/output.jpeg', 'wb') as f: f.write(encoded_image.eval())
def img_size(img_data): # tf.image.resize_images 函数调整图像的大小, # 一个参数为原始图像 # 第二个参数为图像尺寸 # 第三个给出调整图像大小的算法 # method=0 双线性插值法 # method=1 最近邻居法 # method=2 双三次插值法 # method=3 面积插值法 resized_0 = tf.image.resize_images(img_data, (300, 300), method=0) resized_1 = tf.image.resize_images(img_data, (300, 300), method=1) resized_2 = tf.image.resize_images(img_data, (300, 300), method=2) resized_3 = tf.image.resize_images(img_data, (300, 300), method=3) # print(img_data.get_shape()) plt.figure(0) plt.imshow(resized_0.eval()) plt.figure(1) plt.imshow(resized_1.eval()) plt.figure(2) plt.imshow(resized_2.eval()) plt.figure(3) plt.imshow(resized_3.eval()) plt.show()
def img_fill_cut(img_data): # 通过tf.image.resize_image_with_crop_or_pad函数调整图像大小, # 第一个参数为原始图像,第二、三个参数分别表示图像的长高 # 如果原始尺寸大于指定尺寸,则进行裁剪 # 如果原始尺寸小于指定尺寸,则进行填充,默认为0 黑色 corped = tf.image.resize_image_with_crop_or_pad(img_data, 300, 300) padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000) plt.figure(0) plt.imshow(corped.eval()) plt.figure(1) plt.imshow(padded.eval()) # 通过tf.image.central_crop函数按比例裁剪图像 # 第一个参数为原始图像 # 第二个参数为裁剪比例,范围(0,1] central_cropped = tf.image.central_crop(img_data, 0.5) plt.figure(3) plt.imshow(central_cropped.eval()) plt.show()
def img_transposed(img_data): # 上下翻转 flipped_up_down = tf.image.flip_up_down(img_data) # 左右翻转 flipeed_left_right = tf.image.flip_left_right(img_data) # 对角线翻转 transposed = tf.image.transpose_image(img_data) plt.figure(0) plt.imshow(flipped_up_down.eval()) plt.figure(1) plt.imshow(flipeed_left_right.eval()) plt.figure(2) plt.imshow(transposed.eval()) plt.show() # 以一定概率上下翻转 flipped = tf.image.random_flip_up_down(img_data) # 以一定概率左右翻转 flipped_left_right = tf.image.random_flip_left_right(img_data) 这样的用法是,随机翻转训练图像,让模型可以识别不同角度的实体
def img_color(img_data): def img_color(img_data): # 亮度 adjusted_brightness_sub = tf.image.adjust_brightness(img_data, -0.5) adjusted_brightness_add = tf.image.adjust_brightness(img_data, 0.5) adjusted_brightness_random = tf.image.random_brightness(img_data, max_delta=1) # 对比度 adjusted_contrast_sub = tf.image.adjust_contrast(img_data, -0.5) adjusted_contrast_add = tf.image.adjust_contrast(img_data, 0.5) adjusted_contrast_random = tf.image.random_contrast(img_data, lower=0, upper=1) # 色相 adjusted_hue1 = tf.image.adjust_hue(img_data, 0.1) adjusted_hue2 = tf.image.adjust_hue(img_data, 0.3) adjusted_hue3 = tf.image.adjust_hue(img_data, 0.6) adjusted_hue4 = tf.image.adjust_hue(img_data, 0.9) adjusted_hue_random = tf.image.random_hue(img_data, max_delta=0.5) # 饱和度 adjusted_saturation_sub = tf.image.adjust_saturation(img_data,-5) adjusted_saturation_add = tf.image.adjust_saturation(img_data, 5) adjusted_saturation_random = tf.image.random_saturation(img_data, -5, 5)
img_data = tf.image.resize_images(img_data,(180,267),method=1) batched = tf.expand_dims(tf.image.convert_image_dtype(img_data,dtype=tf.float32),0) # [0.05,0.05,0.9,0.7] 表示(180*0.05,180*0.9) (267*0.05,267*0.7)之间的图像 # [y_min,x_min,y_max,x_max] boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]]) result = tf.image.draw_bounding_boxes(batched,boxes) plt.imshow(result.eval().reshape([180, 267, 3])) plt.show() boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box( tf.shape(img_data), bounding_boxes=boxes, min_object_covered=0.1 ) batched = tf.expand_dims( tf.image.convert_image_dtype(img_data, dtype=tf.float32), 0 ) image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw) distorted_image = tf.slice(img_data, begin, size) plt.figure(0) plt.imshow(image_with_box.eval().reshape([180, 267, 3])) plt.figure(1) plt.imshow(distorted_image.eval()) plt.show()
min_object_covered=0.1 需要加入,否则将会报错
文章来源: TensorFlow 图像处理