python使用TensorFlow進(jìn)行圖像處理的方法
一、圖片的放大縮小
在使用TensorFlow進(jìn)行圖片的放大縮小時,有三種方式:
1、tf.image.resize_nearest_neighbor():臨界點插值
2、tf.image.resize_bilinear():雙線性插值
3、tf.image.resize_bicubic():雙立方插值算法
下面是示例代碼:
# encoding:utf-8
# 使用TensorFlow進(jìn)行圖片的放縮
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)
# 顯示結(jié)果圖片
cv2.imshow("result_nn", nn_image)
cv2.imshow("result_bi", bi_image)
cv2.imshow("result_bic", bic_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
二、圖片的亮度調(diào)整
在使用TensorFlow進(jìn)行圖片的亮度調(diào)整時,有兩種方式:
1、tf.image.adjust_brightness():亮度的全局調(diào)整
2、tf.image.random_brightness():亮度的隨機調(diào)整
下面是示例代碼:
# encoding:utf-8
# 使用TensorFlow調(diào)整圖片的亮度
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()
三、圖片的對比度調(diào)整
在使用TensorFlow進(jìn)行圖片的對比度調(diào)整時,有兩種方式:
1、tf.image.adjust_contrast():對比度的全局調(diào)整
2、tf.image.random_contrast():對比度的隨機調(diào)整
代碼與圖片的亮度調(diào)整類似,這里就不贅述了。
四、圖片的飽和度調(diào)整
在使用TensorFlow進(jìn)行圖片的飽和度調(diào)整時,使用下列函數(shù):
tf.image.adjust_saturation()
飽和度調(diào)整范圍為0~5
下面示例代碼:
# encoding:utf-8
# 使用TensorFlow調(diào)整圖片的亮度
import tensorflow as tf
import cv2
import numpy as np
# 讀取圖片
img = cv2.imread("1.jpg")
# 顯示原始圖片
cv2.imshow("resource", img)
# 圖像的飽和度調(diào)整
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()
五、圖片的標(biāo)準(zhǔn)化
在使用TensorFlow對圖像數(shù)據(jù)進(jìn)行訓(xùn)練之前,常需要執(zhí)行圖像的標(biāo)準(zhǔn)化操作,它與歸一化是有區(qū)別的,歸一化不改變圖像的直方圖,標(biāo)準(zhǔn)化操作會改變圖像的直方圖。標(biāo)準(zhǔn)化操作使用如下函數(shù):
tf.image.per_image_standardization()
下面是示例代碼:
# encoding:utf-8
# 使用TensorFlow調(diào)整圖片的亮度
import tensorflow as tf
import cv2
import numpy as np
# 讀取圖片
img = cv2.imread("1.jpg")
# 顯示原始圖片
cv2.imshow("resource", img)
# 圖像標(biāo)準(zhǔn)化操作
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()
六、圖像的色彩空間轉(zhuǎn)化
使用TensorFlow進(jìn)行圖像的色彩空間轉(zhuǎn)化,包括HSV、RGB、灰度色彩空間之間的轉(zhuǎn)化,使用的函數(shù)如下所示:
tf.image.rgb_ to_hsv() tf.image.rgb_ to_grayscale() tf.image.hsv_ to_rgb()
代碼與圖像的標(biāo)準(zhǔn)化操作的代碼相似,這里不再贅述。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
django處理select下拉表單實例(從model到前端到post到form)
這篇文章主要介紹了django處理select下拉表單實例(從model到前端到post到form),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python 實現(xiàn)圖與圖之間的間距調(diào)整subplots_adjust
這篇文章主要介紹了python 實現(xiàn)圖與圖之間的間距調(diào)整subplots_adjust,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
Python爬取肯德基官網(wǎng)ajax的post請求實現(xiàn)過程
這篇文章主要介紹了Python爬取肯德基官網(wǎng)ajax的post請求實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家學(xué)有所得,多多進(jìn)步2021-10-10
Python反向傳播實現(xiàn)線性回歸步驟詳細(xì)講解
回歸是監(jiān)督學(xué)習(xí)的一個重要問題,回歸用于預(yù)測輸入變量和輸出變量之間的關(guān)系,特別是當(dāng)輸入變量的值發(fā)生變化時,輸出變量的值也隨之發(fā)生變化?;貧w模型正是表示從輸入變量到輸出變量之間映射的函數(shù)2022-10-10
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07
使用mypy對python程序進(jìn)行靜態(tài)檢查
大家好,本篇文章主要講的是使用mypy對python程序進(jìn)行靜態(tài)檢查,感興趣的同學(xué)快來看一看吧,對你有幫助的話記得收藏一下哦2021-11-11

