tensorflow(一):圖片處理

1、圖片處理

  一、圖片存取 tf.gfile

import tensorflow as tf
import matplotlib.pyplot as plt

image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read()  #  字節
with tf.Session() as session:
    # 2.圖片解碼
    img = tf.image.decode_jpeg(image_bytes)
    # print(img) # tensor('DecodePnng:0', shape=(?,?,?),dtype=uint8)
    img_array = img.eval()  # 將tensor對象轉成數組
    # 3.圖片顯示
    plt.imshow(img_array)
    plt.show()
    # 4.圖片數據類型轉化(整形)
    # img = tf.image.convert_image_dtype(img, dtype=tf.float32)
    # print(img)
    # 5.圖像重編碼
    encode_image = tf.image.encode_jpeg(img)
    new_img = encode_image.eval()  # 數組
    # 6.圖片保存
    with tf.gfile.GFile("dog_new.png", "wb") as f:
        f.write(new_img)

  二、圖片修改 tf.image

import tensorflow as tf
import matplotlib.pyplot as plt

image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read()  #  字節
with tf.Session() as session:
    img = tf.image.decode_jpeg(image_bytes)
    # 翻轉圖片
    img_flipped = tf.image.flip_up_down(img)    # 上下反轉
    img_flipped = tf.image.flip_left_right(img_flipped) # 左右反轉
    img_flipped = tf.image.transpose_image(img_flipped) # 對角線反轉
    img_flipped = tf.image.random_flip_up_down(img_flipped)    # 隨機上下反轉
    img_flipped = tf.image.random_flip_left_right(img_flipped) # 隨機左右反轉
    # 亮度設置
    img_adjust = tf.image.adjust_brightness(img_flipped, -0.5) # 增長亮度
    img_adjust = tf.image.adjust_brightness(img_adjust, +0.5)  # 下降亮度
    img_adjust = tf.image.random_brightness(img_adjust, max_delta=0.3) # 隨機調整亮度,亮度在[-max_delta, +max_delta]]
    # 色度
    img_saturation = tf.image.adjust_saturation(img_adjust, 1.5)  # 支持random
   # 飽和度
img_hue = tf.image.adjust_hue(img_saturation, delta=0.2)
   # 對比度 img_contrast = tf.image.adjust_contrast(img_hue, 0.5) # 圖片標準化 img_standard = tf.image.per_image_standardization(img_adjust) img_standard = tf.clip_by_value(img_standard, 0.0, 10) # 轉成數組 img_array = img_standard.eval() plt.imshow(img_array) plt.show()

  三、圖像標註框

import tensorflow as tf
import matplotlib.pyplot as plt

image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read()  #  字節
with tf.Session() as session:
    img = tf.image.decode_jpeg(image_bytes)
    # 調整圖片大小
    img_resize = tf.image.resize_image_with_crop_or_pad(img, 300, 300)
    # 按比例截取圖片
    boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38], [0.38, 0.53, 0.53, 0.71]]])  # 兩個標註框
    # boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]])  # 設置一個RGB,設置四個角的比例位置
    # 給原始圖片添加一個圖層
    batched = tf.expand_dims(tf.image.convert_image_dtype(img_resize, tf.float32), 0)
    # 把boxes標註的框畫到原始圖片上
    image_with_boxes = tf.image.draw_bounding_boxes(batched, boxes)
    # 從新將原始圖片設置爲RGB
    image_with_boxes = tf.reshape(image_with_boxes, [300, 300, 3])
    img_array = image_with_boxes.eval()
    plt.imshow(img_array)
    plt.show()

  四、圖片隨機截取

import matplotlib.pyplot as plt

image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read()  #  字節
with tf.Session() as session:
    img = tf.image.decode_jpeg(image_bytes)
    # 給定截取框大小
    bounding_boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]])  # 設置一個RGB,設置四個角的比例位置
    # 選擇相關圖像截取算法截圖
    # Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
    begin, size, bboxes = tf.image.sample_distorted_bounding_box(
        tf.shape(img), bounding_boxes=bounding_boxes, min_object_covered=0.1
    )
    # 生成概要
    # img_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(tf.image.convert_image_dtype(img, dtype=tf.float32), 0), bboxes)
    # tf.summary.image('img_with_box', img_with_box)
    # print(begin.eval(), size.eval())
    # 截圖
    distorted_img = tf.slice(img, begin, size)
    img_array = distorted_img.eval()
    plt.imshow(img_array)
    plt.show()

  五、一個簡單樣例代碼,實現隨機截取圖片

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

class Sample:        
    def load_jpg(self, path, mode='rb'):
        image_bytes = tf.gfile.FastGFile(path, mode).read()
        return tf.image.decode_jpeg(image_bytes, channels=3)
    def _distort_picture(self, image, color_ordering=0):
        if color_ordering == 0:
            image = tf.image.random_brightness(image, max_delta=32./255.)  # 隨機亮度
            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)  # 對比度
            image = tf.image.random_hue(image, max_delta=0.2)              # 飽和度
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
        if color_ordering == 1:
            image = tf.image.random_hue(image, max_delta=0.2)              # 飽和度
            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
            image = tf.image.random_flip_left_right(image)
            image = tf.image.random_flip_up_down(image)
        return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0)  # 歸一化
    def _preprocess_for_train(self, image, height, width, bounding_boxes=None):
        if bounding_boxes is None:
            bounding_boxes = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
        if image.dtype != tf.float32:
            image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        begin, size, bboxes = tf.image.sample_distorted_bounding_box(
            tf.shape(image),  bounding_boxes=bounding_boxes, min_object_covered=0.1
        )
        # 隨機截圖
        distorted_image = tf.slice(image, begin=begin, size=size)
        # 調整隨機截圖的圖片大小
        # distorted_image = tf.image.resize_image_with_crop_or_pad(distorted_image, height, width)
        distorted_image = tf.image.resize_images(
            distorted_image, size=[height, width], method=np.random.randint(4)
        )
        # 隨機調整圖片的一些設置
        distorted_image = self._distort_picture(distorted_image, np.random.randint(2))
        return distorted_image
    def get_random_picture(self, number, image, *args, **kwargs):
        with tf.Session() as session:
            for i in range(number):
                random_picture = self._preprocess_for_train(image, *args, **kwargs)
                plt.imshow(random_picture.eval())
                plt.show()

def main():
    sample = Sample()
    image = sample.load_jpg("dog.jpg", 'rb')
#     bounding_boxes = tf.constant([0.2, 0.2, 0.8, 0.8], dtype=tf.float32, shape=[1, 1, 4])
    bounding_boxes = tf.constant([[[0.2, 0.2, 0.8, 0.8]]])
    height = width = 150
    sample.get_random_picture(5, image, height, width, bounding_boxes)
main()

  五、圖片處理有關函數整理

 函數  描述
tf.gfile.FastGFile 讀取單個圖片,返回字節流數據
tf.decode_jpeg 在圖片讀入操做以後,圖片處理以前,對圖片進行解碼
tf.encode_jpeg 在圖片保存時對圖片進行重編碼
tf.gfile.GFile 寫出單個圖片
tf.image.convert_image_dtype 轉換圖片的數據類型
tf.resize_images 剪裁圖片大小
tf.resize_image_with_crop_of_pad 剪裁單個圖片大小
tf.image.random_flip_left_right 圖片隨機左右反轉
tf.image.random_flip_up_down 圖片隨機上下反轉
tf.image.random_brightness 圖片隨機調整亮度
tf.image.random_hue 圖片隨機調整飽和度
tf.image.random_contrast 圖片隨機調整對比度
tf.image.random_saturation 圖片隨機調整色度
tf.image.per_image_standardization 單個圖片標準化
tf.image.clip_by_value 單個圖片歸一化,其它還有tf.image.clip_by_XXX等方法
tf.expand_dims 給圖片增長維度(圖層)
tf.image.sample_distorted_bounding_box 生成隨機子圖
tf.image.draw_bounding_boxes 將標註框標註的子圖取出來
tf.image.reshape 調整圖片的維度
tf.slice 截取隨機子圖爲單個圖片

2、TFRecord

  TFRecord文件是tensorflow指定的一種文件存儲格式。它由tf.train.Example和tf.train.Feature來規定和實現。python

# tf.train.Example Protocol Buffer
message Example { Features features = 1; } message Features { map<string, Feature> feature = 1; } message Feature { oneof kind{ BytesList bytes_list = 1; FloatList float_list = 2; Int64List int64_list = 3; } }

  一、TFRecord文件寫出

  手寫字mnist數據下載地址: http://yann.lecun.com/exdb/mnist/算法

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np # 導入訓練集和測試集的圖片和標籤 mnist = input_data.read_data_sets("tensorflow/mnist/", dtype=tf.uint8, one_hot=True) # 獲取圖片和標籤 images = mnist.train.images # images.shape (55000, 784) 熱獨編碼 labels = mnist.train.labels # labels.shape (55000, 10) # 獲取圖像的數量及圖片的分辨率([......]) numbers, pixels = images.shape # 按照tf.train.Example Protocol Buffer來定義TFRecord文件格式 def _int64(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def example_protocol_buffer(pixel, size, image): example = tf.train.Example( features=tf.train.Features( feature={ 'pixels': _int64(pixels), 'label': _int64(size), 'image': _bytes(image) } ) ) return example.SerializeToString() # 序列化爲字節 # 輸出文件地址 filename = "tensorflow/test/mnist.tfrecord" # 建立一個writer writer = tf.python_io.TFRecordWriter(filename) # 遍歷每張圖片 for index in range(numbers): image = images[index].tostring() # 轉成字節 serialize = example_protocol_buffer(pixels, np.argmax(labels[index]), image) writer.write(serialize) writer.close() print("done.")

  二、TFRecord文件讀入

import tensorflow as tf
import matplotlib.pyplot as plt
# 建立reader reader = tf.TFRecordReader() # 建立字節流讀取隊列 filename_queue = tf.train.string_input_producer( ["tensorflow/test/mnist.tfrecord"] ) # 從文件中讀取一個樣例,read_up_to函數一次性讀取多個樣例 key, serialized_example = reader.read(filename_queue) # 解析讀取的一個樣例,若是須要解析多個樣例,能夠用parse_example def parse_single(serialized_example): features = tf.parse_single_example( serialized_example, features={ 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'pixels': tf.FixedLenFeature([], tf.int64) } ) # 將讀取的單個樣例解碼 image = tf.decode_raw(features['image'], tf.uint8) label = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32) return image, label, pixels sess = tf.Session() # 啓動多線程處理輸入數據 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(10): image, label, pixel = sess.run(parse_single(serialized_example)) print(image, label, pixel)    plt.imshow(image.reshape(28, 28))    plt.show()
相關文章
相關標籤/搜索