您的位置:首页 > 运维架构

TensorFlow与OpenCV,读取图片,进行简单操作并显示

2016-09-01 16:00 926 查看
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/

http://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TensorFlow opencv