1 环境

  • Ubuntu18.04
  • matplotlib
  • tensorflow
  • opencv

2 图像解码与显示

Tensorflow
图像解码即先读取原始图像文件,将图像转为字节格式(bytes),然后将字节进行解码,获取图像的矩阵数值.
opencv和matplotlib
图像解码通过读取原始图片,将图像转换为numpy.ndarray格式,即图像的矩阵数据,比Tensorflow快了一步.
本次主要利用Tensorflow对图像特征抽取matplotlib显示特征图.图像处理可参考:
Tensorflow图像预处理
(一)OpenCV之读写显示函数
python数据可视化:matplotlib绘图及处理图片

2.1 图像解码

import tensorflow as tf
import matplotlib.pyplot as plt

image_path = "./images/cat.jpg"
png = image_path.lower().endswith("png")
'''读取图像,转为bytes'''
image_bytes = tf.read_file(image_path)
'''图像解码'''
image_decode = tf.image.decode_png(image_bytes) if png else tf.image.decode_jpeg(image_bytes)
with tf.Session() as sess:
	print("image decode: {}".format(image_decode))
	image_value = sess.run(image_decode)

2.2 提取各通特征并显示

import tensorflow as tf
import matplotlib.pyplot as plt
'''字体设置'''
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/usr/share/fonts/truetype/arphic/ukai.ttc')

image_path = "./images/cat.jpg"
png = image_path.lower().endswith("png")

def image_channel_feature():
	'''读取图像,转为bytes'''
    image = tf.read_file(image_path)
    '''图像解码'''
    image = tf.image.decode_png(image, channels=3) if png else tf.image.decode_jpeg(image, channels=3)
    with tf.Session() as sess:
        image_value = sess.run(image)
        channel_r = image_value[:,:,0]
        channels_num = image_value.shape[2]
        plt.figure(figsize=(4, 6))
        '''channels name for show in image'''
        channels_name = ["RGB", "Red", "Green", "Blue"]
        
        for i in range(len(channels_name)):
        	'''image feature list'''
            channel_image = image_value if not i else image_value[:,:,i-1]
			'''plot multi image in one canvas'''
            plt.subplot(2,2,i+1).set_title("通道:{}".format(channels_name[i]), fontproperties=font)
            plt.subplots_adjust(left=0.05, right=0.8, hspace=0.4)
            plt.imshow(channel_image)
            plt.colorbar()
        '''save image'''
        plt.savefig("./image_features/RGB_feature.png", format="png")
        '''show image'''
        plt.show()

2.3 图像特征可视化

图像各通道特征提取

图2.1 原图与特征值图形

3 总结

(1) 原始图像基本通道有RGB三个,通过将图像处理成矩阵,即可获取对应通道的值,进行显示后计算,这也是图像处理的最基础部分.
(2) 神经网络中处理的图像层深更大,如64, 128, 256, 512,也是通过该方法进行提取,以检验图像在不同卷积层提取到的图像内容,如下即为卷积层提取的64层图像.
图像各通道特征提取

图3.1 VGG-conv1/conv1-2提取的图特征

[参考文献]
[1]https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html?highlight=pyplot%20subplot#matplotlib.pyplot.subplot
[2]https://blog.csdn.net/boyStray/article/details/80471028
[3]https://blog.csdn.net/claroja/article/details/70841382


相关文章: