一、圖片的放大縮小
在使用TensorFlow進行圖片的放大縮小時,有三種方式:
1、tf.image.resize_nearest_neighbor():臨界點插值
2、tf.image.resize_bilinear():雙線性插值
3、tf.image.resize_bicubic():雙立方插值算法
下面是示例代碼:
# encoding:utf-8# 使用TensorFlow進行圖片的放縮import tensorflow as tfimport cv2import numpy as np# 讀取圖片img = cv2.imread("1.jpg")# 顯示原始圖片cv2.imshow("resource", img)h, w, depth = img.shapeimg = 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 tfimport cv2import numpy as np# 讀取圖片img = cv2.imread("1.jpg")# 顯示原始圖片cv2.imshow("resource", img)img = np.expand_dims(img, 0)# adjust_brightnessbright_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
新聞熱點
疑難解答