图片读取与展示
#1.引入OpenCV
#2.调用API
#3.程序暂停stop
import cv2
img = cv.imread('image0.jpg',1) #read image 1.name 2. 0:gray 1:color
cv2.imshow('image',img) #1.name win 2.img
cv2.waitKey(0) #stop
图片写入
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('image1.jpg',img) #1.name 2.data
图片质量
#jpg 有损压缩0-100
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('imageTest.jpg',img,[cv2.IMWRITE_JPEG_QUALITY, 50])
# png 无损压缩 透明度属性
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('imageTest.png',img,[cv2.IMWRITE_PNG_COMPRESSION, 0])
jpg 数字越小 压缩比越高 压缩范围:0-100
png 数字越小 压缩比越低 压缩范围:0-9
像素读取写入
- 像素
- RGB
- 颜色深度 8bit 0-255
- w h 640*480
- 1.14M = 7205473*8 bit /8 (B)=1.14M
- png RGB alpha (透明分量)
- RGB bgr
- bgr b g r
import cv2
img = cv2.imread('image0.jpg',1)
(b,g,r) = img[100,100]
print(b,g,r) #bgr
#10 100---110 100
for i in range(1,100):
img[10+i,100] = (255,0,0)
cv2.imread('image',img)
cv2.waitKey(0) #1000 ms
TensorFlow常量变量定义
类比 语法 API 原理
基础数据类型 运算符 流程 字典 数组
import tensorflow as tf
data1 = tf.constant(2.5)
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
print(sess.run(data1))
import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32)
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
print(sess.run(data1))
# data变量初始化
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
或者
import tensorflow.compat.v1 as tf
data1 = tf.constant(2,dtype=tf.int32)
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
'''
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
print(sess.run(data1))
# data变量初始化
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
sess.close()
'''
init = tf.global_variables_initializer()#变量初始化
tf.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.Session()
with sess:
sess.run(init)
print(sess.run(data2))
TensorFlow运算原理
tensorflow = tensor + graphs(计算图)
tensor 数据
op operation 赋值、四则运算
graphs 数据和操作的过程
session 执行的核心,运算的交互环境
import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32)
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
'''
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
print(sess.run(data1))
# data变量初始化
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
sess.close()
'''
init = tf.compat.v1.global_variables_initializer()#变量初始化
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
sess = tf.compat.v1.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)
tf.compat.v1.disable_eager_execution() #保证sess.run()能够正常运行
with tf.compat.v1.Session() as sess:
print(sess.run(dataAdd))
print(sess.run(dataMul))
print(sess.run(dataSub))
print(sess.run(dataDiv))
print('end!')
import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.Variable(2)
dataAdd = tf.add(data1,data2)
dataMul = tf.multiply(data1,data2)
dataSub = tf.subtract(data1,data2)
dataDiv = tf.divide(data1