更多的基本的API請參看TensorFlow中文社區:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html
下面是實驗的代碼,可以參考,對應的圖片是輸出的結果:
import tensorflow as tf import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np %matplotlib inline path = '/home/ubuntu-mm/TensorFlow/Learning/D-TestJupyter/images/Train/Dogs.jpg' file_queue = tf.train.string_input_producer([path]) image_reader = tf.WholeFileReader() _, image = image_reader.read(file_queue) image = tf.image.decode_jpeg(image) with tf.Session() as sess: coord = tf.train.Coordinator() #協同啟動的線程 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #啟動線程運行隊列 sess.run(image) coord.request_stop() #停止所有的線程 coord.join(threads) image_uint8 = tf.image.convert_image_dtype(image, dtype = tf.uint8) plt.figure(1) plt.imshow(image_uint8.eval()) print image_uint8.get_shape() resize_image1 = tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #修改圖片的尺寸 resize_image2 = tf.image.resize_images(image_uint8,[400,300],method=1) #修改圖片的尺寸 1代表的就是NEAREST_NEIGHBOR的方法 central_crop = tf.image.central_crop(image_uint8, 0.6) #從圖片中心開始裁剪圖片,裁剪比例為60% bounding_crop = tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100) #設定裁剪的起始位置和終止位置進行裁剪 pad = tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105) #設定邊緣對圖像的邊緣進行填充(填0) flip1 = tf.image.flip_left_right(resize_image1) #左右翻轉圖片 flip2 = tf.image.flip_up_down(flip1) #上下翻轉圖片 adjust_brightness = tf.image.adjust_brightness(resize_image1, 0.2) #調節圖像的亮度為原來的20% adjust_saturation = tf.image.adjust_saturation(resize_image1, 0.4) #調節圖像的飽和度為原來的40% adjust_hue = tf.image.adjust_hue(resize_image1, 0.7) #調節原來的H(灰度)為原來的70% image_float = tf.cast(resize_image1, dtype=tf.float32) gray = tf.image.rgb_to_grayscale(image_float) #對圖像的類型進行轉換rgb-grayscale hsv = tf.image.rgb_to_hsv(image_float) #對圖像進行hsv轉換rgb-hsv imag_gray = tf.image.convert_image_dtype(gray, tf.uint8) imag_hsv = tf.image.convert_image_dtype(hsv, tf.uint8) sess.run([flip1, flip2]) plt.figure(2) plt.imshow(resize_image1.eval()) plt.figure(3) plt.imshow(resize_image2.eval()) plt.figure(4) plt.imshow(central_crop.eval()) plt.figure(5) plt.imshow(bounding_crop.eval()) plt.figure(6) plt.imshow(pad.eval()) plt.figure(7) plt.imshow(flip2.eval()) plt.figure(8) plt.imshow(adjust_brightness.eval()) plt.figure(9) plt.imshow(adjust_saturation.eval()) plt.figure(10) plt.imshow(adjust_hue.eval()) plt.figure(1) plt.imshow(imag_hsv.eval(), cmap=cm.hsv)
原圖 改變尺寸 改變尺寸 圖像中心裁剪
圖像邊緣裁剪 圖像邊緣補0 圖像水平垂直翻轉
圖像亮度度調節 圖像飽和度變換 圖像弧度調節H 圖像HSV顯示
相關函數介紹:
1、tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
函數的作用是修改圖像的尺寸(縮放放大的形式),第一個參數是原始圖像,第二個參數是輸出圖像的大小,第三個參數是縮放或放大的方法。
2、tf.image.central_crop(image_uint8, 0.6)
函數的功能是裁剪圖像,裁剪的中心式圖像的中心位置,第一個參數是原始圖像,第二個參數是裁剪的比例。
3、tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100)
函數的功能是按照輸入的參數的邊緣來裁剪圖像,第一個參數是原始的圖像,第二個參數是裁剪的y軸起始位置,第三個是x軸起始位置,第4個參數和第5個參數是輸出圖像的尺寸大小。
4、tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105)
函數的功能是擴充圖像的邊緣,對圖像的邊緣進行補零的操作,第一個參數是原始圖像,第二個參數和第三個參數是輸出圖像在原圖上的起始位置,第4和5個參數是輸出圖像的大小,當輸出圖像超出了原始圖像的大小時,就會將超出的部分進行補零的操作。
5、tf.image.flip_left_right(resize_image1)
函數的功能是對圖像進行水平方向的反轉,參數1是原始圖像。
6、tf.image.flip_up_down(flip1)
函數的功能是對圖像進行垂直方向的反轉,參數1是原始圖像。
7、tf.image.adjust_brightness(resize_image1, 0.2)
函數的功能是調節原始圖像的亮度值,第一個參數是原始圖像,第二個參數是調節的比例。
8、tf.image.adjust_saturation(resize_image1, 0.4)
函數的功能是調節原始圖像的飽和度,第一個參數是原始圖像,第二個參數是調節的比例。
9、tf.image.adjust_hue(resize_image1, 0.7)
函數的功能是調節圖像的灰度值(Hue),參數1是原始圖像,參數2是調節的比例。
10、tf.image.rgb_to_grayscale(image_float)
函數的功能是間輸入的rgb格式的圖像轉換成grayscale的灰度圖像,參數1是輸入的原始圖像。(注意輸入圖像的格式需要時浮點形式的float)
11、tf.image.rgb_to_hsv(image_float)
函數的功能是間輸入的圖像轉換成為hsv格式的圖像,參數1是輸入圖像,輸入的格式需要時浮點型的。
完!