图像处理(一)

1、图像获取

import cv2
img = cv2.imread('image0.jpg',1)  # 1、文件名称  2、1:彩色读取 0:灰度读取
cv2.imshow('image',img)  #1:窗体名称 2:展示的文件
cv2.waitKey(10000)
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('image1.jpg',img)  #1:新写入文件名 2:被写文件
import cv2
img = cv2.imread('image0.jpg',1)
cv2.imwrite('image3.png',img,[cv2.IMWRITE_PNG_COMPRESSION,0]) #png使用图片压缩,数值越小压缩比例越小

2、numpy

1、创建矩阵(普通矩阵、零矩阵、单位矩阵)
2、矩阵维度、改查、基本运算
3、矩阵加乘
import numpy as np
data1 = np.array([[1,2,3],[4,5,6]])
data5 = np.array([[1,2],[4,5],[1,2]])
data2 = np.zeros([2,3])
data3 = np.ones([2,3])
data4 = np.array([[1,2,3],[4,5,6]])
print(data1)
print(data2)
print(data3)
print(data1.shape)
print(data1*2)
print(data1+2)
print(data1/2)
print(data1*data4)
print(np.matmul(data1,data5))

3、tensorflow

常量变量定义
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")
data1 = tf.constant(2,dtype=tf.int32)
data2 = tf.Variable(10,name='hh')
print(data1,data2)
init = tf.global_variables_initializer()#变量必须初始化
with tf.Session() as sess: #with语句会自动关闭session
    sess.run(init)
    print(sess.run(data1))
    print(sess.run(data2))```
```python
import tensorflow as tf
data1 = tf.constant(3,dtype=tf.int32)
data2 = tf.constant(2,dtype=tf.int32)
dataadd = tf.add(data1,data2)
datasub = tf.subtract(data1,data2)
datamul = tf.multiply(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))
import tensorflow as tf

data1 = tf.Variable(3,name='a')
data2 = tf.Variable(2,name='b')

dataadd = tf.add(data1,data2)
datacopy = tf.assign(data2,dataadd) #dataadd->data2
datasub = tf.subtract(data1,data2)
datamul = tf.multiply(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))
    print('datacopy1:',sess.run(datacopy))#只有调用sess.run()的时候才会被计算
    print('datacopy.eval',datacopy.eval())
    print('tf.get_default_session()',tf.get_default_session().run(datacopy))
tensorflow=tensor(张量)+计算图

4、mat

#placeholder占位符,在计算的时候需要feed_dict={}
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:3,data2:5}))
print('end!')```

```python
import tensorflow as tf
data1 = tf.constant([[6,6]])#一行两列
data2 = tf.constant([[1,2],
                     [3,4]])#两行两列
data3 = tf.constant([[1],
                     [2]])#两行一列
print(data2.shape)#矩阵维度
with tf.Session() as sess:
    print(sess.run(data2))      #打印整体矩阵
    print(sess.run(data2[0]))   #矩阵第一行
    print(sess.run(data2[:,0])) #矩阵第一列
    print(sess.run(data2[0,0])) #矩阵第一行第一列```

```python
#矩阵的加法和乘法
import tensorflow as tf
data1 = tf.constant([[1,2,3],[4,5,6]])
data2 = tf.constant([[1,2],[3,4],[5,6]])
data3 = tf.constant([[1,2],[3,4],[5,6]])
matAdd = tf.add(data3,data2)
matMul = tf.matmul(data1,data2)
with tf.Session() as sess:
    print(sess.run(matAdd))
    print(sess.run(matMul))```

```python
 #特殊矩阵
#1、空矩阵
import tensorflow as tf
mat0 = tf.zeros([2,3])
mat1 = tf.ones([2,3])
mat2 = tf.fill([2,3],15)
with tf.Session() as sess:
    print(sess.run(mat0))
    print(sess.run(mat1))
    print(sess.run(mat2))```
```python
#2、和已有矩阵大小相同的零矩阵
import tensorflow as tf
mat1 = tf.constant([[1],[2],[3]])
mat2 = tf.zeros_like(mat1)    #和mat1 长宽相等的矩阵
mat3 = tf.linspace(0.0,2.0,11) #从0-2分成10分
mat4 = tf.random_uniform([2,3],-1,2)#2*3矩阵,[-1,2]之间随机数产生的矩阵
with tf.Session() as sess:
    print(sess.run(mat2))
    print(sess.run(mat3))
    print(sess.run(mat4))```

##  5、plot

```python
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7])
y = np.array([23,44,2,5,54,3,4])
plt.plot(x,y,'r',lw=10)
x = np.array([1,2,3,4,5,6,7])
y = np.array([2,4,23,54,4,33,43])
plt.bar(x,y,alpha=0.5,color='b')
plt.show()

6、股票预测

#神经网络逼近股市收盘均价
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#日期、开盘、收盘价格
date = np.linspace(1,15,15)
openPrice = np.array([22,44,42,53,12,53,63,5,51,36,67,35,42,23,45])
closePrice = np.array([3,23,45,56,3,34,67,9,90,45,23,45,63,2,46])
plt.figure('股市预测',facecolor='blue')
plt.ylabel('价格')
plt.xlabel('日期')
for i in range(0,15):
    dateone = np.zeros([2])
    dateone[0] = i
    dateone[1] = i
    priceone = np.zeros([2])
    priceone[0] = openPrice[i]
    priceone[1] = closePrice[i]
    if openPrice[i]>closePrice[i]:
        plt.plot(dateone,priceone,'r',lw=8)
    else:
        plt.plot(dateone,priceone,'g',lw=8)
#plt.show()
dateNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range(0,15):
    dateNormal = i/14
    priceNormal = closePrice[i]/70
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
w1 = tf.Variable(tf.random.uniform([1,10],0,1))
b1 = tf.Variable(tf.zeros([1,10]))
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值