tensorflow图像数据处理

今天学到tensorflow图像数据处理,所以写个笔记。

1 首先是导入库


   
   
  1. import matplotlib.pyplot as plt
  2. import tensorflow as tf
  3. import numpy as np

2 读取图片

   
   
  1. image_raw_data = tf.gfile.FastGFile( '../datasets/cat.jpg', 'rb').read()
  2. with tf.Session() as sess:
  3. img_data = tf.image.decode_jpeg(image_raw_data)
  4. # 输出解码之后的三维矩阵。
  5. print (img_data.eval())
  6. img_data.set_shape([ 1797, 2673, 3])
  7. print (img_data.get_shape())

用的python3。读的是一个猫的图片其shape是(1797, 2673, 3)。

3 打印图片

   
   
  1. with tf.Session() as sess:
  2. plt.imshow(img_data.eval())
  3. plt.show()

4 调整图像大小


   
   
  1. with tf.Session() as sess:
  2. resized = tf.image.resize_images(img_data, [ 300, 300], method= 0)
  3. # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
  4. print ( "Digital type: ", resized.dtype)
  5. cat = np.asarray(resized.eval(), dtype= 'uint8')
  6. # tf.image.convert_image_dtype(rgb_image, tf.float32)
  7. plt.imshow(cat)
  8. plt.show()
其核心是tf.image.resize_images函数。通过上面程序,图片被调整成为300×300的。tf.image.resize_images函数的method参数取值对应不同的图像大小调整算法,如下表
tf.image.resize_images函数的method参数取值对应不同的图像大小调整算法
Method取值图像调整算法

0

双线性插值法(Bilinear Interpolation)
1最近邻居法(nearest_neighbor nearest_neighbor Interpolation)
2双三次插值法(nearest_neighborBicubic interpolation)
3面积插值法(Area nearest_neighbor Interpolation)

不同算法调整的图片结果会有细微差别,但不相差太远。图片不会设置标题就没有放图。

5 裁剪和填充图片
 

             
             
  1. with tf.Session() as sess:
  2. croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
  3. padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
  4. plt.imshow(croped.eval())
  5. plt.show()
  6. plt.imshow(padded.eval())
  7. plt.show()

    通过nearest_neighbortf.image.resize_image_with_crop_or_pad  函数调整图像大小。其第一个参数为原始图像,后面俩个参数是调整后的目标图像大小。如果调整的图像小于原始图像,就会裁剪,如果调整的图像大于原始图像,就会填充0为背景。

可以通过比例调整大小,如下:

central_cropped = tf.image.central_crop(img_data, 0.5)
             
             

    tf.image.central_crop函数第一个参数为原始图像,第二个为调整比例。上面的代码截取的中间50%的图像。

另外,tensorflow也提供了tf.crop_to_bounding_box函数与tf.pad_to_bounding_box函数来裁剪与填充给定区域的图像,故而使用这俩个函数要求给定的尺寸满足一定要求。

6 图像翻转

下面代码实现了图像上下翻转,左右翻转,以及沿对角线翻转:


             
             
  1. with tf.Session() as sess:
  2. # 上下翻转
  3. flipped1 = tf.image.flip_up_down(img_data)
  4. # 左右翻转
  5. flipped2 = tf.image.flip_left_right(img_data)
  6. #对角线翻转
  7. transposed = tf.image.transpose_image(img_data)
  8. plt.imshow(transposed.eval())
  9. plt.show()

7 图片色彩调整
 

             
             
  1. with tf.Session() as sess:
  2. # 将图片的亮度-0.5。
  3. #adjusted = tf.image.adjust_brightness(img_data, -0.5)
  4. # 将图片的亮度-0.5
  5. #adjusted = tf.image.adjust_brightness(img_data, 0.5)
  6. # 在[-max_delta, max_delta)的范围随机调整图片的亮度。
  7. adjusted = tf.image.random_brightness(img_data, max_delta= 0.5)
  8. # 将图片的对比度-5
  9. #adjusted = tf.image.adjust_contrast(img_data, -5)
  10. # 将图片的对比度+5
  11. #adjusted = tf.image.adjust_contrast(img_data, 5)
  12. # 在[lower, upper]的范围随机调整图的对比度。
  13. #adjusted = tf.image.random_contrast(img_data, lower, upper)
  14. plt.imshow(adjusted.eval())
  15. plt.show()

上面程序分别介绍了调整图像亮度与对比度

8 添加色相饱和度

             
             
  1. with tf.Session() as sess:
  2. adjusted = tf.image.adjust_hue(img_data, 0.1)
  3. #adjusted = tf.image.adjust_hue(img_data, 0.3)
  4. #adjusted = tf.image.adjust_hue(img_data, 0.6)
  5. #adjusted = tf.image.adjust_hue(img_data, 0.9)
  6. # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
  7. #adjusted = tf.image.random_hue(image, max_delta)
  8. # 将图片的饱和度-5。
  9. #adjusted = tf.image.adjust_saturation(img_data, -5)
  10. # 将图片的饱和度+5。
  11. #adjusted = tf.image.adjust_saturation(img_data, 5)
  12. # 在[lower, upper]的范围随机调整图的饱和度。
  13. #adjusted = tf.image.random_saturation(img_data, lower, upper)
  14. plt.imshow(adjusted.eval())
  15. plt.show()

9 添加标注框

             
             
  1. with tf.Session() as sess:
  2. boxes = tf.constant([[[ 0.05, 0.05, 0.9, 0.7], [ 0.35, 0.47, 0.5, 0.56]]])
  3. begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
  4. tf.shape(img_data), bounding_boxes=boxes)
  5. batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0)
  6. image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
  7. distorted_image = tf.slice(img_data, begin, size)
  8. plt.imshow(distorted_image.eval())
  9. plt.show()

在图像处理中,图像中需要关注的物体通常会被标注框圈出来,tensorflow通过tf.image.draw_bounding_boxes函数在图像中添加标注框。

1 需要进行图片数据的float转换 

2 如果不是一个batch,是一个图片的话需要手动加一个维度

3 box = [[[]]]

4 最后显示需要转换回去 需要知道原本图片的维度数据利用 print(sess.run(img_data).shape)得出



 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值