tensorflow 图像数据处理(一)

____tz_zs


这是关于图片的编码、解码、随机截取图像的Demo

用到的方法:

tf.image.draw_bounding_boxes

tf.image.sample_distorted_bounding_box

原图式样:



代码:

# -*- coding: utf-8 -*-
"""
@author: tz_zs
图片的编码、解码、随机截取图像
"""
import matplotlib.pyplot as plt
import tensorflow as tf

image_raw_data = tf.gfile.FastGFile("picture.jpg", 'rb').read()

with tf.Session() as sess:
    # 解码
    image_data = tf.image.decode_jpeg(image_raw_data)

    print(image_data.eval())
    plt.imshow(image_data.eval())
    plt.show()#图1
    """
    [[[ 94 131  53]
      [ 87 125  48]
      [ 83 121  48]
      ..., 
      [ 29  63  13]
      [ 31  65  14]
      [ 34  69  15]]
    
     [[106 150  65]
      [100 143  61]
      [ 94 138  59]
      ..., 
      [ 30  64  13]
      [ 32  67  13]
      [ 34  69  15]]
    
     [[111 161  72]
      [103 153  66]
      [ 97 149  64]
      ..., 
      [ 33  65  15]
      [ 34  67  14]
      [ 35  68  13]]
    
     ..., 
     [[250 250 250]
      [250 250 250]
      [251 251 251]
      ..., 
      [ 13  26   8]
      [ 14  25   9]
      [ 12  23   9]]
    
     [[248 248 248]
      [249 249 249]
      [250 250 250]
      ..., 
      [ 14  29  10]
      [ 15  28  10]
      [ 14  27  10]]
    
     [[250 250 250]
      [251 251 251]
      [249 249 249]
      ..., 
      [ 16  31  10]
      [ 16  31  12]
      [ 16  29  11]]]
    """
    # image_data = tf.image.convert_image_dtype(image_data, dtype=tf.float32)  # uint8→float32

    # 编码
    # encode_image = tf.image.encode_jpeg(image_data)# 接收的是 Tensor型uint8
    # with tf.gfile.GFile("output.jpg", 'wb') as f:
    #     f.write(encode_image.eval())

    # 缩小
    image_data = tf.image.resize_images(image_data, [180, 267], method=1)
    plt.imshow(image_data.eval())
    plt.show()#图2
    # print(tf.shape(image_data).eval())  # [180 267   3]

    # 随机截取图像
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    '''
    print(boxes)
    Tensor("Const:0", shape=(1, 2, 4), dtype=float32)
    print(boxes.eval())
    [[[0.05        0.05        0.89999998  0.69999999]
      [0.34999999  0.47        0.5         0.56]]]
    '''
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(image_data), bounding_boxes=boxes)

    print(begin.eval())
    print(size.eval())
    print(bbox_for_draw.eval())
    '''
    [ 9 46  0]
    [161 139  -1]
    [[[ 0.07777778  0.40449437  0.77777779  0.90262175]]]
    '''
    image_data = tf.slice(image_data, begin, size)
    plt.imshow(image_data.eval())
    plt.show()#图3

图1:



图2:



图3:








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值