tensorflow image data processing

Learned tensorflow image data processing today, so write a note.

1 The first is to import the library

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

2 Read the picture

image_raw_data = tf.gfile.FastGFile('../datasets/cat.jpg','rb').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    
    # Output the decoded 3D matrix.
    print (img_data.eval())
    img_data.set_shape([1797, 2673, 3])
    print (img_data.get_shape())

used python3. Read a picture of a cat whose shape is (1797, 2673, 3).

3 Print the picture

with tf.Session() as sess:
    plt.imshow(img_data.eval())
    plt.show()

4 Resize the image

with tf.Session() as sess:    
    resized = tf.image.resize_images(img_data, [300, 300], method=0)
    
    # The data stored by the TensorFlow function after processing the image is in float32 format and needs to be converted to uint8 to print the image correctly.
    print ("Digital type: ", resized.dtype)
    cat = np.asarray(resized.eval(), dtype='uint8')
    # tf.image.convert_image_dtype(rgb_image, tf.float32)
    plt.imshow(cat)
    plt.show()
At its core is the tf.image.resize_images function. Through the above procedure, the picture is resized to 300×300. The value of the method parameter of the tf.image.resize_images function corresponds to different image resizing algorithms, as shown in the following table
The value of the method parameter of the tf.image.resize_images function corresponds to different image resizing algorithms
Method value Image Adjustment Algorithms

0

Bilinear Interpolation
1 nearest neighbor method ( nearest_neighbor  nearest_neighbor  Interpolation )
2 Bicubic interpolation ( nearest_neighbor Bicubic interpolation )
3 Area  nearest_neighbor  Interpolation

The results of images adjusted by different algorithms will be slightly different, but not too far apart. The picture does not have a title set, so there is no picture.

5 Crop and fill the picture

 
with tf.Session() as sess:    
    croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
    padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()

    The image is resized by the nearest_neighbortf.image.resize_image_with_crop_or_pad function. Its first parameter is the original image, and the last two parameters are the adjusted target image size. If the adjusted image is smaller than the original image, it will be cropped, and if the adjusted image is larger than the original image, it will be filled with 0 as the background.

It can be resized proportionally as follows:

central_cropped = tf.image.central_crop(img_data, 0.5)

    The first parameter of the tf.image.central_crop function is the original image, and the second is the adjusted scale. The code above captures the middle 50% of the image.

In addition, tensorflow also provides the tf.crop_to_bounding_box function and the tf.pad_to_bounding_box function to crop and fill the image of the given area, so the use of these two functions requires the given size to meet certain requirements.

6 Image flip

The following code implements flipping the image up and down, flipping left and right, and flipping diagonally:

with tf.Session() as sess:
    # flip up and down
    flipped1 = tf.image.flip_up_down(img_data)
    # Flip left and right
    flipped2 = tf.image.flip_left_right(img_data)
    
    #diagonal flip
    transposed = tf.image.transpose_image(img_data)
    plt.imshow(transposed.eval())
    plt.show()

7 Picture color adjustment

 
with tf.Session() as sess:     
    # Set the brightness of the image to -0.5.
    #adjusted = tf.image.adjust_brightness(img_data, -0.5)
    
    # Set the brightness of the image to -0.5
    #adjusted = tf.image.adjust_brightness(img_data, 0.5)
    
    # 在[-max_delta, max_delta)的范围随机调整图片的亮度。
    adjusted = tf.image.random_brightness(img_data, max_delta=0.5)
    
    # 将图片的对比度-5
    #adjusted = tf.image.adjust_contrast(img_data, -5)
    
    # 将图片的对比度+5
    #adjusted = tf.image.adjust_contrast(img_data, 5)
    
    # 在[lower, upper]的范围随机调整图的对比度。
    #adjusted = tf.image.random_contrast(img_data, lower, upper)

    plt.imshow(adjusted.eval())
    plt.show()

上面程序分别介绍了调整图像亮度与对比度

8 添加色相饱和度

with tf.Session() as sess:         
    adjusted = tf.image.adjust_hue(img_data, 0.1)
    #adjusted = tf.image.adjust_hue(img_data, 0.3)
    #adjusted = tf.image.adjust_hue(img_data, 0.6)
    #adjusted = tf.image.adjust_hue(img_data, 0.9)
    
    # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
    #adjusted = tf.image.random_hue(image, max_delta)
    
    # 将图片的饱和度-5。
    #adjusted = tf.image.adjust_saturation(img_data, -5)
    # 将图片的饱和度+5。
    #adjusted = tf.image.adjust_saturation(img_data, 5)
    # 在[lower, upper]的范围随机调整图的饱和度。
    #adjusted = tf.image.random_saturation(img_data, lower, upper)
         
    plt.imshow(adjusted.eval())
    plt.show()

9 添加标注框

with tf.Session() as sess:         

    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)


    batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0) 
    image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
    
    distorted_image = tf.slice(img_data, begin, size)
    plt.imshow(distorted_image.eval())
    plt.show()

在图像处理中,图像中需要关注的物体通常会被标注框圈出来,tensorflow通过tf.image.draw_bounding_boxes函数在图像中添加标注框。

1 需要进行图片数据的float转换 

2 如果不是一个batch,是一个图片的话需要手动加一个维度

3 box = [[[]]]

4 最后显示需要转换回去 需要知道原本图片的维度数据利用 print(sess.run(img_data).shape)得出



 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325864994&siteId=291194637