美文网首页
7.2.1 图像处理函数

7.2.1 图像处理函数

作者: 醉乡梦浮生 | 来源:发表于2018-07-04 16:21 被阅读0次

读取图片,显示图片

import matplotlib.pyplot as plt
import tensorflow as tf   
import numpy as np


image_raw_data = tf.gfile.FastGFile("../../datasets/cat.jpg",'rb').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    
    # 输出解码之后的三维矩阵。
    print(img_data.eval())
    img_data.set_shape([1797, 2673, 3])
    print(img_data.get_shape())

    plt.imshow(img_data.eval())
    plt.show()

调整图片大小

tf.image.resize_images 函数调整图片大小。第一个参数是原始图片,第二个和第三个是调整后图片大小,method参数给出了调整图片大小算法。

with tf.Session() as sess:
    # 如果直接以0-255范围的整数数据输入resize_images,那么输出将是0-255之间的实数,
    # 不利于后续处理。本书建议在调整图片大小前,先将图片转为0-1范围的实数。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    # tf.image.resize_images 函数调整图片大小。第一个参数是原始图片,第二个和第三个是调整后图片大小,method参数给出了调整图片大小算法。
    resized = tf.image.resize_images(image_float, [300, 300], method=0)
    
    plt.imshow(resized.eval())
    plt.show()

表 tf.image.resize_images函数method参数取值和对应的调整算法

Method取值 图像大小调整算法
0 双线性插值法
1 最近邻法
2 双三次插值法
3 面积插值法

裁剪和填充图片

tf.image.resize_image_with_crop_or_pad调整图片大小,第一个参数是原始图片,后面两个参数是调整后的目标图片大小。如果原始图片尺寸大于目标图片,函数会自动截取原始图片中居中的部分。如果目标图片大于原始图片,这个函数会自动在原始图片的四种填充0.

with tf.Session() as sess:    
    # 自动裁剪
    croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
    # 自动填充
    padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()

按照比例裁剪图片

通过tf.image.central_crop函数可以按照比例裁剪图片。第一个参数是原始图片,第二个是调整比例,这比例需要时一个 (0,1]的实数。

with tf.Session() as sess:   
    central_cropped = tf.image.central_crop(img_data, 0.5)
    plt.imshow(central_cropped.eval())
    plt.show()

翻转图片

with tf.Session() as sess: 
    # 上下翻转
    flipped1 = tf.image.flip_up_down(img_data)
    # 左右翻转
    flipped2 = tf.image.flip_left_right(img_data)
    
    #对角线翻转
    transposed = tf.image.transpose_image(img_data)
    
    # 以一定概率上下翻转图片。
    flipped = tf.image.random_flip_up_down(img_data)

    # 以一定概率左右翻转图片。
    flipped = tf.image.random_flip_left_right(img_data)

图片色彩调整

调整亮度,对比度,饱和度,色相,图像标准化过程

with tf.Session() as sess:
    # 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    
    # 将图片的亮度-0.5。
    adjusted = tf.image.adjust_brightness(image_float, -0.5)
    
    # 将图片的亮度+0.5
    adjusted = tf.image.adjust_brightness(image_float, 0.5)
    
    # 在[-max_delta, max_delta)的范围随机调整图片的亮度。
    adjusted = tf.image.random_brightness(image_float, max_delta=0.5)
    
    # 将图片的对比度0.5
    adjusted = tf.image.adjust_contrast(image_float, 0.5)
    
    # 将图片的对比度+5
    adjusted = tf.image.adjust_contrast(image_float, 5)
    
    # 在[lower, upper]的范围随机调整图的对比度。
    adjusted = tf.image.random_contrast(image_float, lower, upper)

    # 将图片的色相0.1。
    adjusted = tf.image.adjust_hue(image_float, 0.1)
    #adjusted = tf.image.adjust_hue(image_float, 0.3)
    #adjusted = tf.image.adjust_hue(image_float, 0.6)
    #adjusted = tf.image.adjust_hue(image_float, 0.9)
    
    # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
    adjusted = tf.image.random_hue(image_float, max_delta)
    
    # 将图片的饱和度-5。
    adjusted = tf.image.adjust_saturation(image_float, -5)
    # 将图片的饱和度+5。
    adjusted = tf.image.adjust_saturation(image_float, 5)
    # 在[lower, upper]的范围随机调整图的饱和度。
    adjusted = tf.image.random_saturation(image_float, lower, upper)
    
    # 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
    adjusted = tf.image.per_image_standardization(image_float)

    # 在最终输出前,将实数取值截取到0-1范围内。
    adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
    plt.imshow(adjusted.eval())
    plt.show()

添加标注框并裁剪

with tf.Session() as sess:
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    
    # sample_distorted_bounding_box要求输入图片必须是实数类型
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4)
    
    #截取后的图片
    distorted_image = tf.slice(image_float, begin, size)
    plt.imshow(distorted_image.eval())
    plt.show()
    
    
    # 在原图上用标注框画出截取的范围。由于原图的分辨率较大(2673x1797),生成的标注框
    # 在Jupyter Notebook上通常因边框过细而无法分辨,这里为了演示方便先缩小分辨率。
    image_small = tf.image.resize_images(image_float, [180, 267], method=0)
    batched_img = tf.expand_dims(image_small, 0)
    image_with_box = tf.image.draw_bounding_boxes(batched_img, bbox_for_draw)
    print(bbox_for_draw)
    plt.imshow(image_with_box[0].eval())
    plt.show()

相关文章

  • 7.2.1 图像处理函数

    读取图片,显示图片 调整图片大小 tf.image.resize_images 函数调整图片大小。第一个参数是原始...

  • regionprops

    Matlab图像处理函数:regionprops 这里给出在Matlab图像处理工具箱中非常重要的一个图像分析函数...

  • HoughCircles(霍夫变换圆检测)

    概念 HoughCircles函数 效果图对比 ●源图像 ●处理后图像 函数讲解 ●函数原型○c++ ○Andro...

  • PHP学习-生成图片

    PHP手册:GD 和图像处理 函数

  • Canny(边缘提取)

    概念 Canny函数内部使用canny算法对输入图像进行边缘检测。 效果图对比 ●源图像 ●处理后图像 函数讲解 ...

  • demo01

    图像处理基本操作 1. 读取图像 opencv提供了imread函数来读取图像,该函数有两个参数,filepath...

  • Sobel(边缘提取)

    概念 Sobel算子 效果图对比 ●源图像 ●处理后图像 函数讲解 ●函数原型○c++ ○Android ●参数解...

  • Matlab图像操作相关

    图像显示 Matlab中image、imagesc和imshow函数用法解析 图像几何处理

  • 神经网络02

    softmax 函数 显示图像 神经网络推理处理

  • GaussianBlur(高斯滤波)

    概念 高斯滤波 效果图对比 ●源图像 ●处理后图像 函数讲解 ●函数原型○c++ ○Android ●参数解释○s...

网友评论

      本文标题:7.2.1 图像处理函数

      本文链接:https://www.haomeiwen.com/subject/qxgeuftx.html