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

本文介绍如何使用TensorFlow和OpenCV进行图像处理任务,包括读取图片、执行简单的图像操作如转置和裁剪,并展示处理结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1 OpenCV读入图片,使用tf.Variable初始化为tensor,加载到tensorflow对图片进行转置操作,然后opencv显示转置后的结果

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import tensorflow as tf  
  2. import cv2  
  3.   
  4. file_path = "/home/lei/Desktop/"  
  5. filename = "MarshOrchid.jpg"  
  6.   
  7. image = cv2.imread(filename, 1)  
  8. cv2.namedWindow('image'0)  
  9. cv2.imshow('image', image)  
  10.   
  11. # Create a TensorFlow Variable  
  12. x = tf.Variable(image, name='x')  
  13.   
  14. model = tf.initialize_all_variables()  
  15.   
  16. with tf.Session() as session:  
  17.   x = tf.transpose(x, perm=[102])  
  18.   session.run(model)  
  19.   result = session.run(x)  
  20.   
  21. cv2.namedWindow('result'0)  
  22. cv2.imshow('result', result)  
  23. cv2.waitKey(0)  




2 OpenCV读入图片,使用tf.placeholder符号变量加载到tensorflow里,然后tensorflow对图片进行剪切操作,最后opencv显示转置后的结果

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import tensorflow as tf  
  2. import cv2  
  3.   
  4. # First, load the image again  
  5. filename = "MarshOrchid.jpg"  
  6. raw_image_data = cv2.imread(filename)  
  7.   
  8. image = tf.placeholder("uint8", [NoneNone3])  
  9. slice = tf.slice(image, [100000], [3000, -1, -1])  
  10.   
  11. with tf.Session() as session:  
  12.     result = session.run(slice, feed_dict={image: raw_image_data})  
  13.     print(result.shape)  
  14.   
  15. cv2.namedWindow('image'0)  
  16. cv2.imshow('image', result)  
  17. cv2.waitKey(0)  


参考资料:

http://learningtensorflow.com/

http://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值