Tensorflow深度學習之十二:基礎圖像處理之二
首先放出原始圖像:
1、圖像的翻轉
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # 上下翻轉圖像 up_and_down = tf.image.flip_up_down(image_data) cv2Show("up and down",up_and_down) # 左右翻轉圖像 left_and_right = tf.image.flip_left_right(image_data) cv2Show("left and right", left_and_right) # 沿對角線翻轉圖像 transposed = tf.image.transpose_image(image_data) cv2Show("transposed image", transposed) # 以一定概率上下翻轉圖像 random_up_and_down = tf.image.random_flip_up_down(image_data) cv2Show("random up and down", random_up_and_down) # 以一定概率左右翻轉圖像 random_left_and_right = tf.image.random_flip_left_right(image_data) cv2Show("random left and right", random_left_and_right) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
程序運行結果如下:
注:由於個人的顯示器限制,這里只截取了部分的對角線翻轉的圖像。
注:可以看到這里按照一定的概率翻轉,只有上下進行了翻轉,而左右並沒有翻轉。
2、圖像的亮度調整
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # 將圖片的亮度-0.5 adjusted1 = tf.image.adjust_brightness(image_data, -0.5) cv2Show("brightness -0.5", adjusted1) # 將圖片的亮度+0.5 adjusted2 = tf.image.adjust_brightness(image_data, 0.5) cv2Show("brightness +0.5",adjusted2) # 隨機調整圖像的亮度: # random_brightness(image, max_delta, seed=None) # image:待調整的圖像 # max_delta:在[-max_delte,max_delte)的范圍隨機調整圖像的亮度 # seed:隨機數種子 adjusted3 = tf.image.random_brightness(image_data, 0.3) cv2Show("random brightness", adjusted3) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序運行如下:
3、調整圖像的對比度
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # 將圖片的對比度-5 adjusted1 = tf.image.adjust_contrast(image_data, -5) cv2Show("contrast -5", adjusted1) # 將圖片的對比度+5 adjusted2 = tf.image.adjust_contrast(image_data, 5) cv2Show("contrast +5",adjusted2) # 隨機調整圖像的對比度: # random_contrast(image, lower, upper, seed=None) # image:待調整的圖像 # lower,upper:在[lower,upper]的范圍隨機調整圖像的對比度。lower非負。 # seed:隨機數種子 adjusted3 = tf.image.random_contrast(image_data, 1, 9) cv2Show("random contrast", adjusted3) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序運行如下:
4、調整圖像的色相
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # adjust_hue(image, delta, name=None) # delte的范圍:[-1,1] # 將圖片的色相+0.1 adjusted1 = tf.image.adjust_hue(image_data, 0.1) cv2Show("hue +0.1", adjusted1) # 將圖片的色相+0.3 adjusted2 = tf.image.adjust_hue(image_data, 0.3) cv2Show("hue +0.3", adjusted2) # 將圖片的色相+0.6 adjusted3 = tf.image.adjust_hue(image_data, 0.6) cv2Show("hue +0.6", adjusted3) # 將圖片的色相+0.9 adjusted4 = tf.image.adjust_hue(image_data, 0.9) cv2Show("hue +0.9", adjusted4) # 隨機調整圖像的色相: # random_hue(image, max_delta, seed=None) # image:待調整的圖像 # max_delta:在[-max_delta,max_delta]的范圍隨機調整圖像的色相。max_delta的范圍[0,0.5]。 # seed:隨機數種子 adjusted5 = tf.image.random_hue(image_data,0.4) cv2Show("random hue", adjusted5) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
程序運行結果如下:
5、調整圖像的飽和度
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # 將圖片的飽和度-5 adjusted1 = tf.image.adjust_saturation(image_data, -5) cv2Show("saturation -5", adjusted1) # 將圖片的飽和度+5 adjusted2 = tf.image.adjust_saturation(image_data, 5) cv2Show("saturation +5", adjusted2) # 隨機調整圖像的飽和度: # random_saturation(image, lower, upper, seed=None) # image:待調整的圖像 # lower,upper:在[lower,upper]的范圍隨機調整圖像的飽和度。lower非負。 # seed:隨機數種子 adjusted3 = tf.image.random_saturation(image_data, 1, 9) cv2Show("random saturation", adjusted3) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
程序運行結果如下:
6、圖像的標准化
import tensorflow as tf import cv2 # 這里定義一個tensorflow讀取的圖片格式轉換為opencv讀取的圖片格式的函數 # 請注意: # 在tensorflow中,一個像素點的顏色順序是R,G,B。 # 在opencv中,一個像素點的顏色順序是B,G,R。 # 因此,我們循環遍歷每一個像素點,將第0位的顏色和第2位的顏色數值換一下即可。 # 第一個參數name:將要顯示的窗口名稱。 # 第二個參數image:儲存圖片信息的一個tensor。 def cv2Show(name="", image=None): # 獲取矩陣信息 np = image.eval() # 獲取行數列數 row, col = len(np),len(np[1]) # 兩重循環遍歷 for i in range(row): for j in range(col): # 交換數值 tmp = np[i][j][0] np[i][j][0] = np[i][j][2] np[i][j][2] = tmp # 顯示圖片 cv2.imshow(name,np) pass # tensorflow會話 with tf.Session() as sess: # 以二進制的方式讀取圖片。 image_raw_data = tf.gfile.FastGFile("bus.jpg", "rb").read() # 按照jpeg的格式解碼圖片。 image_data = tf.image.decode_jpeg(image_raw_data) # 顯示原圖片。 cv2Show("Read by Tensorflow+Dispalyed by Opencv",image_data) # 將代表一張圖像的三維矩陣中的數字均值變成0,方差變為1。 adjusted = tf.image.per_image_standardization(image_data) cv2Show("image_standardization", adjusted) cv2.waitKey()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
程序運行結果如下: