当前位置:   article > 正文

图像各通道特征提取_图像的通道特征提取

图像的通道特征提取

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/115966
推荐阅读
相关标签
  

闽ICP备14008679号