tf.image.resize_image()直接放大缩小图片操作

# -*- 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)
      
      
 
 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值