一、图像编码处理
在之前的章节中提到一张 R G B RGB RGB色彩模式的图像可以看成一个三维矩阵,矩阵中的每一个数表示了图像上不同位置,不同颜色的亮度。然而图像在存储时并不是直接记录这些矩阵中的数字,而是记录经过压缩编码之后的结果。所以要将一张图像还原成一个三维矩阵,需要解码的过程。 T e n s o r F I o w TensorFIow TensorFIow提供了对 j p e g jpeg jpeg和 p n g png png格式图像的编码/解码函数。以下代码示范了如何使用 T e n s o r F l o w TensorFlow TensorFlow中对 j p e g jpeg jpeg格式图像的编码/解码函数。
import tensorflow as tf
import matplotlib.pyplot as plt
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
with tf.Session() as sess:
# 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
# TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
# 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
img_data = tf.image.decode_jpeg(image_raw_data)
# 使用pyplot工具可视化得到的图像。
plt.imshow(img_data.eval())
plt.show()
# 将数据的类型转换成实数。
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
# 将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中。
# 打开这张图像,可以得到和原始图像一样的图像。
encode_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("/path/to/output", "wb") as f:
f.write(encode_image.eval())
二、图像大小调整
一般来说,网络上获取的图像大小是不固定的,但神经网络输入节点的个数是固定的。所以在将图像的像素作为输入提供给神经网络之前,需要先将图像的大小统一。这就是图像大小调整需要完成的任务。图像大小调整有两种方式,第一种是通过算法使得新的图像尽量保存原始图像上的所有信息。
T
e
n
s
o
r
F
l
o
w
TensorFlow
TensorFlow提供了匹种不同的方法,并且将它们封装到
了tf.image.resize_images
函数。以下代码示范了如何使用这个函数。
import tensorflow as tf
import matplotlib.pyplot as plt
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
with tf.Session() as sess:
# 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
# TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
# 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
img_data = tf.image.decode_jpeg(image_raw_data)
# 将数据的类型转换成实数。
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
# 通过tf.image.resize_images函数调整图像的大小。这个函数第一个参数为原始图像,
# 第二个为调整后图像的大小,method参数给出了调整图像大小的算法。
resized = tf.image.resize_images(img_data, [400, 400], method=0)
# 输出调整后的图像的大小,此处的结果为(?,?,?)。表示图像的的大小是400x400,
# 但图像的深度在没有明确设置之前会是问号。
print(img_data.get_shape())
# 使用pyplot工具可视化得到的图像。
plt.imshow(resized.eval())
plt.show()
> (?, ?, ?)
这里tf.image.resize_images
函数中的method
参数取值与相对应的图像大小调整算法。
method取值 | 图像大小调整算法 |
---|---|
0 | 双线性插值法(Bilinearinterpolation) |
1 | 最近邻居法(Nearestneighborinterpolation) |
2 | 双三次插值法(Bicubicinterpolation) |
3 | 面积插值法(Areainterpolation) |
双线性插值法:
最近邻居法:
双三次插值法:
运行该方法时会报错,需要调整代码。
报的错误:
ValueError: Floating point image RGB values must be in the 0..1 range.
更改后的代码:
with tf.Session() as sess:
。。。
。。。
# 输出调整后的图像的大小,此处的结果为(?,?,?)。表示图像的的大小是300x300,
# 但图像的深度在没有明确设置之前会是问号。
print(img_data.get_shape())
after_img = tf.clip_by_value(resized, 0.0, 1.0)
# 使用pyplot工具可视化得到的图像。
plt.imshow(after_img.eval())
# plt.imshow(resized.eval())
plt.show()
面积差值法:
从上面的图中可以看出,不同算法去调整出来的结果会有细微差别,但不会相差太远。除了将整张图像信息完整保存,
T
e
n
s
o
r
F
l
o
w
TensorFlow
TensorFlow还提供了
A
P
I
API
API对图像进行裁剪或者填充。以下代码展示了通过tf.image.resize-mage-with-crop-or_pad
函数来调整图像大小的功能。
import tensorflow as tf
import matplotlib.pyplot as plt
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("./picture/2.jpeg", 'rb').read()
with tf.Session() as sess:
# 将图像使用jpeg的格式解码从而得到图相对应的三维矩阵。
# TensorFlow还提供了tf.image.decode_png函数对png格式的图像进行解码。
# 解码之后的结果为一个张量,在使用它的取值之前需要明确调用运行的过程。
img_data = tf.image.decode_jpeg(image_raw_data)
# 将数据的类型转换成实数。
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
# 使用pyplot工具可视化得到的图像。
plt.imshow(img_data.eval())
plt.show()
# 通过tf.image.resize_image_with_crop_or_pad函数调整图像的大小。
# 这个函数的第一个参数为原始图像,后面两个参数是调整后的目标图像大小。
# 如果原始图像的尺寸大于目标图像,那么这个函数会自动截取原始图像中居中的部分。
# 如果目标图像大于原始图像,这个函数会自动在原始图像的四周填充全0背景。
croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 2000, 2000)
# 使用pyplot工具可视化得到的图像。
plt.imshow(croped.eval())
plt.show()
plt.imshow(padded.eval())
plt.show()
原始图像:
croped——500x500图像:
padded——2000x2000图像:
如果想要更多的资源,欢迎关注 @我是管小亮,文字强迫症MAX~
回复【福利】即可获取我为你准备的大礼,包括C++,编程四大件,NLP,深度学习等等的资料。
想看更多文(段)章(子),欢迎关注微信公众号「程序员管小亮」~