TensorFlow 图像处理

1.统一调整图像尺寸

__author__ = 'ding'
'''
TensorFlow 图像处理
'''
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data = tf.gfile.FastGFile('./path/to/picture1.jpeg', 'rb').read()

with tf.Session() as sess:
    # 将图像以jpeg的格式解码从而得到图像对应的三维矩阵
    # tf.image_decode_png 函数对png格式图形进行解码。解码之后得到一个张量
    # tf.image_decode_jpeg 函数对jpeg格式图形进行解码。
    img_data = tf.image.decode_jpeg(image_raw_data)
    print(img_data.eval())
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8)

    # # 导出
    # img_export(img_data)
    #
    # # 调整尺寸
    # img_size(img_data)
    #
    # # 裁剪
    # img_fill_cut(img_data)
    #
    # # 翻转
    # img_transposed(img_data)
    #
    # # 色彩调整
    # img_color(img_data)

1.图像导出

def img_export(img_data):
    # 将一张图像的三维矩阵重新按jpeg格式编码并存入文件中,可以得到和原始图像一样的图像
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile('./path/to/output.jpeg', 'wb') as f:
        f.write(encoded_image.eval())

2.调整图像大小

def img_size(img_data):
    # tf.image.resize_images 函数调整图像的大小,
    # 一个参数为原始图像
    # 第二个参数为图像尺寸
    # 第三个给出调整图像大小的算法
    # method=0  双线性插值法
    # method=1  最近邻居法
    # method=2  双三次插值法
    # method=3  面积插值法
    resized_0 = tf.image.resize_images(img_data, (300, 300), method=0)
    resized_1 = tf.image.resize_images(img_data, (300, 300), method=1)
    resized_2 = tf.image.resize_images(img_data, (300, 300), method=2)
    resized_3 = tf.image.resize_images(img_data, (300, 300), method=3)
    # print(img_data.get_shape())

    plt.figure(0)
    plt.imshow(resized_0.eval())
    plt.figure(1)
    plt.imshow(resized_1.eval())
    plt.figure(2)
    plt.imshow(resized_2.eval())
    plt.figure(3)
    plt.imshow(resized_3.eval())
    plt.show()

3.图像裁剪、填充

def img_fill_cut(img_data):
    # 通过tf.image.resize_image_with_crop_or_pad函数调整图像大小,
    # 第一个参数为原始图像,第二、三个参数分别表示图像的长高
    # 如果原始尺寸大于指定尺寸,则进行裁剪
    # 如果原始尺寸小于指定尺寸,则进行填充,默认为0 黑色
    corped = tf.image.resize_image_with_crop_or_pad(img_data, 300, 300)
    padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
    plt.figure(0)
    plt.imshow(corped.eval())
    plt.figure(1)
    plt.imshow(padded.eval())

    # 通过tf.image.central_crop函数按比例裁剪图像
    # 第一个参数为原始图像
    # 第二个参数为裁剪比例,范围(0,1]
    central_cropped = tf.image.central_crop(img_data, 0.5)
    plt.figure(3)
    plt.imshow(central_cropped.eval())
    plt.show()

4.图像翻转

def img_transposed(img_data):
    # 上下翻转
    flipped_up_down = tf.image.flip_up_down(img_data)
    # 左右翻转
    flipeed_left_right = tf.image.flip_left_right(img_data)
    # 对角线翻转
    transposed = tf.image.transpose_image(img_data)
    plt.figure(0)
    plt.imshow(flipped_up_down.eval())
    plt.figure(1)
    plt.imshow(flipeed_left_right.eval())
    plt.figure(2)
    plt.imshow(transposed.eval())
    plt.show()

    # 以一定概率上下翻转
    flipped = tf.image.random_flip_up_down(img_data)
    # 以一定概率左右翻转
    flipped_left_right = tf.image.random_flip_left_right(img_data)
这样的用法是,随机翻转训练图像,让模型可以识别不同角度的实体

5.图像色彩调整

def img_color(img_data):
def img_color(img_data):
    # 亮度
    adjusted_brightness_sub = tf.image.adjust_brightness(img_data, -0.5)
    adjusted_brightness_add = tf.image.adjust_brightness(img_data, 0.5)
    adjusted_brightness_random = tf.image.random_brightness(img_data, max_delta=1)

    # 对比度
    adjusted_contrast_sub = tf.image.adjust_contrast(img_data, -0.5)
    adjusted_contrast_add = tf.image.adjust_contrast(img_data, 0.5)
    adjusted_contrast_random = tf.image.random_contrast(img_data, lower=0, upper=1)

    # 色相
    adjusted_hue1 = tf.image.adjust_hue(img_data, 0.1)
    adjusted_hue2 = tf.image.adjust_hue(img_data, 0.3)
    adjusted_hue3 = tf.image.adjust_hue(img_data, 0.6)
    adjusted_hue4 = tf.image.adjust_hue(img_data, 0.9)
    adjusted_hue_random = tf.image.random_hue(img_data, max_delta=0.5)

    # 饱和度
    adjusted_saturation_sub = tf.image.adjust_saturation(img_data,-5)
    adjusted_saturation_add = tf.image.adjust_saturation(img_data, 5)
    adjusted_saturation_random = tf.image.random_saturation(img_data, -5, 5)

6.标注框

   img_data = tf.image.resize_images(img_data,(180,267),method=1)
   batched = tf.expand_dims(tf.image.convert_image_dtype(img_data,dtype=tf.float32),0)
   # [0.05,0.05,0.9,0.7] 表示(180*0.05,180*0.9) (267*0.05,267*0.7)之间的图像
   # [y_min,x_min,y_max,x_max]
   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)

   plt.imshow(result.eval().reshape([180, 267, 3]))
   plt.show()

   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, min_object_covered=0.1
   )
   batched = tf.expand_dims(
       tf.image.convert_image_dtype(img_data, dtype=tf.float32), 0
   )
   image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
   distorted_image = tf.slice(img_data, begin, size)
   plt.figure(0)
   plt.imshow(image_with_box.eval().reshape([180, 267, 3]))
   plt.figure(1)
   plt.imshow(distorted_image.eval())
   plt.show()

min_object_covered=0.1 需要加入,否则将会报错

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容