一、图片的放大缩小
在使用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
新闻热点
疑难解答