0
点赞
收藏
分享

微信扫一扫

图像数据处理(一)


  在大部分图像识别问题中,通过图像数据预处理可以尽量避免模型受到无关因素的影响或者达到数据增强的目的,从而提高模型的准确性。Tensorflow 提供了几类简单的图像数据处理函数。

  图像编解码处理

  在一副RGB色彩模式的图像可以看做是一个三维矩阵,在矩阵中每一个数都代表图像上的不同位置,不同颜色的亮度。然而图像在存储时并没有直接记录这些矩阵中的数字,而是记录了经过压缩编码之后的结果,所以要将一副图像还原成一个三维矩阵,需要解码的过程。

  Tensorflow 提供了一些函数以实现对一些格式(如 ,jpeg ,jpg,png,gif) 等图像进行编码/解码的支持。

1 #!/usr/bin/env python 
2 # -*- coding: utf-8 -*-
3 import sys
4 import matplotlib.pyplot as plt
5 import tensorflow as tf
6 # 图像数据处理
7
8 # 读取原来的图像
9 image = tf.gfile.FastGFile('duck.png','rb').read()
10 with tf.Session() as sess:
11 # 对图像进行解码,得到图像对应的三维矩阵
12 img_after_decode = tf.image.decode_png(image)
13
14 print(img_after_decode.eval())
15
16 plt.imshow(img_after_decode.eval())
17 plt.show()
18 # 对解码后的图像进行编码
19 encode_image = tf.image.encode_png(img_after_decode)
20 with tf.gfile.GFile("duck2.png","wb") as f :
21 f.write(encode_image.eval() )


-- 选择 --

图像数据处理(一)_Image

   解码后: 

图像数据处理(一)_tensorflow_02

 

 对.jpeg /.jpg 格式图像进行解码,可以使用 image.decode_jpeg() 函数 ; 对于 .gif 格式的图像进行解码,可以使用 image.decode_gif() 函数。下面是解码的函数原型

 

# decode_jepg()
# decode_jepg(contents,channels,ratio,fancy_upscaling,try_recover_truncated,
acceptable_fraction,dct_method,name)
# decode_gif()
# decode_gif(contenst,name)

将图像数据编码成.jepg /.jpg 格式的图像可以使用 image_encode_jpeg() 函数

#encode_jpeg(image,format,quality,progressive,optimize_size,
chroma_downsampling,density_unit, x_density,y_density , xmp_metadata,name)

翻转图像

 在数据增强的过程中,我们最先使用的 image.random_flip_left_right()函数对图像进行随机左右翻转。

 

1 #!/usr/bin/env python
2 # Author: zhanghansen
3 # Created Time: 2019年08月01日 星期四 10时09分50秒
4
5 # coding:utf-8
6 import matplotlib.pyplot as plt
7 import tensorflow as tf
8
9 image = tf.gfile.FastGFile("duck.png","rb").read()
10
11 with tf.Session() as sess:
12 #decode
13 image_after_decode = tf.image.decode_png(image)
14
15 #以一定概论左右翻转图片
16 flipped = tf.image.random_flip_left_right(image_after_decode)
17
18 plt.imshow(flipped.eval())
19 plt.show()
20
21

图像数据处理(一)_Image_03

图像色彩调整

  我们使用 image.random_brightness() 函数对图像进行亮度的调整。

import matplotlib.pyplot as plt
import tensorflow as tf

image = tf.gfile.FastGFile("duck.png","rb").read()

with tf.Session() as sess :
img_after_decode = tf.image.decode_png(image)

# 函数原型 random_brightness(image,max_delta,sesd)
# max_delta 的值非负 , 函数会在[-max_melta ,max_melta] 之间随机调整图像的亮度
adjusted_brightness = tf.image.random_brightness(img_after_decode,max_delta=1)
plt.imshow(adjusted_brightness.eval())
plt.show()

下面两个是随机亮度调整后的图片 

图像数据处理(一)_Tensorflow_04

 

图像数据处理(一)_tensorflow_05

 

使用 image.random_contrast() 函数进行对比度的调整 . 

import matplotlib.pyplot as plt
import tensorflow as tf

image = tf.gfile.FastGFile("duck.png","rb").read()

with tf.Session() as sess :
img_after_decode = tf.image.decode_png(image)

# 函数原型 random_contrast(image,lower,upper,seed)
# 函数会在 [lower ,upper] 之间随机调整图像额对比度
# 参数不能是负数
adjusted_contrast = tf.image.random_contrast(img_after_decode,0.2,18,)
plt.imshow(adjusted_contrast.eval())
plt.show()

图像数据处理(一)_Image_06

 

可以用于对比度调整的函数还有 image.adjust_contrast() : 

# adjust_contrast() 函数原型

# adjust_contrast(images,contrast_factor) 

# 参数 contrase_factor 可为正或者为负, 正直增加对比度 , 负值降低对比度

    同时Tensorflow 还提供了用于调整色相的函数---- adjust_hue() 和 random_hue() ,以及调整饱和度的函数 ---adjust_saturation() 和 random_saturation() . 

#conding:utf-8
import matplotlib.pyplot as plt
import tensorflow as tf

image = tf.gfile.FastGFile("duck.jpg","rb").read()

with tf.Session() as sess:
img_after_decode = tf.image.decode_jpeg(image)

adjusted_hue = tf.image.adjust_hue(img_after_decode,0.1)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.3)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.6)
adjusted_hue = tf.image.adjust_hue(img_after_decode, 0.9)

plt.imshow(adjusted_hue.eval())
plt.show()

  注意,image.adjust_hue() 函数不适用于通道数为4的情况,因此对于 .png 格式的图像需要将其转换成 .jpeg/ .jpg 格式.

 再用 decode_jpeg() 进行解密。

转换: 

需要用到的库 :  pip install opencv-python

"""
先来说一下jpg图片和png图片的区别
jpg格式:是有损图片压缩类型,可用最少的磁盘空间得到较好的图像质量
png格式:不是压缩性,能保存透明等图

"""
from PIL import Image
import cv2 as cv
import os

def PNG_JPG(PngPath):
img = cv.imread(PngPath, 0)
w, h = img.shape[::-1]
infile = PngPath
outfile = os.path.splitext(infile)[0] + ".jpg"
img = Image.open(infile)
img = img.resize((int(w / 2), int(h / 2)), Image.ANTIALIAS)
try:
if len(img.split()) == 4:
# prevent IOError: cannot write mode RGBA as BMP
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
else:
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
return outfile
except Exception as e:
print("PNG转换JPG 错误", e)


if __name__ == '__main__':
PNG_JPG(r"duck.png")

下图展示的是将色相 +0.1 ,+0.3 ,+0.6 +0.9 后的结果。

图像数据处理(一)_Image_07

图像数据处理(一)_tensorflow_08

图像数据处理(一)_Tensorflow_09

 

图像数据处理(一)_Image_10

 

使用 image.random_hue() 函数可以实现随机色相调整。

#random_hue() 函数原型为 random_hue(image,max_delta) 

# 功能是在 [-max_delta ,max_delta] 范围内随机调整色相 

代码: 

import matplotlib.pyplot as plt
import tensorflow as tf

image = tf.gfile.FastGFile('duck.jpg','rb').read()

with tf.Session() as sess :
# 对图像解码
img_after_decode = tf.image.decode_jpeg(image)
# 将饱和度-6
adjusted_saturation = tf.image.adjust_saturation(img_after_decode,-6)
# 将饱和度 +6
# adjusted_saturation = tf.image.adjust_saturation(img_after_decode, 6)

plt.imshow(adjusted_saturation.eval())
plt.show()

                                                          饱和度 -6 的结果

图像数据处理(一)_Tensorflow_11

                                                              饱和度 +6 的结果

图像数据处理(一)_Image_12

 使用 image.random_staturation() 函数能够实现随机饱和度调整 

# 原型 random_staturation(image,lower,upper,seed)

举报

相关推荐

0 条评论