图像各通道特征提取

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


  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值