tensorflow opencv 基本操作

import tensorflow as tf
import cv2
#开始程序,进行测试
hello=tf.constant('hello world')
sess=tf.Session()
print(sess.run(hello))
#图片的读取和展示
img=cv2.imread('baby.jpg',9)
cv2.imshow('imgbaby',img)
cv2.waitKey(0)

#图片的读取和写入。保存为png格式
import cv2
img=cv2.imread("baby.jpg",1)
cv2.imwrite('baby.png',img)

#图片有损压缩
import cv2
img=cv2.imread('baby.jpg',1)
cv2.imwrite('baby-less-quality.jpg',img,(cv2.IMWRITE_JPEG_QUALITY,80)) #压缩参数范围为:0-100 有损压缩

#图片无损压缩
import cv2
img=cv2.imread('baby.jpg',1)
cv2.imwrite('baby-less-quality.png',img,(cv2.IMWRITE_PNG_COMPRESSION,1))#压缩参数范围为:0-9 有损压缩

#图片像素读写
#图片大小计算 长*高*3(三原色)*8(8位256色)
import cv2
img=cv2.imread('baby.jpg',1)
(b,g,r)=img[100,100]
print(b,g,r)#打印一个像素的三原色值

for i in range(1,300):
    img[10+i,100]=(0,0,255)  #写入一条直线
cv2.imshow('imgage',img)
cv2.waitKey(0)
#常量 变量
import tensorflow as tf
data1=tf.constant(2.5,dtype=tf.float32)
data2=tf.Variable(3,name='b')
print(data1)
print(data2)
''' 块注释
sess=tf.Session()
init=tf.global_variables_initializer()
sess.run(init)
print(sess.run(data1))
print(sess.run(data2))
'''
init=tf.global_variables_initializer()
sess=tf.Session()
with sess:
    sess.run(init)
    print(sess.run(data2))

#四则运算  加减乘除
import tensorflow as tf
data1=tf.constant(6)
data2=tf.constant(2)
dataAdd=tf.add(data1,data2)
dataMul=tf.multiply(data1,data2)
dataSub=tf.subtract(data1,data2)
dataDiv=tf.divide(data1,data2)
with tf.Session() as sess:
    print(sess.run(dataAdd))
    print(sess.run(dataSub))
    print(sess.run(dataMul))
    print(sess.run(dataDiv))

#变量 加减乘除
data1=tf.Variable(6)
data2=tf.Variable(2)
dataAdd=tf.add(data1,data2)
dataMul=tf.multiply(data1,data2)
dataSub=tf.subtract(data1,data2)
dataDiv=tf.divide(data1,data2)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(dataAdd))
    print(sess.run(dataSub))
    print(sess.run(dataMul))
    print(sess.run(dataDiv))

# 矩阵运算基础
import tensorflow as tf
data1=tf.placeholder(tf.float32)
data2=tf.placeholder(tf.float32)
dataAdd=tf.add(data1,data2)

with tf.Session() as sess:
    print(sess.run(dataAdd,feed_dict={data1:1,data2:2}))
    print('end')

import tensorflow as tf
data1=tf.constant([[5,6]])
data2=tf.constant([[1,2],
                  [3,4]])
data3=tf.constant([[3,3]])
data4=tf.constant([[0,2],
                  [1,2],
                  [2,2]])
with tf.Session() as sess:
    print(sess.run(data4))#打印整体
    print(sess.run(data4[0]))#打印某一行
    print(sess.run(data4[:,0]))#打印列
    print(sess.run(data4[0,0]))
    print('end!')
    print(sess.run(data1))#打印整体
    print(sess.run(data1[0]))#打印某一行
    print(sess.run(data1[:,0]))#打印列
    print(sess.run(data1[0,0]))

dataMul=tf.matmul(data1,data2)
dataAdd=tf.add(data1,data3)
with tf.Session() as sess:
    print(sess.run(dataMul))
    print(sess.run(dataAdd))

mat0=tf.constant([[0,0,0],[0,0,0]])
mat1=tf.zeros([2,3])
mat2=tf.ones([2,3])
mat3=tf.fill([2,3],15)
print(mat1)
mat4=tf.zeros_like(mat3)
mat5=tf.ones_like(mat3)
mat6=tf.linspace(1.5,2.5,17)
mat7=tf.random_uniform([2,3],-1,17)
with tf.Session() as sess:
    print(sess.run(mat0))
    print(sess.run(mat1))
    print(sess.run(mat2))
    print(sess.run(mat3))
    print(sess.run(mat4))
    print(sess.run(mat5))
    print(sess.run(mat6))
    print(sess.run(mat7))
 

你可能感兴趣的:(人工智能)