TensorFlow provides a number of commonly used image processing interface, allowing us to easily manipulate the image data, the following first shows a piece of the original image of the code, and then on this basis, practice tensorflow different APIs.
Show original picture
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =tf.image.decode_jpeg (raw_data)8 plt.imshow (Img_data.eval ())9Plt.show ()
Operating effects such as:
Where Tf.gfile.FastGFile is used to read the local file,tf.image.decode_jpeg is used to decode the JPEG image raw data into the dimensional space, that is, width, height, channel, Finally call the PYPLT library to display the picture.
Scaling of images
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =tf.image.decode_jpeg (raw_data)8Img_data2 = Tf.cast (Tf.image.resize_images (Img_data, [+]), dtype=tf.uint8);9 plt.imshow (Img_data2.eval ())TenPlt.show ()
TensorFlow scaled image interface is tf.image.resize_images,[200, 200] is the scaled target size, Here called the Tf.cast type conversion function, because after scaling processing, the tensor type is float32, and pyplt to the image format requirements Uint8, so must turn, otherwise what effect, you can try it yourself.
The Tf.image.resize_images interface can specify different scaling algorithms, such as:
Tf.image.resize_images (Img_data, [+], method=tf.image.resizemethod.bicubic)
Inversion of images
The inversion of image is used more in each depth learning algorithm, mainly through this operation can enlarge the number of samples, he le.
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =tf.image.decode_jpeg (raw_data)8Img_data2 = Tf.cast (Tf.image.flip_left_right (img_data), dtype=tf.uint8)9 plt.imshow (Img_data2.eval ())TenPlt.show ()
The above code calls the left and right inversion interfaces, and the TensorFlow also provides up and down inversion and random reversal operations, no more one by one attempts.
Crop center cropping for images
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =tf.image.decode_jpeg (raw_data)8Img_data2 = Tf.cast (Tf.image.resize_image_with_crop_or_pad (Img_data,,), dtype=tf.uint8)9 plt.imshow (Img_data2.eval ())TenPlt.show ()
The Tf.image.resize_image_with_crop_or_pad function can be used for image cropping or expansion, which is determined by the user's target width and height, and whether the cropping or expansion is based on the image center.
Specify position clipping
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =tf.image.decode_jpeg (raw_data)8Img_data2 = Tf.cast (Tf.image.crop_to_bounding_box (img_data, 0, 0, $), dtype=tf.uint8)9 plt.imshow (Img_data2.eval ())Ten plt.show () One A~
The above code specifies that the upper-left corner of the 200px square box is clipped, the specified target range must be reasonable, otherwise an exception will occur.
Picture Frame on image
1 ImportMatplotlib.pyplot as Plt2 ImportTensorFlow as TF3 4Raw_data = Tf.gfile.FastGFile ('./new.jpg','RB'). Read ()5 6 With TF. Session () as Sess:7Img_data =Tf.cast (Tf.expand_dims (Tf.image.decode_jpeg (Raw_data), 0), Tf.float32)8boxes = Tf.constant ([[[[[0.4], 0.4, 0.5, 0.5], [0.5, 0.5, 0.6, 0.6]]])9Img_data2 = Tf.cast (Tf.image.draw_bounding_boxes (Img_data, boxes), dtype=tf.uint8)Ten plt.imshow (Img_data2.eval () [0]) OnePlt.show ()
This code has a few places to note, after the JPEG decoding, called tf.expand_dims, this function means to add a dimension in the specified position, because after decoding is 3-dimensional data, in the 0-bit add one dimension, in fact, added a batch dimension, This is done primarily to cater to the back frame function! The boxes Operation node defines two boxes, uses the 0~1 floating point number to identify the box position ratio, the final picture display position also must notice, the output is four dimensions, please take out the first picture display. To display the effect, manually enlarge the effect of the picture, otherwise, the 1px box may be abbreviated in the PLT, please note!
TensorFlow Image Processing API