python使用TensorFlow进行图像处理的方法

 更新时间:2018年02月28日 10:21:23   作者:HackTheCode  
本篇文章主要介绍了使用TensorFlow进行图像处理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、图片的放大缩小

在使用TensorFlow进行图片的放大缩小时,有三种方式:

1、tf.image.resize_nearest_neighbor():临界点插值
2、tf.image.resize_bilinear():双线性插值
3、tf.image.resize_bicubic():双立方插值算法

下面是示例代码:

# encoding:utf-8
# 使用TensorFlow进行图片的放缩
import tensorflow as tf
import cv2
import numpy as np

# 读取图片
img = cv2.imread("1.jpg")
# 显示原始图片
cv2.imshow("resource", img)

h, w, depth = img.shape
img = np.expand_dims(img, 0)

# 临界点插值
nn_image = tf.image.resize_nearest_neighbor(img, size=[h+100, w+100])
nn_image = tf.squeeze(nn_image)
with tf.Session() as sess:
  # 运行 'init' op
  nn_image = sess.run(nn_image)
nn_image = np.uint8(nn_image)

# 双线性插值
bi_image = tf.image.resize_bilinear(img, size=[h+100, w+100])
bi_image = tf.squeeze(bi_image)
with tf.Session() as sess:
  # 运行 'init' op
  bi_image = sess.run(bi_image)
bi_image = np.uint8(bi_image)

# 双立方插值算法
bic_image = tf.image.resize_bicubic(img, size=[h+100, w+100])
bic_image = tf.squeeze(bic_image)
with tf.Session() as sess:
  # 运行 'init' op
  bic_image = sess.run(bic_image)
bic_image = np.uint8(bic_image)
# 显示结果图片
cv2.imshow("result_nn", nn_image)
cv2.imshow("result_bi", bi_image)
cv2.imshow("result_bic", bic_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

二、图片的亮度调整

在使用TensorFlow进行图片的亮度调整时,有两种方式:
1、tf.image.adjust_brightness():亮度的全局调整
2、tf.image.random_brightness():亮度的随机调整

下面是示例代码:

# encoding:utf-8
# 使用TensorFlow调整图片的亮度
import tensorflow as tf
import cv2
import numpy as np

# 读取图片
img = cv2.imread("1.jpg")
# 显示原始图片
cv2.imshow("resource", img)

img = np.expand_dims(img, 0)
# adjust_brightness
bright_img = tf.image.adjust_brightness(img, delta=.5)
bright_img = tf.squeeze(bright_img)
with tf.Session() as sess:
  # 运行 'init' op
  result = sess.run(bright_img)
result = np.uint8(result)

rand_image = tf.image.random_brightness(img, max_delta=.5)
rand_image = tf.squeeze(rand_image)
with tf.Session() as sess:
  # 运行 'init' op
  result2 = sess.run(rand_image)
result2 = np.uint8(result2)

cv2.imshow("result", result)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()

三、图片的对比度调整

在使用TensorFlow进行图片的对比度调整时,有两种方式:
1、tf.image.adjust_contrast():对比度的全局调整
2、tf.image.random_contrast():对比度的随机调整

代码与图片的亮度调整类似,这里就不赘述了。

四、图片的饱和度调整

在使用TensorFlow进行图片的饱和度调整时,使用下列函数:

tf.image.adjust_saturation() 

饱和度调整范围为0~5

下面示例代码:

# encoding:utf-8
# 使用TensorFlow调整图片的亮度
import tensorflow as tf
import cv2
import numpy as np

# 读取图片
img = cv2.imread("1.jpg")
# 显示原始图片
cv2.imshow("resource", img)

# 图像的饱和度调整
stand_img = tf.image.adjust_saturation(img, saturation_factor=2.4)
with tf.Session() as sess:
  # 运行 'init' op
  result = sess.run(stand_img)
result = np.uint8(result)

cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

五、图片的标准化

在使用TensorFlow对图像数据进行训练之前,常需要执行图像的标准化操作,它与归一化是有区别的,归一化不改变图像的直方图,标准化操作会改变图像的直方图。标准化操作使用如下函数:

tf.image.per_image_standardization() 

下面是示例代码:

# encoding:utf-8
# 使用TensorFlow调整图片的亮度
import tensorflow as tf
import cv2
import numpy as np

# 读取图片
img = cv2.imread("1.jpg")
# 显示原始图片
cv2.imshow("resource", img)

# 图像标准化操作
stand_img = tf.image.per_image_standardization(img)
with tf.Session() as sess:
  # 运行 'init' op
  result = sess.run(stand_img)
result = np.uint8(result)

cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

六、图像的色彩空间转化

使用TensorFlow进行图像的色彩空间转化,包括HSV、RGB、灰度色彩空间之间的转化,使用的函数如下所示:

tf.image.rgb_ to_hsv() 
tf.image.rgb_ to_grayscale() 
tf.image.hsv_ to_rgb() 

代码与图像的标准化操作的代码相似,这里不再赘述。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 在DOS界面如何运行python的py文件

    在DOS界面如何运行python的py文件

    这篇文章主要介绍了在DOS界面如何运行python的py文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • python虚拟环境virtualenv的安装与使用

    python虚拟环境virtualenv的安装与使用

    virtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件 2. 不同应用可以使用不同的套件版本 3. 套件升级不影响其他应用
    2017-09-09
  • python实现多线程及线程间通信的简单方法

    python实现多线程及线程间通信的简单方法

    这篇文章主要为大家介绍了python实现多线程及线程间通信的简单方法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 使用Python的数据可视化库Matplotlib实现折线图

    使用Python的数据可视化库Matplotlib实现折线图

    数据可视化是数据分析和探索中不可或缺的一环,本文将介绍如何使用Python中的数据可视化库Matplotlib,通过示例代码实现一个简单的折线图,感兴趣的同学可以参考阅读下
    2023-07-07
  • Django学习笔记之ORM基础教程

    Django学习笔记之ORM基础教程

    ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,下面这篇文章主要给大家介绍了关于Django学习笔记之ORM基础教程的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2018-03-03
  • Python中re模块常用方法总结分析

    Python中re模块常用方法总结分析

    这篇文章主要为大家介绍了Python中re模块常用方法,并对这些常用方法进行总结分析,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-09-09
  • python and or用法详解

    python and or用法详解

    这篇文章主要介绍了python and or用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • python中re.findall() 的使用案例

    python中re.findall() 的使用案例

    re.findall() 函数是 python 中正则表达式模块(re)的一个重要函数,它可以根据正则表达式搜索字符串,并返回匹配的字符串列表,这篇文章给大家介绍了python中re.findall() 的使用案例,感兴趣的朋友跟随小编一起看看吧
    2023-09-09
  • Python3+Django get/post请求实现教程详解

    Python3+Django get/post请求实现教程详解

    这篇文章主要介绍了Python3+Django get/post请求实现教程详解,需要的朋友可以参考下
    2021-02-02
  • 利用python做数据拟合详情

    利用python做数据拟合详情

    这篇文章主要介绍了利用python做数据拟合,下面文章围绕如何让利用python做数据拟合的相关资料展开详细内容,需要的朋友可以参考一下,希望对大家有所帮助
    2021-11-11

最新评论