Tensorflow 图像处理类
1.导入库
import tensorflow as tf
import numpy as np
2.读取图片数据
#opencv方式
img = cv.imread(img_path)
#tensorflow方式
jpg = tf.read_file(img_path)
img = tf.image.decode_jpeg(jpg, channels=3) #需要解码
3.数据增强(图像变换)
###调整图像尺寸
resized_img = tf.image.reisze_images(img,[width,height].method) #重新定义图像尺寸
crop_img = tf.image.reisze_image_with_crop_or_pad(img,width,height) #裁切图像至给定尺寸 原图比尺寸大
pad_img = tf.image.reisze_image_with_crop_or_pad(img,width,height) #填充图像至给定尺寸 原图比尺寸小
###旋转
Rotate_img = tf.image.rot90(img,k=x) #旋转90度x次 x:旋转次数
Rotate_angle_img = tf.contrib.image.rotate(img,angles) #旋转任意角度 angles:旋转角度
###翻转
Flip_LF_img = tf.image.flip_left_right(img) #水平翻转
Flip_UD_img = tf.image.flip_up_down(img) #垂直翻转
flip_img = = tf.image.transpose_image # 转置图像
###裁剪
Random_Crop_img = tf.random_crop(img,size,seed) #随机裁剪 size为裁剪后大小
Central_Crop_img = tf.image.central_crop(img,rate)#以中心为准切取图像 rate = 0.5(截取中间50%的图片)
Box_Crop_img = tf.image.crop_to_bounding_box(img,offset_height,offset_width,target_height,target_width)#根据给定左上角位置裁剪图像
#offset_height 和 offset_width 是需要切出图像的左上角坐标
###亮度
Adjusted_Brightness_img = tf.image.adjust_brightness(img,delta) #改变图像亮度 delta = ±x
Random_Adjusted_Brightness_img = = tf.image.random_brightness(img_data, max_delta=0.5) 在[-max_delta, max_delta)的范围随机调整图片的亮度。
###对比度
Adjusted_Contrast_img = tf.image.adjust_contrast(img,rate) #调整图像对比度
Random_Adjusted_Contrast_img = tf.image.random_contrast(img_data, lower, upper) # 在[lower, upper]的范围随机调整图的对比度。
###色彩
Adjusted_Hue_img = tf.image.adjust_hue(img,rate) #调整图像色相
Random_Adjusted_Hue_img = tf.image.random_hue(image, max_delta) # 在[-max_delta, max_delta]的范围随机调整图片的色相。
Adjusted_Saturation_img = tf.image.adjust_saturation(img,rate) #调整图像饱和度
Random_Adjusted_Saturation_img = tf.image.random_saturation(img,lower, upper) #调整到范围内随机的图像饱和度
参考文献:
1.https://blog.csdn.net/kwame211/article/details/78579035
2.https://blog.csdn.net/weiwei9363/article/details/79917942
3.https://blog.csdn.net/sinat_29957455/article/details/80629098
4.https://blog.csdn.net/mago2015/article/details/82563856