Tensorflow study notes: image processing

#coding:utf-8
'''
图像处理
'''
import tensorflow as tf
import numpy as np
import cv2


# 读取图像文件方式
image_file = 'D:/Develop/DL/deeplearning-notes/datas/images/f2.jpg'
jpg = tf.read_file(image_file)
img = tf.image.decode_jpeg(jpg,channels=3)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

src = cv2.imread(image_file)
cv2.imshow('src',src)

# 缩放图像
h,w,depth = src.shape
src = np.expand_dims(src,0)
print(src.shape)
bilinear = tf.image.resize_bilinear(src,size=[h*2,w*2])
bilinear = tf.squeeze(bilinear)
result = sess.run(bilinear)
result = np.uint8(result)
cv2.imshow('bilinear',result)

# 亮度调整
brightness = tf.image.adjust_brightness(src,delta=0.5)
brightness = tf.squeeze(brightness)
result = sess.run(brightness)
result = np.uint8(result)
cv2.imshow('brightness',result)

# 对比度调整
contrast = tf.image.adjust_contrast(src,contrast_factor=2.2)
contrast = tf.squeeze(contrast)
result = sess.run(contrast)
result = np.uint8(result)
cv2.imshow('contrast',result)

# 饱和度调整
saturation = tf.image.adjust_saturation(src,saturation_factor=2.2)
saturation = tf.squeeze(saturation)
result = sess.run(saturation)
result = np.uint8(result)
cv2.imshow('saturation',result)

# Gamma校正
gamma = tf.image.adjust_gamma(src,gain=1.0,gamma=4.2)
gamma = tf.squeeze(gamma)
result = sess.run(gamma)
result = np.uint8(result)
cv2.imshow('gamma',result)

# 图像标准化
src = cv2.imread(image_file)
standard = tf.image.per_image_standardization(src)
standard = tf.squeeze(standard)
result = sess.run(standard)
result = np.uint8(result)
cv2.imshow('standard',result)

# 色彩空间转换
src = cv2.imread(image_file)
gray = tf.image.rgb_to_grayscale(src)
result = sess.run(gray)
result = np.uint8(result)
cv2.imshow('gray',result)

cv2.waitKey()
cv2.destroyAllWindows()

sess.close()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325033674&siteId=291194637