tensorflow图像数据处理相关函数总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/summer2day/article/details/82534104

图像大小

调整图像大小:
tf.image.resize_images(img,[300,300],method=0)
参数:method:0  双线性插值
              1  最近邻局法
              2  双三次插值法
              3  面积插值法

tf.image.resize_image.with_crop_or_pad(img,1000,1000)
如果原始图像小于目标图像,居中裁剪。反之,四周填充补0
tf.image.central_crop(img,0.5)按比例裁剪(0,1]之间

图像翻转

上下:tf.image.flip_up_down(img)
左右:tf.image.flip_left_right(img)
对角线:tf.image.transpose_image(img)
以50%概率上下翻转:tf.image.random_flip_up_down(img)
以50%概率左右翻转:tf.image.random_flip_left_right(img)

图像色彩调整

1.亮度

将图像亮度减0.5:tf.image.adjust_brightness(img,-0.5)
在[-max_delta,max_delta]范围内随机调整图像的亮度:
tf.image.random_brightness(img,max_delta)
色彩调整可能使像素的值超过0.0-1.0的正常范围内,从而图像无法正常可视化。截断操作可以将图像拉回到正常范围内。截断操作要在所有图像处理操作之后:tf.clip_by_value(adjusted,0.0,1.0)

2.对比度

对比度减少0.5倍:tf.image.adjust_contrast(img,0.5)
在[lower,upper]范围内随机调整对比度:tf.image.adjust_contrast(img,lower,upper)

3.色相

增加0.1色相:tf.image.adjust_hue(img,0.1)
在[-max_delta,max_delta]范围内随机调整色相:tf.image_random_hue(img,max_delta)

4.饱和度

饱和度增加5:tf.image.adjust_saturation(img,5)
在[lower,upper]范围内随机调整饱和度:tf.image.random_saturation(img,lower,upper)

5.标准化

均值变为0,方差变为1
tf.image.per_image_standardization(img)

处理标注框

tf.image.draw_bounding_boxes函数要求图像矩阵中的数字为实数,输入是一个batch的数据,若是一张图像需要加一维。一个标注框有4个数字,是相对位置,代表着[ymin,xmin,ymax,xmax]

batched=tf.expand_dims(tf.image.convert_image_dype(img,tf.float32),0)
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)

随机截取图像上有信息量的部分,tf.image.sample_distorted_bounding_box,min_object_covered=0.1表示截取部分至少包含某个标注框10%的内容,返回值为3个张量:begin,size和 bboxes。前2个张量用于 tf.slice 剪裁图像。后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。

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(image_data), bounding_boxes=boxes, min_object_covered=0.1)

batched = tf.expand_dims(tf.image.convert_image_dtype(image_data, tf.float32), 0)

image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)

distorted_image = tf.slice(image_data, begin, size)

猜你喜欢

转载自blog.csdn.net/summer2day/article/details/82534104