Tensorflow中影像處理函數(映像大小調整)_tensorflow

來源:互聯網
上載者:User

映像大小的調整方式:在Tensorflow中通過tf.image.resize_images函數實現;

1.雙線性插值演算法(Bilinear interpolation);Method取值為:0;

2.最近鄰居法(Nearest  neighbor interpolation);Method取值為:1;

3.雙立方插值法(Bicubic interpolation);Method取值為:2;

4.面積插值法(Area interpolation) ;Method取值為:3;

import matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npimage_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)  #第一個參數為原始映像,第二個參數為映像大小,第三個參數給出了指定的演算法    resized = np.asarray(resized.eval(),dtype='uint8')    plt.imshow(resized)    plt.show()    croped = tf.image.resize_image_with_crop_or_pad(img_data,200,200)    #靶心圖表像大小<原始映像的大小,則截取原始映像的置中部分,    padded = tf.image.resize_image_with_crop_or_pad(img_data,800,800)    #靶心圖表像大小>原始映像的大小,則會在原始映像的四周填充全為0背景    plt.imshow(croped.eval())    plt.show()    plt.imshow(padded.eval())    plt.show()    central_cropped = tf.image.central_crop(img_data,0.5)                #按照比例裁剪映像,第二個參數為調整比例,比例取值[0,1]    plt.imshow(central_cropped.eval())    plt.show()

在上述代碼中,通過tf.image.resize_images函數調整映像的大小。

並且Tensorflow提供API對映像進行裁剪或者填充,當靶心圖表像大小小於原始映像的大小時,則需要裁剪原始映像

                                                                                           當靶心圖表像大小在大於原始映像的情況下,則需要填充原始映像四周

最後還可以通過函數tf.image.central_crop()來按照比例調整映像大小;

上述代碼實驗結果圖為:

                  原始映像:                tf.image.resize_images(img_data, [300,300],method=0)     tf.image.resize_image_with_crop_or_pad(img_data,200,200)

                       

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

                                                                            








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.