tensorflow Data Augmentation 코드(전복, 밝기, 자르기)

5697 단어 컴퓨터 시각 CV
1 Encoding and Decoding
1. tf.image.decode_jpeg(contents, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)
Decode a JPEG-encoded image to a uint8 tensor. jpeg의 그림을 uint8의 장량, 모양 3-D로 디코딩하여 [height,width,channels]
contents는 인코딩된 그림으로string의 tensor 형식이고 형태 0-D,
channels는 채널을 대표합니다. 기본 0이면 인코딩 그림을 사용하는 채널 수를 표시하고, 1이면 그레이스케일, 3이면 rgb 형식으로 출력합니다.
eg:
import tensorflow as tf
import numpy as np
img_path = '/Users/apple/Downloads/Medlinker/util/1.jpg'   #        

def decode_img(path):
    file_queue = tf.train.string_input_producer([path])    #             ,    shuffle,    
    image_reader = tf.WholeFileReader()
    key, image = image_reader.read(file_queue)
    image_decode = tf.image.decode_jpeg(image, channels=0)

    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        # sess.run(tf.global_variables_initializer())
        x = sess.run(image_decode)
        coord.request_stop()
        coord.join(threads)
    return x


x = decode_img(img_path)
print(x.shape)

2. tf.image.encode_jpeg는 decode의 역방향 작업으로 uint8의 jpeg 그림을 인코딩합니다
3. tf.image.decode_png(contents, channels=None, name=None)
Decode a PNG-encoded image to a uint8 tensor. PNG 그림을 uint8의 장량으로 디코딩하고 모양 3-D with shape[height, width, channels]channels에는 다음과 같은 네 가지 값이 있습니다.
0: Use the number of channels in the PNG-encoded image.
1: output a grayscale image.
3: output an RGB image.
4: output an RGBA image.
4. tf.image.encode_png decode의 역방향 조작으로 uint8의 png 그림을 인코딩합니다
 
2 Resizing
1. tf.image.resize_images(images, new_height, new_width, method=0) 
그림resize를 새로운 모양으로 만들려면 너비와 높이를 괄호로 묶어야 한다
def resize_img(path):
    file_queue = tf.train.string_input_producer([path])
    image_reader = tf.WholeFileReader()
    key, image = image_reader.read(file_queue)
    decode_ = tf.image.decode_png(image)
    image_resize = tf.image.resize_images(decode_, (666, 55))   #           
    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        x = sess.run(image_resize)
        coord.request_stop()
        coord.join(threads)
    return x

2. tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)
그림을 목표 사이즈로 재단(또는 채우기)하여 너비와 높이를 묶지 않아도 된다
image_resize = tf.image.resize_image_with_crop_or_pad(decode_, 666, 55)

3.tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
image_resize = tf.image.crop_to_bounding_box(decode_, 100,200,300,400)  # 100,200       ,300,400      

4. tf.image.decode_and_crop_jpeg(image,size,seed=None,name=None)은 그림을 디코딩하고 재단합니다. 사이즈는 재단된 좌표와 목표 사이즈입니다. 이 함수는 decode와crop을 조합합니다.
def resize_img(path):
    file_queue = tf.train.string_input_producer([path])
    image_reader = tf.WholeFileReader()
    key, image = image_reader.read(file_queue)
    # decode_ = tf.image.decode_png(image)
    image_resize = tf.image.decode_and_crop_jpeg(image, [40, 50, 200, 300])
    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        x = sess.run(image_resize)
        coord.request_stop()
        coord.join(threads)
    return x

x = resize_img(img_path)
plt.ion()
plt.imshow(x)
plt.pause(10)

 
 
 
3 Flipping and Transposing
1. tf.image.flip_up_down 그림을 위아래로 뒤집기
2. tf.image.random_flip_up_down(image, seed=None) 무작위 위아래 뒤집기
3. tf.image.flip_left_right(image) 좌우 뒤집기
4. tf.image.random_flip_left_right(image, seed=None) 무작위 좌우 뒤집기
def flip_img(path):
    file_queue = tf.train.string_input_producer([path])
    image_reader = tf.WholeFileReader()
    key, image = image_reader.read(file_queue)
    decode_img = tf.image.decode_png(image)
    flip_im = tf.image.random_flip_up_down(decode_img)
    with tf.Session() as sess:
        coord = tf.train.Coordinator()   # #       ,    
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        x = sess.run(flip_im)
        coord.request_stop()
        coord.join(threads)
    return x


x = flip_img(img_path)
plt.ion()
plt.imshow(x)
plt.pause(10)

 
5. tf.image.transpose_이미지 (image) 를 그림으로 바꾸기, 즉 너비와 높이를 바꾸기
 
 
4 Image Adjustments
1. tf.image.adjust_brightness(image, delta, min value=None, max value=None) # delta는 음수
2. tf.image.random_brightness(image,max delta,seed=None)#여기maxdelta는 반드시 마이너스여야 합니다.
3. tf.image.adjust_contrast(images, contrast factor,min value=None,max value=None) 대비 조정
4. tf.image.random_contrast(image, lower, upper, seed=None)
def adjust_img(path):
    file_queue = tf.train.string_input_producer([path])
    image_reader = tf.WholeFileReader()
    key, image = image_reader.read(file_queue)
    decode_img = tf.image.decode_jpeg(image)
    adjust_img = tf.image.adjust_brightness(decode_img, 0.2)    # 0.2         ,        
    with tf.Session() as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        x = sess.run(adjust_img)
        coord.request_stop()
        coord.join(threads)
    return x

x = adjust_img(img_path)
plt.ion()
plt.imshow(x)
plt.pause(10)

5. tf.image.per_image_standardization(image) 백화 작업은 3차원 행렬의 디지털 균일치가 0이 되고 방차가 1이 된다.
 
 
 
 
 

좋은 웹페이지 즐겨찾기