Image processing function (image resizing) in TensorFlow _tensorflow

Source: Internet
Author: User

Image size Adjustment mode: In TensorFlow through the tf.image.resize_images function to achieve;

1. bilinear interpolation algorithm (bilinear interpolation); Method takes the value of: 0;

2. Nearest neighbour law (nearest neighbor interpolation); Method takes the value of: 1;

3. Double three times interpolation method (bicubic interpolation); Method takes the value of: 2;

4. Area interpolation method (areas interpolation); Method takes the value of: 3;

 Import Matplotlib.pyplot as PLT import tensorflow as TF import numpy as NP Image_raw_data = Tf.gfile.GFile (' d:/path/ To/picture/8.jpg ', ' RB '). Read () #加载原始图像 with TF.

    Session () as Sess:img_data = Tf.image.decode_jpeg (image_raw_data) plt.imshow (Img_data.eval ()) Plt.show () resized = Tf.image.resize_images (Img_data, [300,300],method=0) #第一个参数为原始图像, the second parameter is the image size, and the third parameter gives the specified algorithm resized = Np.asa Rray (Resized.eval (), dtype= ' uint8 ') plt.imshow (resized) plt.show () croped = Tf.image.resize_image_with_crop_or _pad (img_data,200,200) #目标图像大小 < The size of the original image, the center portion of the original image is intercepted, padded = Tf.image.resize_image_with_crop_or_pad (img_data,8 
    00,800) #目标图像大小 > The size of the original image, the full 0 background plt.imshow (croped.eval ()) plt.show () Plt.imshow (Padded.eval ()) is filled around the original image.
    Plt.show () central_cropped = Tf.image.central_crop (img_data,0.5) #按照比例裁剪图像, the second parameter is the adjustment scale, the proportional value [0,1] Plt.imshow (Central_cropped.eval ()) plt.show () 

In the above code, resize the image by using the Tf.image.resize_images function.

And TensorFlow provides an API to crop or populate images, and when the target image size is smaller than the original image size, you need to crop the original image

When the target image size is larger than the original image, you need to populate the original image around

Finally, the function Tf.image.central_crop () can be used to adjust the image size in proportion.

The above code experiment result diagram is:

Original image: Tf.image.resize_images (img_data, [300,300],method=0) Tf.image.resize_image_with_crop_or_pad (img_d ATA,200,200)

Tf.image.resize_image_with_crop_or_pad (img_data,800,800)                       Tf.image.central_crop (img_data,0.5)









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.