TensorFlow與OpenCV,讀取圖片,進行簡單操作並顯示
1 OpenCV讀入圖片,使用tf.Variable初始化為tensor,載入到tensorflow對圖片進行轉置操作,然後opencv顯示轉置後的結果
import tensorflow as tfimport cv2file_path = "/home/lei/Desktop/"filename = "MarshOrchid.jpg"image = cv2.imread(filename, 1)cv2.namedWindow('image', 0)cv2.imshow('image', image)# Create a TensorFlow Variablex = tf.Variable(image, name='x')model = tf.initialize_all_variables()with tf.Session() as session: x = tf.transpose(x, perm=[1, 0, 2]) session.run(model) result = session.run(x)cv2.namedWindow('result', 0)cv2.imshow('result', result)cv2.waitKey(0)
2 OpenCV讀入圖片,使用tf.placeholder符號變數載入到tensorflow裡,然後tensorflow對圖片進行剪下操作,最後opencv顯示轉置後的結果
import tensorflow as tfimport cv2# First, load the image againfilename = "MarshOrchid.jpg"raw_image_data = cv2.imread(filename)image = tf.placeholder("uint8", [None, None, 3])slice = tf.slice(image, [1000, 0, 0], [3000, -1, -1])with tf.Session() as session: result = session.run(slice, feed_dict={image: raw_image_data}) print(result.shape)cv2.namedWindow('image', 0)cv2.imshow('image', result)cv2.waitKey(0)
參考資料:
http://learningtensorflow.com/
http://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow