当前位置:网站首页>使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
2022-08-03 22:29:00 【Erosion_ww】
tf.image.resize和tf.image.resize_with_pad使用方法
tf.image.resize()
作用
~~~~ 使用指定的方法调整图像大小。
~~~~ 如果原始纵横比与大小不同,调整大小的图像将失真。 为避免失真,可用tf.image.resize_with_pad()
参数
tf.image.resize(
images, # 形状 [batch, height, width, channels] 的 4-D 张量或形状 [height, width, channels] 的 3-D 张量。
size, # 2 个元素的一维 int32 张量:new_height,new_width。 图像的新尺寸。
method=ResizeMethod.BILINEAR, # 一个 image.ResizeMethod,或等效的字符串。 默认为双线性。
preserve_aspect_ratio=False, # 是否保留纵横比。
# true,图像将被调整为适合大小的大小,同时保留原始图像的纵横比。
# 若尺寸大于图像的当前尺寸,则放大图像。 默认为false。
antialias=False, # 对图像进行下采样时是否使用anti-aliasing filter。
name=None # 此操作的名称(可选)。
)
~~~~ 其中,Image.ResizeMethod()的选项有:
~~~~ • bilinear: 双线性插值。若antialisa为true,则在下采样时成为半径为1的hat/tent filter。
~~~~ • lanczos3: Lanczos kernel with radius 3。高质量的实用过滤器,但可能有一些ringing,尤其是在合成图像上。
~~~~ • lanzos5:Lanczos kernel with radius 5。非常高质量的滤波器,但可能有更强的ringing。
~~~~ • bicubic:三次插值。相当于Catmull-Rom kernel,与lanczos3kernel相比较,质量相当好,速度更快,尤其是在上采样时。
~~~~ • gaussian:Gaussian kernel with radius 3, sigma = 1.5 / 3.0.
~~~~ • nearest:最近邻插值。 与最近邻插值一起使用时,antialias无效。
~~~~ • area:使用区域插值进行抗锯齿重采样。 与区域插值一起使用时,antialias没有效果。
~~~~ •mitchellcubic:Mitchell-Netravali Cubic non-interpolating filter. 对于合成图像(尤其是那些缺乏适当预过滤的图像),ringing比Cubic interpolant of Keys少,不那么锐利。
返回值
~~~~ If images was 4-D, a 4-D float Tensor of shape [batch, new_height, new_width, channels].
~~~~ If images was 3-D, a 3-D float Tensor of shape [new_height, new_width, channels].
例子
import tensorflow as tf # 导入tensorflow
img_path ='C:\\Users\\xxx\\.keras\\datasets\\flower_photos\\roses\\6158504080_b844a9ae05.jpg' # 输入图片路径
img_raw = tf.io.read_file(img_path) # 读取图片内容,返回值时string的tensor
img_tensor = tf.image.decode_image(img_raw) # 对输入的string的tensor进行解码
print(img_tensor.shape) # 输入解码后的图像形状
print(img_tensor.dtype) # 输入解码后的图像表示类型
img_final = tf.image.resize(img_tensor, [192, 192]) # 调整图像大小
print(img_final.shape) # 输入调整后的图像大小
(333, 500, 3)
<dtype: 'uint8'>
(192, 192, 3)
tf.image.resize_with_pad()
作用
~~~~ 调整图像大小并将图像填充到目标宽度和高度。
~~~~ 通过保持纵横比不变而不失真,将图像大小调整为目标宽度和高度。 如果目标尺寸与图像尺寸不匹配,则会调整图像大小,然后用零填充以匹配请求的尺寸。
参数
tf.image.resize_with_pad(
image, # 形状 [batch, height, width, channels] 的 4-D 张量或形状 [height, width, channels] 的 3-D 张量。
target_height, # 目标高度
target_width, # 目标宽度
method=ResizeMethod.BILINEAR, # 用于调整图像大小的方法。 见 image.resize()
antialias=False # 调整大小时是否使用抗锯齿。 参见“image.resize()”。
)
返回值
~~~~ Resized and padded image.
~~~~ If images was 4-D, a 4-D float Tensor of shape [batch, new_height, new_width, channels].
~~~~ If images was 3-D, a 3-D float Tensor of shape [new_height, new_width, channels].
主要参考:
tf.image.resize | TensorFlow Core v2.9.1 (google.cn)
tf.image.resize_with_pad | TensorFlow Core v2.9.1 (google.cn)
边栏推荐
- start with connect by implements recursive query
- Nine ways to teach you to read the file path in the resources directory
- LabVIEW code generation error 61056
- [N1CTF 2018]eating_cms
- 2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析
- 21天打卡挑战学习MySQL——《MySQL工具的使用》第一周 第二篇
- 重发布实验报告
- Embedded Systems: GPIO
- 关于Yii2批量更新的操作
- Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
猜你喜欢

override学习(父类和子类)

HCIP第十六天

Network basic learning series four (network layer, data link layer and some other important protocols or technologies)

Bytebase数据库 Schema 变更管理工具
![[b01lers2020]Life on Mars](/img/d0/d5c9b7224542c8843ce29adc7ef713.png)
[b01lers2020]Life on Mars

PowerMockup 4.3.4::::Crack

【bug】汇总Elipse项目中代码中文乱码解决方法!

Zilliz 2023 秋季校园招聘正式启动!

21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇

Conditional Statements for Shell Programming
随机推荐
【MySQL进阶】数据库与表的创建和管理
utlis thread pool
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
Cisco ike2 IPSec configuration
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
深度学习和机器学习有什么区别?
2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析
【day6】类与对象、封装、构造方法
.NET6之MiniAPI(十四):跨域CORS(上)
Work Subtotal QT Packing
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
Optimize the query (work in progress)
七夕快乐!
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
The development status of cloud computing at home and abroad
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
用于流动质押和收益生成的 Web3 基础设施
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)