python使用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進行圖片的飽和度調整時,使用下列函數(shù):
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對圖像數(shù)據(jù)進行訓練之前,常需要執(zhí)行圖像的標準化操作,它與歸一化是有區(qū)別的,歸一化不改變圖像的直方圖,標準化操作會改變圖像的直方圖。標準化操作使用如下函數(shù):
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、灰度色彩空間之間的轉化,使用的函數(shù)如下所示:
tf.image.rgb_ to_hsv() tf.image.rgb_ to_grayscale() tf.image.hsv_ to_rgb()
代碼與圖像的標準化操作的代碼相似,這里不再贅述。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實例教程
wxPython是一款集成了Python的圖形化類庫的工具,而py2exe是一款將Python程序轉換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來學習Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實例教程:2016-07-07
Facebook開源一站式服務python時序利器Kats詳解
這篇文章主要為答案及介紹了Facebook開源一站式服務python時序利器Kats的功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11
python實現(xiàn)企業(yè)微信定時發(fā)送文本消息的示例代碼
這篇文章主要介紹了python實現(xiàn)企業(yè)微信定時發(fā)送文本消息的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

