Tensorflow 图像处理函数
2017-08-22 12:14
441 查看
1. 图像编码处理
以下代码示范了如何使用Tensorflow中对jpeg格式图像的编码/解码函数:
2. 图像大小调整:
resize=tf.image.
以下代码示范了如何使用Tensorflow中对jpeg格式图像的编码/解码函数:
import matplotlib.pyplot as plt import tensorflow as tf image_raw_data=tf.gfile.FastGFile("/home/cynthia/Pictures/IMG_20170701_150105.jpeg",'r').read() with tf.Session() as sess: img_data=tf.image.decode_jpeg(image_raw_data) print img_data.eval() plt.imshow(img_data.eval()) plt.show() img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8) encoded_image=tf.image.encode_jpeg(img_data) with tf.gfile.GFile("/home/cynthia/Pictures/1.jpeg","wb") as f: f.write(encoded_image.eval())
2. 图像大小调整:
resized=tf.image.resize_images(img_data,[300,300],method=0) print img_data.get_shape()tf.image.resize_images函数中method参数取值与相应的图像大小调整算法 [code]0 双线行插值法ResizeMethod.BILINEAR: Bilinear interpolation.[/code]1 最近邻居法 ResizeMethod.NEAREST_NEIGHBOR: Nearest neighbor interpolation.2 双三次插值法 ResizeMethod.BICUBIC: Bicubic interpolation.3 面积插值法 ResizeMethod.AREA: Area interpolation
In [1]:import matplotlib.pyplot as plt import tensorflow as tf import numpy as np1. 读取图片
In [2]:image_raw_data = tf.gfile.FastGFile("../../datasets/cat.jpg",'r').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()[[[163 161 140] [163 161 138] [163 161 140] ..., [106 139 48] [101 137 47] [104 140 52]] [[164 162 141] [164 162 139] [163 161 138] ..., [107 138 45] [103 138 46] [108 138 50]] [[167 162 142] [167 162 140] [164 162 139] ..., [105 136 42] [103 137 43] [108 139 46]] ..., [[207 200 181] [207 200 181] [206 199 180] ..., [109 84 54] [109 83 56] [107 82 52]] [[207 200 181] [206 199 180] [206 199 180] ..., [108 83 52] [106 81 51] [106 81 50]] [[207 200 181] [206 199 180] [206 199 180] ..., [109 85 51] [107 82 51] [106 81 50]]] (1797, 2673, 3)2. 打印图片
In [3]:with tf.Session() as sess: plt.imshow(img_data.eval()) plt.show()3. 重新调整图片大小
In [4]:with tf.Session() as sess: resized = tf.image.resize_images(img_data, [300, 300], method=0) # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。 print "Digital type: ", resized.dtype cat = np.asarray(resized.eval(), dtype='uint8') # tf.image.convert_image_dtype(rgb_image, tf.float32) plt.imshow(cat) plt.show()Digital type: <dtype: 'float32'>4. 裁剪和填充图片
In [5]: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()5. 截取中间50%的图片
In [6]:with tf.Session() as sess: central_cropped = tf.image.central_crop(img_data, 0.5) plt.imshow(central_cropped.eval()) plt.show()6. 翻转图片
In [7]:with tf.Session() as sess: # 上下翻转 #flipped1 = tf.image.flip_up_down(img_data) # 左右翻转 #flipped2 = tf.image.flip_left_right(img_data) #对角线翻转 transposed = tf.image.transpose_image(img_data) plt.imshow(transposed.eval()) plt.show() # 以一定概率上下翻转图片。 #flipped = tf.image.random_flip_up_down(img_data) # 以一定概率左右翻转图片。 #flipped = tf.image.random_flip_left_right(img_data)7. 图片色彩调整
In [8]:with tf.Session() as sess: # 将图片的亮度-0.5。 #adjusted = tf.image.adjust_brightness(img_data, -0.5) # 将图片的亮度-0.5 #adjusted = tf.image.adjust_brightness(img_data, 0.5) # 在[-max_delta, max_delta)的范围随机调整图片的亮度。 adjusted = tf.image.random_brightness(img_data, max_delta=0.5) # 将图片的对比度-5 #adjusted = tf.image.adjust_contrast(img_data, -5) # 将图片的对比度+5 #adjusted = tf.image.adjust_contrast(img_data, 5) # 在[lower, upper]的范围随机调整图的对比度。 #adjusted = tf.image.random_contrast(img_data, lower, upper) plt.imshow(adjusted.eval()) plt.show()8. 添加色相和饱和度
In [9]:with tf.Session() as sess: adjusted = tf.image.adjust_hue(img_data, 0.1) #adjusted = tf.image.adjust_hue(img_data, 0.3) #adjusted = tf.image.adjust_hue(img_data, 0.6) #adjusted = tf.image.adjust_hue(img_data, 0.9) # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。 #adjusted = tf.image.random_hue(image, max_delta) # 将图片的饱和度-5。 #adjusted = tf.image.adjust_saturation(img_data, -5) # 将图片的饱和度+5。 #adjusted = tf.image.adjust_saturation(img_data, 5) # 在[lower, upper]的范围随机调整图的饱和度。 #adjusted = tf.image.random_saturation(img_data, lower, upper) # 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。 #adjusted = tf.image.per_image_whitening(img_data) plt.imshow(adjusted.eval()) plt.show()9. 添加标注框并裁减。
In [10]:with tf.Session() as sess: 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) batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0) image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw) distorted_image = tf.slice(img_data, begin, size) plt.imshow(distorted_image.eval()) plt.show()
完整代码:代码源地址:https://github.com/caicloud/tensorflow-tutorial/blob/master/Deep_Learning_with_TensorFlow/0.12.0/Chapter07/2.2. 图像预处理完整样例.ipynb
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 1.随机调整图片的色彩,定义两种顺序。In[2]:
def distort_color(image, color_ordering=0):
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32. / 255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
else:
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
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)
return tf.clip_by_value(image, 0.0, 1.0)
# 2.对图片进行预处理,将图片转化成神经网络的输入层数据。In[3]:
def preprocess_for_train(image, height, width, bbox):
# 查看是否存在标注框。
if bbox is None:
bbox = 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)
# 随机的截取图片中一个块。
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes=bbox)
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes=bbox)
distorted_image = tf.slice(image, bbox_begin, bbox_size)
# 将随机截取的图片调整为神经网络输入层的大小。
distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))
distorted_image = tf.image.random_flip_left_right(distorted_image)
distorted_image = distort_color(distorted_image, np.random.randint(2))
return distorted_image
# 3.读取图片。In[4]:
image_raw_data = tf.gfile.FastGFile("/home/cynthia/Pictures/cat.jpg", "r").read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
for i in range(9):
result = preprocess_for_train(img_data, 299, 299, boxes)
plt.imshow(result.eval())
plt.show()
resize=tf.image.
相关文章推荐
- 如何用TensorFlow图像处理函数裁剪图像?
- Tensorflow中图像处理函数(图像大小调整)
- TensorFlow 图像处理和解码操作函数概述
- TensorFlow图像预处理-函数
- Tensorflow中图像处理函数
- TensorFlow 图像处理和解码操作函数
- 如何用TensorFlow图像处理函数裁剪图像?
- Matlab 图像处理相关函数命…
- DM6000处理图像的函数入口
- Matlab图像处理常用基本函数
- 几个图像处理函数
- tensorflow 图像数据处理(一)
- 【图像处理】MATLAB:退化函数及多种复原方法
- Matlab图像处理函数:regionprops
- MATLAB中与图像处理有关的函数
- GD 和图像处理 函数
- OpenCV图像处理中常用函数汇总
- matlab的几个图像处理函数
- TensorFlow学习笔记-图像预处理
- tensorflow 图像处理