TensorFlow與OpenCV,讀取圖片,進行簡單操作並顯示_TensorFlow

來源:互聯網
上載者:User

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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.