關(guān)于Tensorflow中的幾個(gè)image_resize對(duì)比

方法介紹:

簡單嘗試了一下Tensorflow中的幾調(diào)整圖像大小的方法:
首先從知乎上搬磚下來相關(guān)中文介紹:

## Resizing
*   @{tf.image.resize_images}
*   @{tf.image.resize_area}
*   @{tf.image.resize_bicubic}
*   @{tf.image.resize_bilinear}
*   @{tf.image.resize_nearest_neighbor}

resize_images是總的接口,該接口的參數(shù)如下:

resize_images(images, size, method=ResizeMethod.BILINEAR, align_corners=False)

形參:

images:shape 為[batch, height, width, channels]的4-D圖像張量或者shape為 [height, width, channels]的3-D圖像張量,如果傳入圖像張量不兼容所制定規(guī)則會(huì)報(bào)錯(cuò)

size:一個(gè)dtypeint32擁有兩個(gè)元素的1-D張量,格式為[new_height, new_width]

method:resize使用的方法,有四種方式,分別為:

方法 介紹
ResizeMethod.BILINEAR 雙線性內(nèi)插,其核心思想是在兩個(gè)方向分別進(jìn)行一次線性插值。
ResizeMethod.NEAREST_NEIGHBOR 最近鄰插值法,將變換后的圖像中的原像素點(diǎn)最鄰近像素的灰度值賦給原像素點(diǎn)的方法,返回圖像張量dtype與所傳入的相同。
ResizeMethod.BICUBIC 雙三次插值,雙三次插值是一種更加復(fù)雜的插值方式,它能創(chuàng)造出比雙線性插值更平滑的圖像邊緣。
ResizeMethod.AREA 基于區(qū)域的圖像插值算法,首先將原始低分辨率圖像分割成不同區(qū)域,然后將插值點(diǎn)映射到低分辨率圖像, 判斷其所屬區(qū)域, 最后根據(jù)插值點(diǎn)的鄰域像素設(shè)計(jì)不同的插值公式, 計(jì)算插值點(diǎn)的值。

align_corners:精確對(duì)準(zhǔn)輸入輸出圖像的四個(gè)角,默認(rèn)為false不精確對(duì)準(zhǔn)。

return:dtypefloat3-D4-D圖像張量,其shape分別為[batch, new_height, new_width, channels][new_height, new_width, channels]

而其余四個(gè)接口則是具體的不同實(shí)現(xiàn)圖像縮放處理的方法,他們的參數(shù)都形如:

(images, size, align_corners=False, name=None)

第一個(gè)參數(shù)要求其shape一定是形如[batch, height, width, channels]的4-D格式,中間兩個(gè)參數(shù)如resize_image所解釋,后一個(gè)name是操作的名稱,可有可無。

實(shí)驗(yàn)部分

首先我們拿紅發(fā)香克斯的圖像作為測(cè)試:


shanks_small.jpg

測(cè)試代碼如下:

import tensorflow as tf
import cv2
import numpy as np

img_raw = cv2.imread('data/shanks_small.jpg')
img_in = img_raw / 255.
h, w, _ = img_in.shape

tf_img_in = tf.placeholder(dtype=tf.float32, shape=(None, None, 3))

scale = 3
tf_img_op1 = tf.image.resize_images(tf_img_in, [h * scale, w * scale], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
tf_img_op2 = tf.image.resize_images(tf_img_in, [h * scale, w * scale], method=tf.image.ResizeMethod.BILINEAR)
tf_img_op3 = tf.image.resize_images(tf_img_in, [h * scale, w * scale], method=tf.image.ResizeMethod.BICUBIC)
tf_img_op4 = tf.image.resize_images(tf_img_in, [h * scale, w * scale], method=tf.image.ResizeMethod.AREA)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

img_op1, img_op2, img_op3, img_op4 = sess.run([tf_img_op1, tf_img_op2, tf_img_op3, tf_img_op4],
                                              feed_dict={tf_img_in: img_in})

img_op1[img_op1 < 0] = 0
img_op1[img_op1 > 1] = 1
img_op2[img_op1 < 0] = 0
img_op2[img_op1 > 1] = 1
img_op3[img_op1 < 0] = 0
img_op3[img_op1 > 1] = 1
img_op4[img_op1 < 0] = 0
img_op4[img_op1 > 1] = 1

img_op1 = np.asarray(img_op1 * 255, np.uint8)
img_op2 = np.asarray(img_op2 * 255, np.uint8)
img_op3 = np.asarray(img_op3 * 255, np.uint8)
img_op4 = np.asarray(img_op4 * 255, np.uint8)

cv2.imshow('image in', img_raw)
cv2.imshow('nearest neighbor', img_op1)
cv2.imshow('bi-linear', img_op2)
cv2.imshow('bi-cubic', img_op3)
cv2.imshow('area', img_op4)

# cv2.imwrite('data/nearest_neighbor.jpg', img_op1)
# cv2.imwrite('data/bi-linear.jpg', img_op2)
# cv2.imwrite('data/bi-cubic.jpg', img_op3)
# cv2.imwrite('data/area.jpg', img_op4)

cv2.waitKey()

其中關(guān)于python下配置tensorflow以及opencv的網(wǎng)上方法比較多,可以自行g(shù)oogle之,或者我這有一個(gè)懶人版的配置tensorflow-gpu的參考
最終效果如下:

area.jpg

bi-cubic.jpg
bi-linear.jpg
nearest_neighbor.jpg

比較奇怪的是bi-cubic插值的方法中出現(xiàn)了許多artifacts

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容