TensorFlow與OpenCV,讀取圖片,進行簡單操作並顯示
1 OpenCV讀入圖片,使用tf.Variable初始化爲tensor,加載到tensorflow對圖片進行轉置操作,然後opencv顯示轉置後的結果
import tensorflow as tf
import cv2
file_path = "/home/lei/Desktop/"
filename = "MarshOrchid.jpg"
image = cv2.imread(filename, 1)
cv2.namedWindow('image', 0)
cv2.imshow('image', image)
# Create a TensorFlow Variable
x = 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 tf
import cv2
# First, load the image again
filename = "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/