方法介紹:
簡單嘗試了一下Tensorflow
中的幾調(diào)整圖像大小的方法:
首先從知乎上搬磚下來相關中文介紹:
## 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ī)則會報錯
size:一個dtype
為int32
擁有兩個元素的1-D張量藕咏,格式為[new_height, new_width]
method:resize使用的方法氮凝,有四種方式芝硬,分別為:
方法 | 介紹 |
---|---|
ResizeMethod.BILINEAR | 雙線性內(nèi)插泻蚊,其核心思想是在兩個方向分別進行一次線性插值栅哀。 |
ResizeMethod.NEAREST_NEIGHBOR | 最近鄰插值法溃睹,將變換后的圖像中的原像素點最鄰近像素的灰度值賦給原像素點的方法而账,返回圖像張量dtype與所傳入的相同。 |
ResizeMethod.BICUBIC | 雙三次插值因篇,雙三次插值是一種更加復雜的插值方式泞辐,它能創(chuàng)造出比雙線性插值更平滑的圖像邊緣。 |
ResizeMethod.AREA | 基于區(qū)域的圖像插值算法竞滓,首先將原始低分辨率圖像分割成不同區(qū)域咐吼,然后將插值點映射到低分辨率圖像, 判斷其所屬區(qū)域商佑, 最后根據(jù)插值點的鄰域像素設計不同的插值公式锯茄, 計算插值點的值。 |
align_corners:精確對準輸入輸出圖像的四個角茶没,默認為false
不精確對準肌幽。
return:dtype
為float
的3-D
或4-D
圖像張量,其shape
分別為[batch, new_height, new_width, channels]
和[new_height, new_width, channels]
而其余四個接口則是具體的不同實現(xiàn)圖像縮放處理的方法抓半,他們的參數(shù)都形如:
(images, size, align_corners=False, name=None)
第一個參數(shù)要求其shape
一定是形如[batch, height, width, channels]
的4-D格式喂急,中間兩個參數(shù)如resize_image
所解釋,后一個name
是操作的名稱笛求,可有可無廊移。
實驗部分
首先我們拿紅發(fā)香克斯的圖像作為測試:
測試代碼如下:
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()
其中關于python下配置tensorflow以及opencv的網(wǎng)上方法比較多讥蔽,可以自行g(shù)oogle之,或者我這有一個懶人版的配置tensorflow-gpu的參考画机。
最終效果如下:
比較奇怪的是bi-cubic
插值的方法中出現(xiàn)了許多artifacts