Python image processing operation based on tensor flow


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
with tf.Session() as sess:
  #The three bit matrix data of the image is obtained by decoding the JPEG image

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
  #The processed image of tensorflow is float32 format, which needs to be converted to uint8 to output correctly

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
  #Random clipping


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
  #Flip left and right

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
  #Reduce contrast
  #Random contrast

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
#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]
  #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
  #Put each processed image in an array
#Reassemble the processed single images together  

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.