jupyter

https://www.jianshu.com/p/17dabbc5936e(python 3.7)
mlTF: numpy tensorflow
ipython jupyter
opencv:https://www.pianshen.com/article/39091554701/

hello jupyter

image.png
https://blog.csdn.net/sinat_36502563/article/details/102302392

4.图片的几何变换

4.1图片的缩放

det. 是 determiner 的缩写,
1.调用API:
resize

2.算法:
2.1最近邻域插值法
原理
image.png
代码实现

2.2双线性插值法
原理
image.png

4.2图片移位

任务:读入一张图片,分别用api和原理实现图片位移

image.pngwarpAffine
api算法分析
矩阵拆分、矩阵计算
image.png

  1. #向左移动
  2. import cv2
  3. import numpy as np
  4. img= cv2.imread('1.png',1)
  5. cv2.imshow('src',img)
  6. imgInfo = img.shape
  7. height = imgInfo[0]
  8. width = imgInfo[1]
  9. dst = np.zeros(img.shape,np.uint8)
  10. for i in range(0,height):
  11. for j in range(0,width-100):
  12. dst[i,j+100] = img[i,j]
  13. cv2.imshow('image',dst)
  14. cv2.waitKey(0)

4.3图片镜像

  1. import cv2
  2. import numpy
  3. img = imread('i.png')
  4. imgInfo = img.shape
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. # deep说明图片颜色的组成,RGB
  8. deep = imgInfo[2]
  9. newImgInfo = (height*2,width,deep)
  10. dst = np.zeros(newImgInfo,np,uint8)
  11. # 显示原图+镜像图片
  12. for i in range(0,height):
  13. for j in range(0,width):
  14. dst[i,j]= img[i,j]
  15. #x不变 y=2*h-y-1
  16. dst[height*2-i-1,j]=img[i,j]
  17. # 画中间的那一条红线
  18. for i in range(0,width):
  19. dst[height,i]=(0,0,255)#BGR
  20. cv2.imshow('dst',dst)
  21. cv2.waitKey(0)

小总结

for i,j就是原图片你想展现在新图片的部分
dst计算新图片相对于原图片的坐标

4.4图片缩放

用图片移位学的公式(api)将图片缩一半
image.png

  1. import cv2
  2. import numpy as np
  3. img = cv2.imread('1.png',1)
  4. imgInfo = img.shape
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. matSacle = np.float32([[0.5,0,0],[0,0.5,0]])
  8. dst = cv2.warpAffine(img,matScle,int(width/2),int(height/2))
  9. cv2.imshow('dst',dst)
  10. cv2.waitKey(0)

4.5图片的仿射变换

  1. import cv2
  2. import numpy as np
  3. img= cv2.imread('i.png')
  4. imgInfo = img.shape
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. #src 3->dst 3(左上角 左下角 右上角)
  8. matSrc = np.float32([[0,0],[0,height-1],[width-1,0]])
  9. matDst = np.float32([[50,50],[300,height-200],[width-300],100])
  10. #矩阵组合
  11. matAffine = cv2.getAffineTransform(matSrc,matDst) # mat 第1参数src 第2参数dst
  12. dst = cv2.warpAffine(img,matAffine,(width,height))
  13. cv2.imshow('dst',dst)
  14. cv2.waitKey(0)

4.6图片旋转

  1. #mat 1 center 2 angle 3 sc
  2. #为什么要有缩放的系数(sc):不然旋转超出范围了
  3. matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)
  4. dst = cv2.warpAffine(img,matRotate,(height,width))

5图片特效

5.1灰度处理(最重要,基础,实时性)

1。imread

  1. import cv2
  2. #0代表灰度图片 1代表彩色图片
  3. img0 = cv2.imread('1.png',0)
  4. img1 = cv2.imread('1.png',1)
  5. print(img0.shape)
  6. print(img1.shape)
  7. cv2.imshow('src',img0)

image.png

2.cvtColor

  1. import cv2
  2. img = cv2.imread('1.png')
  3. #颜色空间的转换 1data 2BGR gray
  4. dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  5. cv2.imshow('dst',dst)

3.gray = (r+b+g)/3

  1. import cv2
  2. import numpy as np
  3. img= cv2.imread('i.png')
  4. imgInfo = img.shape
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. #RGB R=G=B =gray (R+G+B)/3
  8. dst = np.zeros((height,width,3),np.uint8)
  9. for i in range(0,height):
  10. for j in range(0,width):
  11. (b,g,r) = img[i,j]
  12. #uint8 进行相加计算可能会溢出,所以要转int
  13. gray = (int(b)+int(g)+int(r))/3
  14. dst[i,j] = np.uint8(gray)
  15. cv2.imshow('dst',dst)
  16. cv2.waitKey(0)

4.gray = r0.299+g0.587+b*0.114

  1. import cv2
  2. import numpy as np
  3. img= cv2.imread('i.png')
  4. imgInfo = img.shape
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. #RGB R=G=B =gray (R+G+B)/3
  8. dst = np.zeros((height,width,3),np.uint8)
  9. for i in range(0,height):
  10. for j in range(0,width):
  11. (b,g,r) = img[i,j]
  12. #uint8 进行相加计算可能会溢出,所以要转int
  13. gray = int(b)*0.114+int(g)*0.587+int(r)*0.299
  14. dst[i,j] = np.uint8(gray)
  15. cv2.imshow('dst',dst)
  16. cv2.waitKey(0)

4.算法优化(实时性)

定点-》浮点 +-/ >>
image.png
浮点运算转为定点运算(
4再/4,即左移两位再右移两位)
image.png
定点运算转为移位运算
image.png

5.2颜色反转

6机器学习

6.1视频与图片的分解和合成

视频的分解

1 load 2 info 3 parse(解码) 4 imshow 5 imwrite

  1. import cv2
  2. cap = cv2.VideoCapture('1.mp4')
  3. isOpened = cap.isOpended
  4. print(isOpened)
  5. fps = cap.get(cv2.CAP_PROP_FPS)
  6. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  7. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  8. print(fps,width,height)
  9. i = 0
  10. while(isOpened):
  11. if i ==10:
  12. break
  13. else:
  14. i = i+1
  15. (flag,frame) = cap.read()#读取每一张flag frame
  16. fileName = 'image'+str(i)+'jpg'
  17. print(fileName)
  18. if flag == True:
  19. cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100]
  20. print('end!')

图片的合成

videoWrite = cv2.VideoWriter(‘2.mp4’,-1,5,size)—>size
接着循环遍历图片加到视频里

  1. import cv2
  2. img = cv2.imread('image1.jpg')
  3. imgInfo = img.shape
  4. size = (imgInfo[1],imgInfo[0])
  5. print(size)
  6. videoWrite = cv2.VideoWriter('2.mp4',-1,5,size)
  7. for i in range(1,11):
  8. fileName = 'image'+str(i)+'.jpg'
  9. img = cv2.imread(fileName)
  10. videoWrite.write(img)
  11. print('end!')

6.2Haar特征+adaboost分类器

Haar特征

原理

image.png
image.png
image.png
image.png

Haar特征的计算量

image.png

快速计算的方法

计算特征->要计算矩形方框中的像素->有快速计算的方法:
image.png
image.png
结论:一个任意的方框,都可以由它相邻的ABCD四个矩形通过加减运算得来

adaboost分类器

原理

image.png
image.png
image.png
image.png

adaboost分类器的训练

image.png

Haar_adaboost人脸识别

  1. import cv2
  2. import numpy as np
  3. face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  4. eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')
  5. img = cv2.imread('face.png')
  6. cv2.imshow('src',img)
  7. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  8. # detect faces 1 (gray)data 2 scale 3 5(目标大小,人脸最小不能小于5个像素)
  9. faces = face_xml.detectMultiScale(gray,1.3,5)
  10. print('faces=',len(faces))
  11. #draw给人脸画方框
  12. for (x,y,w,h) in faces:
  13. cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)#最后一个参数:线条宽度
  14. #detect eyes
  15. roi_face = gray[y:y+h,x:x+w]
  16. roi_color = img[y:y+h,x:x+w]
  17. eyes = eye_xml.detectMultiScale(roi_face)
  18. print('eyes=',len(eyes))
  19. #draw 给眼睛画方框
  20. for (e_x,e_y,e_w,e_h) in eyes:
  21. cv2.rectangle( roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,0,255),2)
  22. cv2.imshow('dst',img)
  23. cv2.waitKey(0)

image.png

6.3Hog特征+SVM

SVM(分类、监督学习)

小案例(给出身高体重,判断男or女)

Hog特征

image.png

Hog_SVM小狮子的识别

数字识别的案例

样本准备http://yann.lecun.com/exdb/mnist/
image.png

image.png

knn最近邻域法

1.Load Data

  1. #本质:knn test 样本K个 max10 8个1 -》1
  2. import tensorflow as tf
  3. import numpy as np
  4. import random
  5. from tensorflow.examples.tutorials.mnist import input_data
  6. # 1.load data 1 fileName 2 one_hot: 1 0000
  7. mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
  8. # 2.属性设置
  9. trainNum = 55000
  10. testNum = 10000
  11. trainSize = 500
  12. testSize = 5
  13. #在trainSize中找到4个与testSize最像的图片
  14. k=4
  15. #data分解
  16. #获取范围在 0-trainNum 中的 trainSize个随机值 (replace不可重复)
  17. trainIndex = np.random.choice(trainNum,trainSize,replace = False)#训练图片的下标(随机选取)
  18. testIndex = np.random.choice(testNum,testSize,replace = False)
  19. trainData = mnist.train.images[trainIndex]#训练图片
  20. trainLabel = mnist.train.labels[trainIndex]#训练标签
  21. testData = mnist.test.images[testIndex]#测试图片
  22. testLabel = mnist.test.labels[testIndex]#测试标签
  23. print('trainData.shape=',trainData.shape)#(500, 784) 一共500张训练图片,大小(宽*高)28*28 =784(一张图片784像素)
  24. print('trainLabel.shape=',trainLabel.shape)#(500, 10)
  25. print('testData.shape=',testData.shape)#(5, 784)
  26. print('testLabel.shape=',testLabel.shape)#(5, 10)
  27. print('testLabel=',testLabel)
  28. #tf input
  29. trainDataInput = tf.placeholder(shape=[None,784],dtype= tf.float32)
  30. trainLabelInput = tf.placeholder(shape=[None,10],dtype = tf.float32)
  31. testDataInput = tf.placeholder(shape=[None,784],dtype= tf.float32)
  32. testLabelInput = tf.placeholder(shape=[None,10],dtype = tf.float32)

image.png\

2.knn test train distance

3.knn 对每一个测试图片,在500个训练图中,挑选k个(4个)最近(最像)的图片

4.用label对应找出k个(4个)最近(最像)的图片 对应的数字值

  1. #knn distance
  2. #增加维度 5*784(2D) 变为5*1*784(3D)
  3. #原因:5张测试784 分别与500张训练784 相减 即 5 500 784(3D) 5*500*784
  4. f1 = tf.expand_dims(testDataInput,1)#维度扩展
  5. f2 = tf.subtract(trainDataInput,f1)
  6. f3 = tf.reduce_sum(tf.abs(f2),reduction_indices=2)#完成数据的累加 多维指定784的维数相加
  7. #选取最接近的4张
  8. f4 = tf.negative(f3)#取反功能
  9. f5,f6 = tf.nn.top_k(f4,k=4)#选取f4最大的4个值 取反 f3最小的4个值
  10. #由index获取具体图片内的数字值 f6 index -> trainLabelInput
  11. f7 = tf.gather(trainLabelInput,f6)
  12. #竖直方向的累加
  13. f8 = tf.reduce_sum(f7,reduction_indices =1 )
  14. #选取在某一个最大的值,获得下标index
  15. #f9 ->test5 image ->5num
  16. f9= tf.argmax(f8,dimension=1)
  17. with tf.Session() as sess:
  18. p1 = sess.run(f1,feed_dict={testDataInput:testData[0:5]})
  19. print('p1.shape=',p1.shape)# (5, 1, 784)
  20. p2 = sess.run(f2,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
  21. print('p2.shape=',p2.shape)#(5, 500, 784)
  22. p3 = sess.run(f3,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
  23. print('p3.shape=',p3.shape)#(5, 500)
  24. print('p3[0,0]=',p3[0,0])#p3[0,0]= 114.79216
  25. p4 = sess.run(f4,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
  26. print('p4.shape=',p4.shape)
  27. print('p4[0,0]=',p4[0,0])#p4[0,0]= -114.79216
  28. p5,p6 = sess.run((f5,f6),feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
  29. print('p5.shape=',p5.shape)#(5, 4) 每张测试图片(5张)分别对应4张最近训练图片
  30. print('p6.shape=',p6.shape)#(5, 4)
  31. print('p5[0,0]=',p5[0,0]) # -45.580387
  32. print('p6[0,0]=',p6[0,0])# 196 (p6是下标index、)
  33. p7 = sess.run(f7,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
  34. print('p7.shape=',p7.shape) #(5, 4, 10)
  35. print('p7=',p7)
  36. p8 = sess.run(f8,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
  37. print('p8.shape=',p8.shape) # (5, 10)
  38. print('p8=',p8)
  39. p9 = sess.run(f9,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
  40. print('p9.shape=',p9.shape) # (5, )
  41. print('p9=',p9)

image.png
image.png

5.检测概率统计

  1. p10 = np.argmax(testLabel[0:5],axis = 1)
  2. print('p10[]=',p10)
  3. j = 0
  4. for i in range(0,5):
  5. if p10[i] == p9[i]:
  6. j = j + 1
  7. print('ac=',j*100/5)

cnn