博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
神经网络调参实战(三) —— (1) —— 图像增强函数API(resize&crop裁剪&flip翻转& brightness & contrast改变光照和对比度)
阅读量:2135 次
发布时间:2019-04-30

本文共 3830 字,大约阅读时间需要 12 分钟。

数据增强的API

  • ①resize
  • ②crop  裁剪
  • ③flip  翻转
  • ④brightness & contrast  改变光照和对比度

 

基础的读入图片并显示的代码

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)sess = tf.Session()img_decoded_val = sess.run(img_decoded)print(img_decoded_val.shape)imshow(img_decoded_val)

显示图片的size是(365,600,3)

高是365,长是600,3通道(颜色)

 

resize

tf中有好几个resize的函数可以用来缩放图片

tf.image.resize_areatf.image_resize_bicubic #用二次线性插值法进行图片的缩放tf.image.resize_nearest_neighbor #在图片放大的时候用最相近的像素点来插入像素值

这些函数的输入都是四维的tensor

要先对原图片读入的tensor进行reshape一下,最后输出的时候再reshape回来

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])resize_img = tf.image.resize_bicubic(img_decoded,[730,1200]) #将图片拉大了sess = tf.Session()img_decoded_val = sess.run(resize_img)img_decoded_val = img_decoded_val.reshape((730,1200,3))print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()

出现这样的原因是做了线性插值之后,数值和类型就变成float的值了,我们需要变成int的值(是因为imshow这个函数不能将float很好得显示)

我们给它做一个类型变换

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])resize_img = tf.image.resize_bicubic(img_decoded,[730,1200])sess = tf.Session()img_decoded_val = sess.run(resize_img)img_decoded_val = img_decoded_val.reshape((730,1200,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()

得到了一张高730,宽1200的图像

 

crop

tf.image.pad_to_bounding_box #填充tf.image.crop_to_bounding_box #裁剪tf.random_crop #随机裁剪

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])padded_img = tf.image.pad_to_bounding_box(img_decoded,50,100,500,800) #画布的大小是500,800, 原始图像在画布上的位置是50,100sess = tf.Session()img_decoded_val = sess.run(padded_img)img_decoded_val = img_decoded_val.reshape((500,800,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()

 

flip

tf.image.flip_up_down #上下翻转 tf.image.flip_left_right #左右翻转tf.image.random_flip_up_down #随机上下翻转tf.image.random_flip_left_right #随机左右翻转

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])flipped_img = tf.image.flip_up_down(img_decoded)sess = tf.Session()img_decoded_val = sess.run(flipped_img)img_decoded_val = img_decoded_val.reshape((365,600,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()

 

brightness & contrast

tf.image.adjust_brightnesstf.image.random_brightnesstf.image.adjust_contrasttf.image.random_comtrast

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])new_img = tf.image.adjust_brightness(img_decoded,-0.5) #光照强度变为原来的50%sess = tf.Session()img_decoded_val = sess.run(new_img)img_decoded_val = img_decoded_val.reshape((365,600,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()

如果是加上50%

 

 

 

 

 

 

 

 

转载地址:http://xlygf.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】198-House Robber
查看>>
【LEETCODE】62-Unique Paths
查看>>
【LEETCODE】310-Minimum Height Trees
查看>>
【LEETCODE】207-Course Schedule
查看>>
【LEETCODE】263-Ugly Number
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>
【LEETCODE】205-Isomorphic Strings
查看>>