首页 > 其他分享 >TensorFlow图像处理函数

TensorFlow图像处理函数

时间:2023-02-06 18:05:22浏览次数:40  
标签:adjusted 函数 img image 图像处理 tf TensorFlow data 255


1. 读取图片

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

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

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

输出结果:

[[[  0   0   0 255]
  [  0   0   0 255]
  [  0   0   0 255]
  ...
  [  0   0   0 255]
  [  0   0   0 255]
  [  0   0   0 255]]


 [[  0   0   0 255]
  [ 31  31  26 255]
  [ 39  39  34 255]
  ...
  [ 20  30   3 255]
  [ 21  30   5 255]
  [ 21  30   8 255]]


 [[  0   0   0 255]
  [131 130 111 255]
  [164 162 139 255]
  ...
  [ 90 126  18 255]
  [ 93 130  19 255]
  [ 93 129  34 255]]


 ...


 [[  0   0   0 255]
  [162 160 143 255]
  [203 196 177 255]
  ...
  [181 145 170 255]
  [137  98 133 255]
  [116  76 111 255]]


 [[  0   0   0 255]
  [164 160 143 255]
  [204 197 178 255]
  ...
  [ 95  62  70 255]
  [ 91  67  69 255]
  [119 100  94 255]]


 [[  0   0   0 255]
  [131 127 114 255]
  [163 159 143 255]
  ...
  [ 89  67  45 255]
  [ 86  66  45 255]
  [ 86  66  43 255]]]

(1797, 2673, 3) #shape

2. 打印图片


with tf.Session() as sess:
plt.imshow(image_data.eval())
plt.show()

TensorFlow图像处理函数_tensorflow

3. 重新调整图片大小


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

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

with tf.Session() as sess:
image_data = tf.image.decode_png(image_raw_data)
'''
resize_images第三个参数
0 双线性插值法
1 最近邻居法
2 双三次插值法
3 面积插值法

'''
resized = tf.image.resize_images(image_data, [300, 300], method=0)

# TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
print("Digital type: ", resized.dtype)
cat = np.asarray(resized.eval(), dtype='uint8')
# tf.image.convert_image_dtype(rgb_image, tf.float32)
plt.imshow(cat)
plt.show()


TensorFlow图像处理函数_tensorflow_02

TensorFlow图像处理函数_插值法_03

4. 裁剪和填充图片


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

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

with tf.Session() as sess:
image_data = tf.image.decode_png(image_raw_data)

croped = tf.image.resize_image_with_crop_or_pad(image_data, 200, 200)
padded = tf.image.resize_image_with_crop_or_pad(image_data, 3000, 3000)
plt.imshow(croped.eval())
plt.show()
plt.imshow(padded.eval())
plt.show()

TensorFlow图像处理函数_tensorflow_04

TensorFlow图像处理函数_tensorflow_05

TensorFlow图像处理函数_tensorflow_06


6. 翻转图片


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

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

with tf.Session() as sess:
image_data = tf.image.decode_png(image_raw_data)

# 上下翻转
# flipped1 = tf.image.flip_up_down(img_data)
# 左右翻转
# flipped2 = tf.image.flip_left_right(img_data)

# 对角线翻转
transposed = tf.image.transpose_image(image_data)
plt.imshow(transposed.eval())
plt.show()

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


TensorFlow图像处理函数_sed_07


7. 图片色彩调整


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

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

with tf.Session() as sess:
img_data = tf.image.decode_png(image_raw_data)

# 将图片的亮度-0.5。
# adjusted = tf.image.adjust_brightness(img_data, -0.5)

# 将图片的亮度-0.5
# adjusted = tf.image.adjust_brightness(img_data, 0.5)

# 在[-max_delta, max_delta)的范围随机调整图片的亮度。
adjusted = tf.image.random_brightness(img_data, max_delta=0.5)

# 将图片的对比度-5
# adjusted = tf.image.adjust_contrast(img_data, -5)

# 将图片的对比度+5
# adjusted = tf.image.adjust_contrast(img_data, 5)

# 在[lower, upper]的范围随机调整图的对比度。
# adjusted = tf.image.random_contrast(img_data, lower, upper)

plt.imshow(adjusted.eval())
plt.show()


TensorFlow图像处理函数_sed_08


8. 添加色相和饱和度


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

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

with tf.Session() as sess:

img_data = tf.image.decode_jpeg(image_raw_data)

print(img_data.get_shape())
#img_data.set_shape([1797, 2673, 3])

#adjusted = tf.image.adjust_hue(img_data, 0.1)
adjusted = tf.image.adjust_hue(img_data, 0.3)
#djusted = tf.image.adjust_hue(img_data, 0.6)
# adjusted = tf.image.adjust_hue(img_data, 0.9)

# 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
# adjusted = tf.image.random_hue(image, max_delta)

# 将图片的饱和度-5。
# adjusted = tf.image.adjust_saturation(img_data, -5)
# 将图片的饱和度+5。
# adjusted = tf.image.adjust_saturation(img_data, 5)
# 在[lower, upper]的范围随机调整图的饱和度。
# adjusted = tf.image.random_saturation(img_data, 1, 2)

# 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
#adjusted = tf.image.per_image_whitening(img_data)

plt.imshow(adjusted.eval())
plt.show()


TensorFlow图像处理函数_sed_09


9. 添加标注框并裁减。










标签:adjusted,函数,img,image,图像处理,tf,TensorFlow,data,255
From: https://blog.51cto.com/u_15955675/6040031

相关文章

  • PLC利用函数块连接MQTT订阅消息(一)
    在亿佰特介绍了西门子PLC如何通过函数块连接MQTT服务器和发布消息,本文为大家介绍如何通过函数与函数块实现MQTT云消息的订阅,直接切入重点。一、飞燕物联网平台配置这里......
  • PLC利用函数块连接MQTT订阅消息(一)
    在亿佰特介绍了西门子PLC如何通过函数块连接MQTT服务器和发布消息,本文为大家介绍如何通过函数与函数块实现MQTT云消息的订阅,直接切入重点。一、飞燕物联网平台配置这里的配......
  • mysql concat函数的用法
    mysql中的这个函数非常强大,可以对查出的参数进行拼接,其实这个方法在java中也有api可以进行调用。那么什么时候进行使用呢?例如,你老大叫你做一个数据库的数据采集,需要整理成......
  • tf.split()函数的用法
    fromPILimportImageimportnumpyasnpimporttensorflowastf'''split对维度进行分割tf.split(data,数据图片(300*600*3)......
  • 窗口函数的5种方法总结
    窗口函数也称为OLAP函数,是对一组值进行操作,但是在对一组值操作时,又不需要使用groupby子句,去达成分组计算的逻辑实现。而且还可以达成一条数据被分到多个组里去重复计算。当......
  • 一文详解TensorFlow模型迁移及模型训练实操步骤
    摘要:本文介绍将TensorFlow网络模型迁移到昇腾AI平台,并执行训练的全流程。然后以TensorFlow1.15训练脚本为例,详细介绍了自动迁移、手工迁移以及模型训练的操作步骤。本文分......
  • 一文详解TensorFlow模型迁移及模型训练实操步骤
    摘要:本文介绍将TensorFlow网络模型迁移到昇腾AI平台,并执行训练的全流程。然后以TensorFlow1.15训练脚本为例,详细介绍了自动迁移、手工迁移以及模型训练的操作步骤。本文......
  • 【☀️C语言函数传参の结构体数组篇☀️】
    背景介绍C语言中函数参数传递的两种方式(“引用传递做函数参数”是C++的特性,C语言不支持。)(1)传值,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形......
  • C++内联函数:那时我还太年轻,并不知道使用inline带来的效率,早已在暗中标好了价格
    一、前言关键字​​inline​​​是C++相对于C语言的又一个扩充,在函数的声明或定义、函数的返回类型前加上关键字​​inline​​​,即可把函数指定为内联函数从而提升程序运行......
  • 第三章函数
    一、函数定义<1>函数定义的语法形式类型标识符函数名(形式参数表)/形参表<type1>name1,<type2>name2,…是被初始化的内部变量,寿命和可见性仅限于函数内部  类型标识符即......