Python image processing operation based on tensor flow

Time:2020-11-13

This paper describes the python image processing operation based on tensor flow. For your reference, the details are as follows:

In the deep learning of images, sometimes the number of pictures may be insufficient, or we hope that the network can learn more. At this time, we can process the existing image data to make it into a new picture, and then learn on this basis, so as to improve the accuracy of network recognition.

1. Image decoding display

Using matplot library, we can easily and succinctly draw and output pictures in jupyter tf.gfile Open the picture file and use the function tf.image.decode_ JPEG decodes the JPEG image into a three bit matrix, and then the image information can be drawn and displayed by matplot

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
 
#Read image file
image_raw=tf.gfile.GFile('D:\Temp\MachineLearning\data\cat.jpeg','rb').read()
 
with tf.Session() as sess:
  #The three bit matrix data of the image is obtained by decoding the JPEG image
  image_data=tf.image.decode_jpeg(image_raw)
  print(image_data.eval())
  plt.imshow(image_data.eval())
  plt.show()

You can see the printed picture, 3D matrix information and displayed picture:

                            

2. Image zooming

Tensorflow also comes with many image processing functions, such as resce_ Image zooms the size of the image. The first parameter represents the image data source, the second array represents the size after scaling, and the third method represents the scaling method adopted. The default value is bilinear interpolation, 1 represents nearest neighbor interpolation, 2 represents bicubic interpolation, and 3 represents pixel region interpolation.

#Scale the image size
  image_resize=tf.image.resize_images(image_data,[500,500],method=0)
  #The processed image of tensorflow is float32 format, which needs to be converted to uint8 to output correctly
  image_resize=np.asarray(image_resize.eval(),dtype='uint8')
  plt.imshow(image_resize)
  plt.show()

3. Image cutting

function tf.image.resize_ image_ with_ crop_ Or_ Pad can be used to cut or fill the image under the condition of ensuring the original proportion of the image.

function tf.image.random_ Crop is a random selection of the image crop, not the center.

#Image clipping
  image_crop=tf.image.resize_image_with_crop_or_pad(image_data,500,500)
  plt.imshow(image_crop.eval())
  plt.show()
  #Random clipping
  img_random=tf.image.random_crop(image_data,[300,300,3])
  plt.imshow(img_random.eval())
  plt.show()

                              

resize_ image_ with_ crop_ Or_ The first parameter of pad is the image resource, and the last two parameters are the size of the cut image. When the original image is larger than the target value, the redundant parts on both sides will be cut off. When the image is smaller than the target value, it will be filled with black. For example, the left and right sides of the above picture are trimmed, and the top and bottom are filled with black.

random_ The first parameter of crop is the image resource, and the second parameter is a three digit tensor, which represents the size of the target image.

4. Image flip

The image can be flipped up and down, left and right through the function. During the model training, the original sample image can be inverted and input as a new eigenvalue for model training.

#Flip up and down
  img_down=tf.image.flip_up_down(image_data)
  plt.imshow(img_down.eval())
  plt.show()
  
  #Flip left and right
  img_left=tf.image.flip_left_right(image_data)
  plt.imshow(img_left.eval())
  plt.show()

5. Adjust contrast, lightness and saturation

adopt tf.image.adjust_ Contrast can adjust the image contrast. When the parameter is greater than 1, it means deepening, and less than 1 means thinning

tf.image.random_ Contrast can adjust the contrast within the specified range

There is also an adjustment_ brightness、adjust_ saturation、adjust_ Hue adjusts the lightness, saturation and hue

#Deepen contrast
  img_deep=tf.image.adjust_contrast(image_data,2)
  plt.imshow(img_deep.eval())
  plt.show()
  #Reduce contrast
  img_fade=tf.image.adjust_contrast(image_data,0.5)
  plt.imshow(img_fade.eval())
  plt.show()
  #Random contrast
  img_contrast=tf.image.random_contrast(image_data,0.5,2)
  plt.imshow(img_contrast.eval())
  plt.show()

6. The input picture of VGg network is processed

Image parameter x in VGg network training_ IMG is based on batch_ Size is the unit of four-dimensional data, for example, 20 32 × 32 3-channel pictures are passed in, and the data are [20,32,32,3]. However, tensorflow’s image processing function can only process a single picture in three dimensions. Therefore, it is necessary to first split 20 images into a single [1,32,32,3], and then convert them into 3D data [32,32,3] by reshape() function. Then, we call the image processing function to process the images, restore the processed images to four dimensions, and then put them in the array res_ In arr, the original set of 20 × 32 × 32 × 3 data is spliced.

#Batch batch_ Size images are divided into single images in the first dimension
img_arr=tf.split(x_img,batch_size,axis=0)
res_arr=[]
#Traverse each image and process it
for img in img_arr:
  #Processing a single four-dimensional picture [1,32,32,3] into three-dimensional [32,32,3]
  img=tf.reshape(img,[32,32,3])
  #Image enhancement of single image
  img_ flip= tf.image.random_ flip_ left_ Right (IMG) ා flip picture
  img_ bright= tf.image.random_ brightness(img_ flip,max_ Delta = 63) ා adjust brightness randomly
  img_ contrast= tf.image.random_ contrast(img_ Bright, lower = 0.2, upper = 1.8)
  #Change the enhanced image back to the original four-dimensional format
  img=tf.reshape(img_contrast,[1,32,32,3])
  #Put each processed image in an array
  res_arr.append(img)
#Reassemble the processed single images together  
img_aug=tf.concat(res_arr,axis=0)

More interested readers about Python related content can view the special topics of this website: Python data structure and algorithm tutorial, python encryption and decryption algorithm and skills summary, python coding operation skills summary, python function use skills summary, python character string operation skills summary and python introduction and advanced classic tutorial

I hope this article will be helpful to python programming.