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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

二、图片的亮度调整

在使用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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

三、图片的对比度调整

在使用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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

五、图片的标准化

在使用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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

六、图像的色彩空间转化

使用TensorFlow进行图像的色彩空间转化,包括HSV、RGB、灰度色彩空间之间的转化,使用的函数如下所示:
tf.image.rgb_ to_hsv()
tf.image.rgb_ to_grayscale()
tf.image.hsv_ to_rgb()
代码与图像的标准化操作的代码相似,这里不再赘述。

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/github_39611196">
                <img src="https://profile.csdnimg.cn/F/8/0/3_github_39611196" class="avatar_pic" username="github_39611196">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/github_39611196" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">cchangcs</a></span>
                                        </div>
                <div class="text"><span>发布了271 篇原创文章</span> · <span>获赞 192</span> · <span>访问量 43万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://bbs.csdn.net/forums/p-github_39611196" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-messageboard">他的留言板
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值