# -*- coding:utf-8 -*- import tensorflow as tf import cv2
Img_H=222 Img_W=250
img=cv2.imread(filename="234.jpg")
img2 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.BILINEAR) img3 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) img4 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.BICUBIC) img5 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.AREA)
img2_ = tf.cast(img2, tf.uint8) img3_ = tf.cast(img3, tf.uint8) img4_ = tf.cast(img4, tf.uint8) img5_ = tf.cast(img5, tf.uint8)
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print (img.shape) cv2.imshow("img",img) img2_, img3_, img4_, img5_=sess.run([img2_, img3_, img4_, img5_])
# print ("******双线性插值***Bilinear interpolation********") cv2.imshow("双线性插值***Bilinear interpolation", img2_) # print ("******最近邻插值***Nearest neighbor interpolation************") cv2.imshow("最近邻插值***Nearest neighbor interpolation", img3_) # print ("********双三次插值**** Bicubic interpolation.*********") cv2.imshow("双三次插值** Bicubic interpolation.", img4_) # print ("*****区域插值****Area interpolation.************") cv2.imshow("区域插值**Area interpolation", img5_) print ("ok")
cv2.waitKey(0)
原图:
缩放图片:
错误范例: 线性插值之后,像素的值,就会变成非整数,需要转成整数,才可以进行显示: 而临近插值,是从旁边的像素值拿到,还是整数,不需要转格式,就可以显示。 错误如下:
# -*- coding:utf-8 -*- import tensorflow as tf import cv2
Img_H=222 Img_W=250
img=cv2.imread(filename="234.jpg")
img2 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.BILINEAR) img3 = tf.image.resize_images(images=img,size=[Img_H,Img_W],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print (img.shape) cv2.imshow("img",img) img2_, img3_=sess.run([img2, img3])
# print ("******双线性插值***Bilinear interpolation********") cv2.imshow("双线性插值***Bilinear interpolation", img2_) # print ("******最近邻插值***Nearest neighbor interpolation************") cv2.imshow("最近邻插值***Nearest neighbor interpolation", img3_) # print ("********双三次插值**** Bicubic interpolation.*********")
cv2.waitKey(0)
|