|
02、利用tensorflow对图片进行卷积(轮廓信息提取)和池化(膨胀)
- import matplotlib.pyplot as plt
- import tensorflow as tf
- '''
- 利用tensorflow对图片进行卷积(轮廓信息提取)和池化(膨胀)
- '''
- def show_image_tensor(image_tensor):
- # 要求:使用交互式会话
- # 获取图像tensor对象对应的image对象,image对象时一个[h,w,c]
- # print(image_tensor)
- image = image_tensor.eval()
- # print(image)
- print("显示时图像大小为:{}".format(image.shape))
- if len(image.shape) == 3 and image.shape[2] == 1:
- # 黑白图像
- plt.imshow(image[:, :, 0], cmap='Greys_r')
- plt.show()
- elif len(image.shape) == 3:
- # 彩色图像
- plt.imshow(image)
- plt.show()
- sess = tf.InteractiveSession()
- path = '0.jpg'
- # 读取图像数据并转换为[height, weight, channels]的格式 HWC
- img = tf.image.decode_jpeg(tf.read_file(path), 3)
- # 图像重置大小 nearest_neighbor
- img = tf.image.resize_images(img, size=(300, 300), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
- # 将图像转换为黑白的图像
- img = tf.image.rgb_to_grayscale(img)
- old_img = img
- # 边缘/轮廓信息提取 多次卷积 滤波器的作用 filter
- img = tf.cast(img, dtype=tf.float32)
- # NHWC
- img = tf.expand_dims(img, 0)
- show_image_tensor(img[0])
- for i in range(1):
- sobel_gx = [-1, 0, +1, 0, 0, 0, -1, 0, +1]
- # 卷积核是常量
- conv2d_filter = tf.constant(value=sobel_gx, dtype=tf.float32, shape=[3, 3, 1, 1])
- # [filter_height, filter_width, in_channels, out_channels]
- img = tf.nn.conv2d(img, filter=conv2d_filter, strides=[1, 1, 1, 1], padding='SAME')
- show_image_tensor(img[0])
- # 二值化,小于等于170的设置为0 黑色,大于170设置为255 白色
- img = tf.where(tf.less_equal(img, 170), tf.zeros_like(img), tf.ones_like(img) * 255)
- # 将白色的区域(车牌区域就是白色的),
- # 扩大/膨胀的意思是:将这些白色区域的周边也变成白色的 ==> 只需要做一个maxpool
- # 前面已经二值化了 0 或 255(白色) 最大池化:会将白色的周边也变白色 因为255最大
- # 多做几次最大池化:膨胀得越大
- # 注意池化核大小的变化:(2, 3) 车牌的区域也是长方形的
- # img = tf.expand_dims(img, 0)
- show_image_tensor(img[0])
- # 膨胀
- for i in range(2):
- # 池化核的大小:2*3
- img = tf.nn.max_pool(img, ksize=(1, 2, 3, 1), strides=(1, 1, 1, 1), padding='SAME')
- show_image_tensor(img[0])
- # 修图:将有一些膨胀不太好的地方,还原成黑色
- # 0 255 变成 0 -255 前面已经二值化了
- # 池化核大小:(3, 1) 在抑制纵轴方向的膨胀呢
- img = tf.nn.max_pool(img * -1, ksize=(1, 3, 1, 1), strides=(1, 1, 1, 1), padding='SAME')
- img = img * -1
- show_image_tensor(img[0])
- # 继续膨胀
- for i in range(5):
- # 池化核大小:(1, 5) 加码横轴方向的膨胀
- img = tf.nn.max_pool(img, ksize=(1, 1, 5, 1), strides=(1, 1, 1, 1), padding='SAME')
- img = img[0]
- print(img.get_shape())
- show_image_tensor(img)
复制代码
|
|